FFmpeg patch encoding with Avanti

Avanti is a graphic user interface to FFmpeg and Avisynth.

This article is based on the study to encode ~0.5 TB of video files produced while recording VHS tapes to digital format. The codec selected for video is H.264 and for audio AAC.

Material is mainly consisting of the following properties:

  • Frame size 720 x 576 pixels
  • Frame rate 25 fps
  • Extremely high bit rate

For selecting the suitable profile and level, the targeted decoding environment needs to be defined. The resulting media files should be playable in Apple iPod /iPhoneand Nokia N900, thus the profile and level are limited according to the device limitations. In case the files should be playable in Sony PSP, the limitations would be even more strict.

The frame size and frame rate allows level 3 at the lowest standard. Baseline profile is the "best" profile which both of the devices can play without fail. The resizing is skipped as the main focus will be in getting the items played in N900. iPodwould require slightly smaller frame size (640 x 480). In case you would need the iPod playability, add resizing to your Avisynth template.

In order to achieve such requirements, few FFmpeg presets are used. Best references for their options used are by Robert Swain and Dark_Shikari. The related line in the template attached is:

v_userOpt=-@pass1 -vpre "libx264-fastfirstpass.ffpreset" -vpre "libx264-main.ffpreset" -@pass2 -vpre "libx264-hq.ffpreset" -vpre "libx264-main.ffpreset"

The template in question is used in Avanti Job Manager for patch processing which in turn uses the given presets when initiating FFmpeg via command line.

Avisynth is used for deinterlacing and colour correction, as shown below. These commands are included in the template attached:

V=[DirectShowSource](http://avisynth.org/mediawiki/DirectShowSource "DirectShowSource reads filename using MS DirectShow, the same multimedia playback system which Windows Media Player uses")("@source1_path")
V=[ConvertToYV12](http://avisynth.org/mediawiki/ConvertToYV12 "Starting from v2.50 AviSynth can also deal with a third color format, YV12")(V)
V=[Levels](http://avisynth.org/mediawiki/Levels "The Levels filter adjusts brightness, contrast, and gamma")(V, 16, 1, 235, 0, 255, coring=false)
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\TomsMoComp.dll")
V=[TomsMoComp](http://avisynth.org/mediawiki/TomsMoComp "TomsMoComp is a filter that uses motion compensation and adaptive processing to deinterlace video sources. ")(V,-1,5,1)

LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\UnDot.dll")
V=[UnDot](http://avisynth.org/mediawiki/External_filters#Film_Damage_correction "UnDot is a simple median filter for removing dots, that is stray orphan pixels and mosquito noise")(V)

V=[TemporalSoften](http://avisynth.org/mediawiki/TemporalSoften "The SpatialSoften and TemporalSoften filters remove noise from a video clip by selectively blending pixels")(V,4,4,8,15,2)
LoadPlugin("C:\Program Files\AviSynth 2.5\plugins\Convolution3DYV12.dll")
V=[Convolution3D](http://hellninjacommando.com/con3d/ "Convolution3D is an AviSynth filter that will apply a 3D convolution filter to a video stream.")(V, "vhsBQ")

V=[Tweak](http://avisynth.org/mediawiki/Tweak "This filter provides the means to adjust the hue, saturation, brightness, and contrast of a video clip")(V, sat=0.95)

[Return ](http://avisynth.org/mediawiki/Grammar "As a shorthand, a bare expression as the final statement in a script (or script block) is treated as if the keyword return was present.")V

Additionally, depending of the content quality, some scripts for Avisynth could be added, such as DeVCR. A plugin made by Moscow State University for field shift fixing is worth checking out.

One thing missing in the Job Manager is the ability to save the given files in their originating directories, versus the current way of defining just one unified destination for all the resulting files. Even better would be a directory scanner for files matching a given regular expression and save the resulting files in the same directory with the original.

Output from the Mediainfo for the resulting file is somewhat as such:

Format                           : MPEG-4
Format profile                   : Base Media
Codec ID                         : isom
Overall bit rate                 : 1 119 Kbps
Writing application              : Lavf52.52.0

ID                               : 1
Format                           : AVC
Format/Info                      : Advanced Video Codec
Format profile                   : Baseline@L3.0
Format settings, CABAC           : No
Format settings, ReFrames        : 4 frames
Codec ID                         : avc1
Codec ID/Info                    : Advanced Video Coding
Bit rate mode                    : Variable
Bit rate                         : 1 000 Kbps
Width                            : 720 pixels
Height                           : 576 pixels
Display aspect ratio             : 5:4
Frame rate mode                  : Constant
Frame rate                       : 25.000 fps
Standard                         : PAL
Resolution                       : 8 bits
Colorimetry                      : 4:2:0
Scan type                        : Progressive
Bits/(Pixel*Frame)               : 0.096
Writing library                  : x264 core 83 r1400 20fa784
Encoding settings                : cabac=0 / ref=4 / deblock=1:0:0 / analyse=0x1:0x111 / me=umh / subme=8 / psy=1 / psy_rd=1.0:0.0 / mixed_ref=1 / me_range=16 / chroma_me=1 / trellis=0 / 8x8dct=0 / cqm=0 / deadzone=21,11 / fast_pskip=1 / chroma_qp_offset=-2 / threads=1 / sliced_threads=0 / nr=0 / decimate=1 / mbaff=0 / constrained_intra=0 / bframes=0 / wpredp=0 / keyint=250 / keyint_min=25 / scenecut=40 / intra_refresh=0 / rc_lookahead=40 / rc=2pass / mbtree=1 / bitrate=1000 / ratetol=4.0 / qcomp=0.60 / qpmin=10 / qpmax=51 / qpstep=4 / cplxblur=20.0 / qblur=0.5 / ip_ratio=1.41 / aq=1:1.00

ID                               : 2
Format                           : AAC
Format/Info                      : Advanced Audio Codec
Format version                   : Version 4
Format profile                   : LC
Format settings, SBR             : No
Codec ID                         : 40
Bit rate mode                    : Variable
Bit rate                         : 111 Kbps
Channel(s)                       : 2 channels
Channel positions                : L R
Sampling rate                    : 48.0 KHz

Hopefully someday there will be a solution which would do all the work but would still let the user decide on details. This is where most all-in-one tools fail. Those who succeed in giving user the control are then in another hand too complex to process thousands of files at one go.

Several video codecs exists. Ten years ago most of videos were encoded with Divx 3 and soon followed by Xvid. Recently H.264 has become increasingly popular, but the support for its different profiles is still rather incomplete, keeping the Baseline as the most reasonable choice.

Next step might be the Snow codec from FFmpeg...

Quick links for the tools needed in this set: