0

I'm cutting a file into two parts, and concatenating them together, but certain frames around the boundary of the two files appear out of order, like this:

clip 1: [..12,13,14]
clip 2: [15,16,17...]
clip 3: [...13,15,14,15,16,17... ]

Why does this happen and how can I stop this?

ffmpeg version N-90649-g9825f77ac7
File: https://nofile.io/f/8gC27TobzyX/trailer.mp4
Commands:

ffmpeg -i trailer.mp4 -to 13.92  -an -copyts -c copy first-bun.mp4
ffmpeg -ss 14 -i trailer.mp4 -to 15 -an -video_track_timescale 90000 -copyts bun.mp4
ffmpeg -f concat -i concat.txt -c copy -copyts trailer-keyframed.mp4

where concat.txt is

file 'first-bun.mp4'
file 'bun.mp4'

inspecting the frames with:

ffprobe -of json -select_streams v -show_frames [input]

Gives me, for the last frame of first-bun.mp4:

        {
            "media_type": "video",
            "stream_index": 0,
            "key_frame": 0,
            "pkt_pts": 1256400,
            "pkt_pts_time": "13.960000",
            "best_effort_timestamp": 1256400,
            "best_effort_timestamp_time": "13.960000",
            "pkt_duration": 3600,
            "pkt_duration_time": "0.040000",
            "pkt_pos": "4821742",
            "pkt_size": "1074",
            "width": 1920,
            "height": 1080,
            "pix_fmt": "yuv420p",
            "sample_aspect_ratio": "1:1",
            "pict_type": "B",
            "coded_picture_number": 349,
            "display_picture_number": 0,
            "interlaced_frame": 0,
            "top_field_first": 0,
            "repeat_pict": 0,
            "color_range": "tv",
            "color_space": "smpte170m",
            "color_primaries": "bt470bg",
            "color_transfer": "bt709",
            "chroma_location": "left"
        }
    ]
}

And for the first few frames of bun.mp4:

{
    "frames": [
        {
            "media_type": "video",
            "stream_index": 0,
            "key_frame": 1,
            "pkt_pts": 1260000,
            "pkt_pts_time": "14.000000",
            "pkt_dts": 1260000,
            "pkt_dts_time": "14.000000",
            "best_effort_timestamp": 1260000,
            "best_effort_timestamp_time": "14.000000",
            "pkt_duration": 3600,
            "pkt_duration_time": "0.040000",
            "pkt_pos": "48",
            "pkt_size": "163733",
            "width": 1920,
            "height": 1080,
            "pix_fmt": "yuv420p",
            "sample_aspect_ratio": "1:1",
            "pict_type": "I",
            "coded_picture_number": 0,
            "display_picture_number": 0,
            "interlaced_frame": 0,
            "top_field_first": 0,
            "repeat_pict": 0,
            "chroma_location": "left"
        },
        {
            "media_type": "video",
            "stream_index": 0,
            "key_frame": 0,
            "pkt_pts": 1263600,
            "pkt_pts_time": "14.040000",
            "pkt_dts": 1263600,
            "pkt_dts_time": "14.040000",
            "best_effort_timestamp": 1263600,
            "best_effort_timestamp_time": "14.040000",
            "pkt_duration": 3600,
            "pkt_duration_time": "0.040000",
            "pkt_pos": "163781",
            "pkt_size": "5015",
            "width": 1920,
            "height": 1080,
            "pix_fmt": "yuv420p",
            "sample_aspect_ratio": "1:1",
            "pict_type": "P",
            "coded_picture_number": 1,
            "display_picture_number": 0,
            "interlaced_frame": 0,
            "top_field_first": 0,
            "repeat_pict": 0,
            "chroma_location": "left"
        },
        {
            "media_type": "video",
            "stream_index": 0,
            "key_frame": 0,
            "pkt_pts": 1267200,
            "pkt_pts_time": "14.080000",
            "pkt_dts": 1267200,
            "pkt_dts_time": "14.080000",
            "best_effort_timestamp": 1267200,
            "best_effort_timestamp_time": "14.080000",
            "pkt_duration": 3600,
            "pkt_duration_time": "0.040000",
            "pkt_pos": "168796",
            "pkt_size": "3639",
            "width": 1920,
            "height": 1080,
            "pix_fmt": "yuv420p",
            "sample_aspect_ratio": "1:1",
            "pict_type": "P",
            "coded_picture_number": 2,
            "display_picture_number": 0,
            "interlaced_frame": 0,
            "top_field_first": 0,
            "repeat_pict": 0,
            "chroma_location": "left"
        },

However, in the output file: trailer-keyframed.mp4, I see this around the point of concatenation: UPDATE: now with show_packets

    {
        "type": "packet",
        "codec_type": "video",
        "stream_index": 0,
        "pts": 1263600,
        "pts_time": "14.040000",
        "dts": 1256400,
        "dts_time": "13.960000",
        "duration": 3600,
        "duration_time": "0.040000",
        "size": "5015",
        "pos": "4986758",
        "flags": "__"
    },
    {
        "type": "frame",
        "media_type": "video",
        "stream_index": 0,
        "key_frame": 0,
        "pkt_pts": 1263600,
        "pkt_pts_time": "14.040000",
        "pkt_dts": 1256400,
        "pkt_dts_time": "13.960000",
        "best_effort_timestamp": 1263600,
        "best_effort_timestamp_time": "14.040000",
        "pkt_duration": 3600,
        "pkt_duration_time": "0.040000",
        "pkt_pos": "4809688",
        "pkt_size": "12222",
        "width": 1920,
        "height": 1080,
        "pix_fmt": "yuv420p",
        "sample_aspect_ratio": "1:1",
        "pict_type": "P",
        "coded_picture_number": 348,
        "display_picture_number": 0,
        "interlaced_frame": 0,
        "top_field_first": 0,
        "repeat_pict": 0,
        "color_range": "tv",
        "color_space": "smpte170m",
        "color_primaries": "bt470bg",
        "color_transfer": "bt709",
        "chroma_location": "left"
    },
    {
        "type": "packet",
        "codec_type": "video",
        "stream_index": 0,
        "pts": 1267200,
        "pts_time": "14.080000",
        "dts": 1260000,
        "dts_time": "14.000000",
        "duration": 3600,
        "duration_time": "0.040000",
        "size": "3639",
        "pos": "4991773",
        "flags": "__"
    },
    {
        "type": "frame",
        "media_type": "video",
        "stream_index": 0,
        "key_frame": 1,
        "pkt_pts": 1260000,
        "pkt_pts_time": "14.000000",
        "pkt_dts": 1260000,
        "pkt_dts_time": "14.000000",
        "best_effort_timestamp": 1260000,
        "best_effort_timestamp_time": "14.000000",
        "pkt_duration": 3600,
        "pkt_duration_time": "0.040000",
        "pkt_pos": "4822984",
        "pkt_size": "163774",
        "width": 1920,
        "height": 1080,
        "pix_fmt": "yuv420p",
        "sample_aspect_ratio": "1:1",
        "pict_type": "I",
        "coded_picture_number": 350,
        "display_picture_number": 0,
        "interlaced_frame": 0,
        "top_field_first": 0,
        "repeat_pict": 0,
        "color_range": "tv",
        "color_space": "smpte170m",
        "color_primaries": "bt470bg",
        "color_transfer": "bt709",
        "chroma_location": "left"
    },
    {
        "type": "packet",
        "codec_type": "video",
        "stream_index": 0,
        "pts": 1270800,
        "pts_time": "14.120000",
        "dts": 1263600,
        "dts_time": "14.040000",
        "duration": 3600,
        "duration_time": "0.040000",
        "size": "2865",
        "pos": "4995412",
        "flags": "__"
    },
    {
        "type": "frame",
        "media_type": "video",
        "stream_index": 0,
        "key_frame": 0,
        "pkt_pts": 1263600,
        "pkt_pts_time": "14.040000",
        "pkt_dts": 1263600,
        "pkt_dts_time": "14.040000",
        "best_effort_timestamp": 1263600,
        "best_effort_timestamp_time": "14.040000",
        "pkt_duration": 3600,
        "pkt_duration_time": "0.040000",
        "pkt_pos": "4986758",
        "pkt_size": "5015",
        "width": 1920,
        "height": 1080,
        "pix_fmt": "yuv420p",
        "sample_aspect_ratio": "1:1",
        "pict_type": "P",
        "coded_picture_number": 351,
        "display_picture_number": 0,
        "interlaced_frame": 0,
        "top_field_first": 0,
        "repeat_pict": 0,
        "color_range": "tv",
        "color_space": "smpte170m",
        "color_primaries": "bt470bg",
        "color_transfer": "bt709",
        "chroma_location": "left"
    },
    {
        "type": "packet",
        "codec_type": "video",
        "stream_index": 0,
        "pts": 1285200,
        "pts_time": "14.280000",
        "dts": 1267200,
        "dts_time": "14.080000",
        "duration": 3600,
        "duration_time": "0.040000",
        "size": "8918",
        "pos": "4998277",
        "flags": "__"
    },
    {
        "type": "frame",
        "media_type": "video",
        "stream_index": 0,
        "key_frame": 0,
        "pkt_pts": 1267200,
        "pkt_pts_time": "14.080000",
        "pkt_dts": 1267200,
        "pkt_dts_time": "14.080000",
        "best_effort_timestamp": 1267200,
        "best_effort_timestamp_time": "14.080000",
        "pkt_duration": 3600,
        "pkt_duration_time": "0.040000",
        "pkt_pos": "4991773",
        "pkt_size": "3639",
        "width": 1920,
        "height": 1080,
        "pix_fmt": "yuv420p",
        "sample_aspect_ratio": "1:1",
        "pict_type": "P",
        "coded_picture_number": 352,
        "display_picture_number": 0,
        "interlaced_frame": 0,
        "top_field_first": 0,
        "repeat_pict": 0,
        "color_range": "tv",
        "color_space": "smpte170m",
        "color_primaries": "bt470bg",
        "color_transfer": "bt709",
        "chroma_location": "left"
    },

output of the concat command

ffmpeg -f concat -i concat.txt -c copy -copyts trailer-keyframed.mp4
ffmpeg version N-90649-g9825f77ac7 Copyright (c) 2000-2018 the FFmpeg developers
  built with gcc 7.3.0 (GCC)
  configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-bzlib --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth
  libavutil      56. 13.100 / 56. 13.100
  libavcodec     58. 17.100 / 58. 17.100
  libavformat    58. 11.101 / 58. 11.101
  libavdevice    58.  2.100 / 58.  2.100
  libavfilter     7. 14.100 /  7. 14.100
  libswscale      5.  0.102 /  5.  0.102
  libswresample   3.  0.101 /  3.  0.101
  libpostproc    55.  0.100 / 55.  0.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001ceada64880] Auto-inserting h264_mp4toannexb bitstream filter
Input #0, concat, from 'concat.txt':
  Duration: N/A, start: 0.000000, bitrate: 2755 kb/s
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/bt709), 1920x1080 [SAR 1:1 DAR 16:9], 2755 kb/s, 25 fps, 25 tbr, 90k tbn, 180k tbc
    Metadata:
      handler_name    : VideoHandler
Output #0, mp4, to 'trailer-keyframed.mp4':
  Metadata:
    encoder         : Lavf58.11.101
    Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, smpte170m/bt470bg/bt709), 1920x1080 [SAR 1:1 DAR 16:9], q=2-31, 2755 kb/s, 25 fps, 25 tbr, 90k tbn, 90k tbc
    Metadata:
      handler_name    : VideoHandler
Stream mapping:
  Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mov,mp4,m4a,3gp,3g2,mj2 @ 000001ceada6d0c0] Auto-inserting h264_mp4toannexb bitstream filter
frame=  375 fps=0.0 q=-1.0 Lsize=    4959kB time=00:00:14.88 bitrate=2730.1kbits/s speed= 476x
video:4954kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.106020%
1

Frame 15 isn't repeated. During streamcopy, ffmpeg has no ability to duplicate or drop frames from the middle of a stream.

Check these details of the relevant frames

    "pkt_pts_time": "13.960000",
    "pkt_pos": "4821910",
    "pkt_size": "1074",
    "pict_type": "B",
    "coded_picture_number": 349,

    "pkt_pts_time": "14.040000",
    "pkt_pos": "4809688",
    "pkt_size": "12222",
    "pict_type": "P",
    "coded_picture_number": 348,

    "pkt_pts_time": "14.000000",
    "pkt_pos": "4822984",
    "pkt_size": "163774",
    "pict_type": "I",
    "coded_picture_number": 350,

    "pkt_pts_time": "14.040000",
    "pkt_pos": "4986758",
    "pkt_size": "5015",
    "pict_type": "P",
    "coded_picture_number": 351,

    "pkt_pts_time": "14.080000",
    "pkt_pos": "4991773",
    "pkt_size": "3639",
    "pict_type": "P",
    "coded_picture_number": 352,

Note the coded_picture_number and byte position. The 2nd frame is a P frame needed for the decoding of the first frame, a B-frame, but it won't be displayed itself. If you do a show_packets, you should see a discard flag for that 2nd frame (_D)

  • Is there really no error here? When I attempt to playback my output file frame-by-frame in VLC, I get a EOF reached message at this timestamp , and cannot step past this frame. When I do the same in WMP, I can see that a frame actually has been duplicated, or at least, that's how it's rendered. When I step through the input file, this frame does not appear twice. – House- 'Reinstate Monica' -man Apr 13 '18 at 20:10
  • I did a show_packets on the output, and do not see a discard flag for the first 14.04 frame. – House- 'Reinstate Monica' -man Apr 13 '18 at 20:16
  • That's a probliem. Let me see if discard flags can be set on individual packets. Else you have to see that you only encode whole GOPs from the source file. – Gyan Apr 13 '18 at 20:49
  • I followed your suggestion and made sure that I was only splitting at GOP boundaries and this solved my problem. Everything is in order now, and there are no errors. If you want to post that as another answer, or edit this one to include this information, I'll accept it – House- 'Reinstate Monica' -man Apr 13 '18 at 22:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.