Keep frames at the same timestamps after converting with FFmpeg

5,398

You're forcing FFmpeg to use a different frame rate:

Input #0, matroska,webm, from 'spider.mkv':
 … 23.98 fps
Output #0, mpegts, to 'spideddr.ts':
 … 24 tbc

Try leaving out the -r option altogether and see if that solves it, or explicitly set the frame rate to 23.98.

If that doesn't work, I suspect it's because the original input video stream is corrupt. x264 is quite eager to tell you that it has discarded some reference frames and duplicated others during encoding. There's no way to avoid that really, because it's a problem with the source stream—wherever it comes from.

Also, is there any reason to use an MPEG-TS container? MP4 would work just as well here and might be supported better by various playback devices.

Share:
5,398
jackode
Author by

jackode

iOS, full stack web application developer

Updated on September 18, 2022

Comments

  • jackode
    jackode almost 2 years

    I use this command to convert

    ffmpeg -i spider.mkv  -acodec libfaac -ar 48000 -ab 128k -ac 2  -vcodec libx264 -vf "scale=480:-1" -r 24 -f mpegts spider.ts
    

    but frames' timestamp in the output file (spider.ts) is different from original file. Frame at 1:00:00 in spider.mkv differs from frame at 1:00:00 in spider.ts

    What is the reason for that? And what option should I use solve it?

    FFmpeg's output:

    ffmpeg version N-44232-gd93a53a Copyright (c) 2000-2012 the FFmpeg developers
      built on Sep 12 2012 13:20:28 with Apple clang version 4.0 (tags/Apple/clang-421.0.60) (based on LLVM 3.1svn)
      configuration: --enable-nonfree --enable-libfaac --enable-libfreetype --enable-libmp3lame --enable-libx264 --enable-gpl --cc=cc --cxx=c++ --extra-cflags=-I/usr/X11/include/freetype2/freetype --extra-cflags=-I/usr/X11/include/freetype2 --extra-cflags=-I/usr/X11/include --extra-ldflags=-L/usr/X11/lib --extra-ldflags=-lfreetype --extra-ldflags=-lz --extra-ldflags=-lbz2
      libavutil      51. 72.100 / 51. 72.100
      libavcodec     54. 55.100 / 54. 55.100
      libavformat    54. 25.105 / 54. 25.105
      libavdevice    54.  2.100 / 54.  2.100
      libavfilter     3. 16.100 /  3. 16.100
      libswscale      2.  1.101 /  2.  1.101
      libswresample   0. 15.100 /  0. 15.100
      libpostproc    52.  0.100 / 52.  0.100
    Input #0, matroska,webm, from 'spider.mkv':
      Metadata:
        creation_time   : 2012-10-28 04:27:50
      Duration: 02:16:17.16, start: 0.000000, bitrate: 1629 kb/s
        Stream #0:0(eng): Video: h264 (High), yuv420p, 1280x544 [SAR 1:1 DAR 40:17], 23.98 fps, 23.98 tbr, 1k tbn, 47.95 tbc (default)
        Stream #0:1(eng): Audio: dts (DTS), 48000 Hz, 5.1(side), s16, 768 kb/s (default)
    [mpegts @ 0x7fc151035e00] muxrate VBR, pcr every 2 pkts, sdt every 200, pat/pmt every 40 pkts
    Output #0, mpegts, to 'spideddr.ts':
      Metadata:
        encoder         : Lavf54.25.105
        Stream #0:0(eng): Video: h264, yuv420p, 480x204 [SAR 1:1 DAR 40:17], q=-1--1, 90k tbn, 24 tbc (default)
        Stream #0:1(eng): Audio: aac, 48000 Hz, stereo, s16, 128 kb/s (default)
    Stream mapping:
      Stream #0:0 -> #0:0 (h264 -> libx264)
      Stream #0:1 -> #0:1 (dca -> libfaac)
    Press [q] to stop, [?] for help
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding onedrop=0    
    [h264 @ 0x7fc152380600] number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding onedrop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    [h264 @ 0x7fc152381e00] mmco: unref short failure
    [h264 @ 0x7fc152381e00] number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one
    [h264 @ 0x7fc15237ee00] reference picture missing during reorder
    [h264 @ 0x7fc15237ee00] Missing reference picture
    [h264 @ 0x7fc15237ee00] reference picture missing during reorder
    [h264 @ 0x7fc15237ee00] Missing reference picture
    [h264 @ 0x7fc15237ee00] reference picture missing during reorder
    [h264 @ 0x7fc15237ee00] Missing reference picture
    [h264 @ 0x7fc152380000] mmco: unref short failure
    [h264 @ 0x7fc152380000] number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one
    [h264 @ 0x7fc15237ee00] reference picture missing during reorder
    [h264 @ 0x7fc15237ee00] Missing reference picture
    [h264 @ 0x7fc152381800] mmco: unref short failure
    number of reference frames (0+6) exceeds max (5; probably corrupt input), discarding one drop=0    
    Read error20 fps=172 q=25.0 size=  226486kB time=01:18:37.84 bitrate= 393.3kbits/s dup=113 drop=0    
    frame=113267 fps=172 q=-1.0 Lsize=  226680kB time=01:18:39.46 bitrate= 393.5kbits/s dup=113 drop=0    
    video:120521kB audio:73724kB subtitle:0 global headers:0kB muxing overhead 16.698057%