Keep frames at the same timestamps after converting with FFmpeg

24
2013-08
  • jackode

    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%
    
  • Answers
  • slhck

    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.


  • Related Question

    Would like to change audio codec, but keep video settings with ffmpeg
  • Craig Tataryn

    I have a video for which I'd like to convert the audio codec to AAC 320 kbps / 44.100 kHz. What would I use for ffmpeg switches such that all the video settings and codec remain the same, but only the audio codec and settings change?

    Here's my video:

    $ ffmpeg -i Winnipeg.rb\ Scala-Talk.mov FFmpeg version SVN-r25375, Copyright (c) 2000-2010 the FFmpeg developers
      built on Oct  6 2010 13:02:41 with gcc 4.2.1 (Apple Inc. build 5664)
      configuration: --enable-libmp3lame --enable-shared --disable-mmx --arch=x86_64
      libavutil     50.32. 2 / 50.32. 2
      libavcore      0. 9. 1 /  0. 9. 1
      libavcodec    52.92. 0 / 52.92. 0
      libavformat   52.80. 0 / 52.80. 0
      libavdevice   52. 2. 2 / 52. 2. 2
      libavfilter    1.48. 0 /  1.48. 0
      libswscale     0.12. 0 /  0.12. 0
    
    Seems stream 0 codec frame rate differs from container frame rate: 2000.00 (2000/1) -> 10.00 (10/1)
    Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Winnipeg.rb Scala-Talk.mov':
      Metadata:
        major_brand     : qt  
        minor_version   : 537199360
        compatible_brands: qt  
      Duration: 01:10:53.00, start: 0.000000, bitrate: 283 kb/s
        Stream #0.0(eng): Video: h264, yuv420p, 800x598, 94 kb/s, 10 fps, 10 tbr, 1k tbn, 2k tbc
        Stream #0.1(eng): Audio: adpcm_ima_qt, 22050 Hz, 1 channels, s16
        Stream #0.2(eng): Audio: adpcm_ima_qt, 22050 Hz, 1 channels, s16
    At least one output file must be specified
    

    Many thanks in advance! One with with ffmpeg I've never been able to grok is how to just "tweak" files without having to regurgitate every little setting for things you don't want changes.


  • Related Answers
  • harrymc

    Hard to judge what you are doing, since the ffmpeg command was a bit garbled in your post.

    To replace an audio track:

    ffmpeg -i test.avi -i normalized.mp3 -map 0:0 -map 1:0 -vcodec copy -acodec copy new_test.avi
    

    More information about the above parameters is found in: FFmpeg Documentation.

  • Benny

    Why don't you use Handbrake which lets keep the video and change the audio and it is a GUI for FFmpeg and it works like a charm.