Cutting and merging camera's H264 video results in corrupt file

0

1

I am not sure is the problem with the specific video or incorrect commands. I have tested with the latest ffmpeg 4/4/2016 build and older v2.8.6. The Sample.avi is recorded by a PTZ camera.

To cut and merge a video file I used the commands from this post.

# skip 30 seconds and re-mux a 60 seconds segment
ffmpeg -ss 30 -t 60 -i D:\VideoCutting\Sample.avi -c:v copy -c:a copy -bsf:v h264_mp4toannexb D:\VideoCutting\Segment1.ts

# skip 3600 seconds and re-mux a 60 seconds segment
ffmpeg -ss 3600 -t 60 -i D:\VideoCutting\Sample.avi -c:v copy -c:a copy -bsf:v h264_mp4toannexb D:\VideoCutting\Segment2.ts

The output:

Input #0, avi, from 'D:\VideoCutting\Sample.avi':
  Duration: 06:05:07.14, start: 0.000000, bitrate: 2479 kb/s
    Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], 2476 kb/s, 14.98 fps, 14.99 tbr, 14.98 tbn, 59.94 tbc
Output #0, mpegts, to 'D:\VideoCutting\Segment1.ts':
  Metadata:
    encoder         : Lavf57.29.101
    Stream #0:0: Video: h264 (H264 / 0x34363248), yuv420p, 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 2476 kb/s, 14.98 fps, 14.99 tbr, 90k tbn, 14.98 tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)

Error:

[NULL @ 05149480] Packet header is not contained in global extradata, corrupted stream or invalid MP4/AVCC bitstream
[NULL @ 05149480] Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
[mpegts @ 051c8420] Timestamps are unset in a packet for stream 0. This is depre cated and will stop working in the future. Fix your code to set the timestamps properly

The video segments were created and they are playable.

However, when I join the segments the merged video is not playable anymore:

# merge the segments and re-mux them as mp4
ffmpeg -i "concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segment2.ts" -c:v copy -c:a copy -movflags empty_moov -flags global_header -bsf:v dump_extra Merged.mp4

Result:

Input #0, mpegts, from 'concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segme
nt2.ts':
  Duration: 00:01:00.15, start: 1.400000, bitrate: 7610 kb/s
  Program 1
    Metadata:
      service_name    : Service01
      service_provider: FFmpeg
    Stream #0:0[0x100]: Video: h264 (High) ([27][0][0][0] / 0x001B), yuv420p, 19
20x1080 [SAR 1:1 DAR 16:9], 29.97 fps, 14.99 tbr, 90k tbn, 59.94 tbc
Output #0, mp4, to 'Merged.mp4':
  Metadata:
    encoder         : Lavf57.29.101
    Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 1920x1080 [SAR 1
:1 DAR 16:9], q=2-31, 29.97 fps, 14.99 tbr, 90k tbn, 90k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mpegts @ 0003ca60] DTS 126000 < 5539218 out of order
frame= 1803 fps=0.0 q=-1.0 Lsize=   51655kB time=00:02:00.26 bitrate=3518.7kbits
/s speed= 601x
video:51633kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing
 overhead: 0.042451%

Error: [mpegts @ 0003ca60] DTS 126000 < 5539218 out of order

From other posts I can guess the problem is in the video file which is not properly formatted. Can I cut and merge it without re-encoding or do I have to somehow fix the video file first?

J Pollack

Posted 2016-04-05T08:22:44.227

Reputation: 143

Answers

0

So, you have H.264 in AVI and looks like the H.264 bitstream filter isn't meant for that.

Use this to generate the segments:

# skip 30 seconds and re-mux a 60 seconds segment
ffmpeg -ss 30 -t 60 -i D:\VideoCutting\Sample.avi -c copy -avoid_negative_ts make_zero -fflags +genpts D:\VideoCutting\Segment1.ts

And then merge segments

ffmpeg -i "concat:D:\VideoCutting\Segment1.ts|D:\VideoCutting\Segment2.ts" -c copy -flags +global_header -fflags +genpts Merged.mp4

Gyan

Posted 2016-04-05T08:22:44.227

Reputation: 21 016

This is it! This is what works for me. How did you know how to do it? Could you please explain a bit further so next time I could come to the solution by myself? – J Pollack – 2016-04-05T13:16:22.153

Just for info, I still got the timestamp warning and [mpegts @ 02ecc9a0] DTS 126000 < 5539218 out of order [mpegts @ 02ecc9a0] DTS 126000 < 5533210 out of order. Could you please explain them as well? – J Pollack – 2016-04-05T13:17:40.770

This cutting and merging is fr34king fast. Just a blink of an eye! BTW does it work with video with audio as well? I cannot test as I currently have mute videos... – J Pollack – 2016-04-05T13:23:41.630