title: What ffmpeg settings to ensure 0 duplicate frames and 0 dropped frames when capturing to mpeg-2 program stream using ffmpeg/avfoundation on Mac?
body: <p>I'm trying to capture to a DVD compliant mpeg-2 file (ffmpeg: <code>-target ntsc-dvd</code>) from the HDMI output of a camcorder into a Magewell HDMI to USB 3.0 box into my Late 2012 15" non-retina MacBook Pro (quad core 2.3, 16gb ram, ssd), using ffmpeg/avfoundation.</p>
  
  <p>I've tried everything I can think of, or find online. I'm still getting duplicate and dropped frames, which either leads to audio/video sync issues, or audio dropouts, especially for longer recordings. I need this to be stable for recordings of up to 2.5 hours. This is the Terminal output for a 1.5 hour recording:</p>
  
  <pre><code>Lapaki:~ Lapaki$ /Users/Lapaki/Desktop/ffmpeg -f avfoundation -video_size 960x540 -pixel_format uyvy422 -framerate ntsc -i "XI:XI" -vf crop=iw-240:ih:120:0 -target ntsc-dvd -aspect 4:3 -q:v 3 -ab 256k /Users/Lapaki/Desktop/FF\ Test/`date +%F`\ `date +%H_%M_%S`.mpg
  ffmpeg version 3.2.3-tessus Copyright (c) 2000-2017 the FFmpeg developers
    built with Apple LLVM version 8.0.0 (clang-800.0.42.1)
    configuration: --cc=/usr/bin/clang --prefix=/opt/ffmpeg --extra-version=tessus --enable-avisynth --enable-fontconfig --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopus --enable-libschroedinger --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzmq --enable-version3 --disable-ffplay --disable-indev=qtkit --disable-indev=x11grab_xcb
    libavutil      55. 34.101 / 55. 34.101
    libavcodec     57. 64.101 / 57. 64.101
    libavformat    57. 56.101 / 57. 56.101
    libavdevice    57.  1.100 / 57.  1.100
    libavfilter     6. 65.100 /  6. 65.100
    libswscale      4.  2.100 /  4.  2.100
    libswresample   2.  3.100 /  2.  3.100
    libpostproc    54.  1.100 / 54.  1.100
  Input #0, avfoundation, from 'XI:XI':
    Duration: N/A, start: 610606.984208, bitrate: N/A
      Stream #0:0: Video: rawvideo (UYVY / 0x59565955), uyvy422, 960x540, 29.97 fps, 29.97 tbr, 1000k tbn, 1000k tbc
      Stream #0:1: Audio: pcm_f32le, 48000 Hz, stereo, flt, 3072 kb/s
  Output #0, dvd, to '/Users/Lapaki/Desktop/FF Test/2017-02-15 17_46_28.mpg':
    Metadata:
      encoder         : Lavf57.56.101
      Stream #0:0: Video: mpeg2video (Main), yuv420p, 720x480 [SAR 8:9 DAR 4:3], q=2-31, 6000 kb/s, 29.97 fps, 90k tbn, 29.97 tbc
      Metadata:
        encoder         : Lavc57.64.101 mpeg2video
      Side data:
        cpb: bitrate max/min/avg: 9000000/0/6000000 buffer size: 1835008 vbv_delay: -1
      Stream #0:1: Audio: ac3, 48000 Hz, stereo, fltp, 256 kb/s
      Metadata:
        encoder         : Lavc57.64.101 ac3
  Stream mapping:
    Stream #0:0 -&gt; #0:0 (rawvideo (native) -&gt; mpeg2video (native))
    Stream #0:1 -&gt; #0:1 (pcm_f32le (native) -&gt; ac3 (native))
  Press [q] to stop, [?] for help
  [swscaler @ 0x7fd315892800] Warning: data is not aligned! This can lead to a speedloss
  frame=   20 fps=0.0 q=3.0 size=     298kB time=00:00:00.65 bitrate=3721.4kbits/sframe=   35 fps= 35 q=3.0 size=     498kB time=00:00:01.13 bitrate=3591.2kbits/sframe=   50 fps= 33 q=3.0 size=     708kB time=00:00:01.64 bitrate=3519.4kbits/sframe=   65 fps= 32 q=3.0 size=     920kB time=00:00:02.16  
  ...
   bitrate=2721.7kbits/frame=162094 fps= 30 q=3.0 size= 1796936kB time=01:30:08.47 bitrate=2721.7kbits/frame=162109 fps= 30 q=3.0 size= 1797142kB time=01:30:08.98 bitrate=2721.8kbits/frame=162110 fps= 30 q=3.0 Lsize= 1797202kB time=01:30:09.01 bitrate=2721.9kbits/s dup=221 drop=0 speed=   1x    
  video:1579050kB audio:168069kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 2.866632%
  </code></pre>
  
  <p>I deleted the middle part (denoted by the "..."), which is just a lot more of the same accumulating information. By the end there are 221 duplicate frames, and for this one I didn't get any dropped frames, but that happens every other time as well it seems.</p>
  
  <p>With this code, the audio seems to stay in pretty good sync, but I get little dropouts every 30 seconds to a minute or so. On this recording, there are dropouts at:</p>
  
  <p>00:00:43, 00:01:19, 00:01:47, 00:02:17, 00:03:18, ...</p>
  
  <p>I stopped listening there; they happen at the end too, so I assume similarly spaced dropouts are happening throughout the file.</p>
  
  <p>Is there some secret ffmpeg code to ensure there are no dropped or duplicate frames when capturing from a live source to dvd compliant mpeg-2 files?</p>
  
  <p>When I convert from pretty much any type of file to mpeg-2 files using <code>-target ntsc-dvd</code>, the speed is something like 10x on this machine, so it seems like it would have no problem keeping up with a live source, right?</p>
  
  <p>I've also tried constant bit rate using <code>-b:v 5000k -minrate 5000k -maxrate 5000k -bufsize 2500k</code>, which also doesn't prevent dropped/duplicate frames.</p>
  
  <p>I've tried separating out the audio and video inputs, which doesn't solve it.</p>
  
  <p>I've tried using <code>-vsync 0</code> on the video input, which does seem to solve the issue, because the output doesn't report dups/drops, but the audio/video drift out of sync more and more, so that hasn't worked either.</p>
  
  <p>Thanks so much for any help. I've been testing and testing and searching and searching for weeks...</p>