On video formats

On a trip recently my friends and I recorded video on a few different devices, all of which used several different video formats.  We ended up with the following flavours:

MTS, x264, 50fps (interlaced), 1440x1080 (Effective ratio 1920x1080)
MP4, x264, 25fps (progressive), 1440x1080 (Effective ratio 1920x1080)
MP4, x264, 30fps (progressive), 1920x1080

We wanted to join all the clips together in movie maker, but unfortunately Windows Movie Maker is not that good at making all these different flavours look the same when they are run together - some are smooth (ish), some are noticeably more jagged and so on.

I first decided on a common format that I would try to convert all the video to.  The obvious choice for codec is x264, and MP4 is also the container of choice these days.  Frame size would stay at 1920x1080, and the video would be progressive (as all screens these days are progressive in nature).  The only real decision was frame rate.  Naturally the 50fps video looked smoothest, and was noticeably staggered once resampled at 25fps.  In the end I decided to convert everything to 50fps.  For the MTS video, this was straightforward using Avidemux with  yadif and scaling filters:

adm = Avidemux()
adm.videoCodec("x264", <omitted>)
adm.addVideoFilter("yadif", "mode=1", "order=1")
adm.addVideoFilter("swscale", "width=1920", "height=1080", "algo=2", "sourceAR=1", "targetAR=1")
adm.addVideoFilter("resampleFps", "mode=0", "newFpsDen=1000", "newFpsNum=50000")
adm.audioClearTracks()
adm.setSourceTrackLanguage(0,"unknown")
adm.audioAddTrack(0)
adm.audioCodec(0, "LavAAC");
adm.audioSetDrc(0, 0)
adm.audioSetShift(0, 0,0)
adm.setContainer("MP4V2", "optimize=0", "add_itunes_metadata=0")

Avidemux can be used for batch processing quite easily, just save the project file and remove the lines at the top of the project file that load the video and set the markers (else we end up cutting longer videos short!). Then we need a batch or powershell script to pass all the files we want to process, I used the following:

set avidemux="C:\Program Files\Avidemux 2.6\avidemux.exe"
for %%f in (*.MTS) do %avidemux% --force-alt-h264 --load "%%f" --run MTS2MP4.py --save "%%f.mp4" --quit

Next up I needed a way to convert the 25 and 30fps video to 50fps.  There seems to be two ways to do this - either fill the gaps with the existing frames, or predict the intermediate frames based on the frames either side.  Filling the gaps is not going to make the video any smoother, so I opted for the latter.  MeGui is the tool that gets the job done here - it uses AviSynth to do the heavy lifting (see http://mewiki.project357.com/wiki/Main_Page).  The Avisynth script I used:

Setmemorymax(512)
SetMTMode(3,4)
PluginPath = "C:\Program Files (x86)\MeGUI\tools\avisynth_plugin\"
LoadPlugin(PluginPath+"svpflow1.dll")
LoadPlugin(PluginPath+"svpflow2.dll")
Import(PluginPath+"InterFrame2.avsi")
<input>.ConvertToYV12()
SetMTMode(2)
InterFrame(Cores=4, NewNum=50000, NewDen=1000)
LanczosResize(1920, 1080)

I was pleasantly surprised with the results from this - the video looked much smoother, and the process introduced few artefacts.  With all the video in the same format, we can now import  into WMM and get a nice consistent frame rate in the output.

| February 6th, 2014 | Posted in Software |

Leave a Reply