FFmpeg Settings – Convert VHS MPEG2/MP2 to H264/MP4 VBR

ffmpegvideovideo conversionvideo-encoding

My input is as follows:

Input #0, mpeg, from 'D:\Videos\2015.mpg':
Duration: 01:49:08.74, start: 0.220000, bitrate: 4940 kb/s
Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p(tv, bt470bg), 720x576 [SAR 16:15 DAR 4:3], max. 7500 kb/s, 25 fps, 25 tbr, 90k tbn, 50 tbc
Stream #0:1[0x1c0]: Audio: mp2, 48000 Hz, stereo, s16p, 224 kb/s

The file is almost 4GB for less than 2 hours. It's typical (or maybe even slightly worse than typical) VHS quality with interspersed blank screens for a few minutes (hence need for VBR).

CPU is 4-core Inteal 3770K: MMX2 SSE2Fast SSSE3 SSE4.2 AVX

Question: what ffmpeg parameters should I use to convert this input to MP4(H264/AAC) reasonably quickly (several times faster than actual rate, i.e. for this file in no more than 30 minutes) and reducing file size by at least a half. I guess quality is of a lesser concern since it's a VHS rip anyways, and there is plenty of static and artifacts. What command line switches (for Windows) should I use?

EDIT/UPDATE/BACKSTORY:

After a suggestion I am elaborating a bit…
I would have loved to use ffmpeg directly to capture VHS video into MP4, but the problem is that my capture devise driver is configured/installed as so:

C:\Tools\video.tools\ffmpeg-20151123-git-12a419d-win64-static\bin>ffmpeg -loglevel verbose -list_options true -f dshow -i video="ezcap Video Grabber" -loglevel verbose
ffmpeg version N-76822-g12a419d Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 5.2.0 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
  libavutil      55.  9.100 / 55.  9.100
  libavcodec     57. 16.100 / 57. 16.100
  libavformat    57. 19.100 / 57. 19.100
  libavdevice    57.  0.100 / 57.  0.100
  libavfilter     6. 15.100 /  6. 15.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
[dshow @ 0000018baeadb920] DirectShow video device options (from video devices)
[dshow @ 0000018baeadb920]  Pin "Video YUV Out" (alternative pin name "2")
[dshow @ 0000018baeadb920]   pixel_format=uyvy422  min s=160x120 fps=25 max s=720x576 fps=25
[dshow @ 0000018baeadb920]   pixel_format=uyvy422  min s=160x120 fps=29.97 max s=720x486 fps=29.97
[dshow @ 0000018baeadb920]   pixel_format=uyvy422  min s=160x120 fps=29.97 max s=720x480 fps=29.97
[dshow @ 0000018baeadb920]  Pin "Audio PCM Out" (alternative pin name "3")
[dshow @ 0000018baeadb920] Crossbar Switching Information for ezcap Video Grabber:
[dshow @ 0000018baeadb920]   Crossbar Output pin 0: "Video Decoder" related output pin: 1 current input pin: 1 compatible input pins: 1 2
[dshow @ 0000018baeadb920]   Crossbar Output pin 1: "Audio Decoder" related output pin: 0 current input pin: 5 compatible input pins: 5
[dshow @ 0000018baeadb920]   Crossbar Input pin 0 - "Video Tuner" related input pin: 4
[dshow @ 0000018baeadb920]   Crossbar Input pin 1 - "Video Composite" related input pin: 5
[dshow @ 0000018baeadb920]   Crossbar Input pin 2 - "S-Video" related input pin: 5
[dshow @ 0000018baeadb920]   Crossbar Input pin 3 - "Video YRYBY" related input pin: 5
[dshow @ 0000018baeadb920]   Crossbar Input pin 4 - "Audio Tuner" related input pin: 0
[dshow @ 0000018baeadb920]   Crossbar Input pin 5 - "Audio Line" related input pin: 1

The issue is that there is no separate DirectShow Audio device – only Video device (which has both Video and Audio pins). ffmpeg seems to have issues trying to capture both video+audio with such a setup:

ffmpeg -loglevel verbose -f dshow -i video="ezcap Video Grabber":audio="ezcap Video Grabber" test5.mp4
ffmpeg version N-76822-g12a419d Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 5.2.0 (GCC)
  configuration: --enable-gpl --enable-version3 --disable-w32threads --enable-avisynth --enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libdcadec --enable-libfreetype --enable-libgme --enable-libgsm --enable-libilbc --enable-libmodplug --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger --enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxavs --enable-libxvid --enable-libzimg --enable-lzma --enable-decklink --enable-zlib
  libavutil      55.  9.100 / 55.  9.100
  libavcodec     57. 16.100 / 57. 16.100
  libavformat    57. 19.100 / 57. 19.100
  libavdevice    57.  0.100 / 57.  0.100
  libavfilter     6. 15.100 /  6. 15.100
  libswscale      4.  0.100 /  4.  0.100
  libswresample   2.  0.101 /  2.  0.101
  libpostproc    54.  0.100 / 54.  0.100
[dshow @ 0000017449b7c0e0] Could not find audio only device with name [ezcap Video Grabber] among source devices of type audio.
[dshow @ 0000017449b7c0e0] Searching for audio device within video devices for ezcap Video Grabber
[dshow @ 0000017449b7c0e0] Could not run graph (sometimes caused by a device already in use by other application)
video=ezcap Video Grabber:audio=ezcap Video Grabber: Input/output error

Hence, I was forced to use the software that came with the capture device: ArcSoft ShowBiz, which does the job, but is less than optimal due to complicated setup and UI, and because it produces MPEG2's rather than more compact MP4's.

Best Answer

Encoding from a VHS source

ffmpeg -i input -vf "yadif=1:1,hqdn3d=3,drawbox=y=ih-h:w=0:h=6:t=max,format=yuv420p" \
-crf 23 -preset medium -c:a aac -movflags +faststart \
-metadata title="Title" output.mp4

filters

Encoding form a VHS input can be improved with some filters:

  • yadif is a deinterlacer. See the documentation and try out the different modes to see what looks best to you. Alternatively, you could try the w3fdif deinterlacing filter, but I haven't tested that one yet.

  • hqdn3d is a video denoiser. VHS can be noisy, so experiment with various values, and try without it too. Denoising can improve compressibility, but too much denoising will add noticeable blur.

  • drawbox in this example is is making a 6 pixel black box on the very bottom to mask any head switching noise. You may have to adjust the height, or just remove it. This is better than cropping because simply covering it up avoids changing the aspect ratio: if that is not a concern the feel free to crop instead.

  • format is added to use a widely compatible pixel format. It does not really matter with your input though since it is already yuv420p.

other options

  • -crf and -preset will allow you to adjust the output quality and encoding speed. See FFmpeg Wiki: H.264 Video Encoding Guide.

  • -movflags +faststart is useful if your viewers will watch via progressive download. After encoding it will move some info to the beginning of the file so it can begin playback quicker.

  • You will need to add -strict experimental if using an old ffmpeg: the native FFmpeg AAC encoder used to be considered experimental but that is now no longer the case.

Related Question