Batch Transcoding MP4 Videos to MPEG-2 Format

July 12, 2009

I was looking for a way to transcode a bunch of medium quality Mp4 videos into good quality MPEG-2 formatted video so I could then make them into a DVD.

I had problems with my favourite transcoder (MediaCoder) and also a bunch of other transcoders that I unsuccessfully tried.

[Note: I use VLC media player to play my media files. Since VLC has no requirement for codecs to be installed on one’s computer, it turns out I didn’t notice that some of the codecs on my computer were corrupt. After working around the problem with the solution I discuss below, I found that I couldn’t even open the MP4 files in Nero ShowTime – I would get a StackHash_1703 error when it crashed.  The root cause of the problem that prevented me from using the transcoders I tried was a corruption of codecs from my installation of Nero. The root cause of the problem has been solved by applying an update to Nero, and now I can once again use my favourite MediaCoder for everything…]

However before I solved that root cause problem, the following is what I did.

I found that AviDemux 1.4 successfully converted the videos to MPEG-2, but version 1.5 would give an error. But, even after using version 1.4 to do the conversion, the MPEG-2 videos were not compliant with DVD standards. From here though I could then use MediaCoder to add the borders I wanted, increase the brightness, increase the volume, deinterlace the video and convert it to 25 frames per second for PAL. But, I wasn’t happy about doing double encoding and so I wanted a better solution.

And so we finally come to the alternative solution that worked very well for me and forced me to learn some lower-level details about transcoding. This solution required the following tools:

  • ffdshow – a DirectShow and VFW codec for decoding/encoding many video and audio formats
  • AviSynth 2.5.8 – a powerful tool for video post-production that provides ways of editing and processing videos, and the Decomb plugin
  • QuEnc 0.72 – a tool that uses a built in FFMPEG library to encode from an AviSynth Script file to a raw MPEG-1/MPEG-2(VCD,SVCD,DVD) video stream

While learning the AviSynth syntax, I found it very useful to open the AviSynth script in VirtualDubMod so I could immediately see and confirm the effects of the script on the video.

I created the following AviSynth user-defined function, and put it into a file named ConvertCommon.avsi:

LoadPlugin(“C:\Program Files (x86)\AviSynth 2.5\plugins\decomb.dll”)

function Convert(string mp4Filename)

FieldDeinterlace(show=false)        #DECOMB


Tweak(bright=10, cont=1.1)

AddBorders(0, 84, 0, 84, $000000)

return AmplifydB(3)

This user-defined function is specific to the video processing I wanted to perform on all the MP4 files in my batch. It resizes the video to the size and aspect ratio that I carefully pre-determined, converts the colour to YV12, deinterlaces the video, converts it to 25 frames per second, tweaks the brightness and contrast, adds letterboxing to make the final video size 720×576 for PAL DVD compliance, and amplifies the volume by 3 decibels.

I also stumbled on the following AVS2DVD Batch Script which automates the use of QuEnc on all the AviSynth scripts in a particular folder, and also goes even further to create DVD-Video files. This provided me with a good starting point for automating the conversion of my MP4 files. There were however three issues I has with that script (for my purposes):

  1. I didn’t want to manually create AviSynth Script files for every MP4 file in the batch I wanted to process – because I wanted the exact same post-processing to occur to all the videos.
  2. I needed to edit the resulting MPEG-2 videos so I didn’t want to automatically convert them to DVD-Video files.
  3. It was using the command-line syntax of an older version of QuEnc, which errored in the new version.

Below is the modified batch script which loops through all of the MP4 files in a specified folder, creates a temporary AviSynth script which calls my user-defined function, and passes the temporary script to QuEnc with PAL DVD-compliant settings.

@SET MP4SourceFolder=F:\Videos\Mp4
@SET MPG2OutputFolder=F:\Videos\ConvertedMpeg2

@SET quenc=F:\Videos\QuEnc

@SET QBitrate=3000
@SET QMaxBitrate=9000
@REM Set to the max bitrate minus the sum of all the audio track bitrates. For DVD, 9800-xxx
@SET QPasses=2
@SET QGOPSize=15
@SET QMaxBFrames=2
@SET QAspectRatio=1:1
@SET QAudioCodec=AC3
@SET QAudioBitrate=192
@REM AC3 @ 192/256 kbps for stereo and 384/448 kbps for 5.1ch is recommended
@REM Leave QHQ blank if you want High Quality turned on, otherwise set it to no
@REM Leave QVBR blank if you want VBR turned on, otherwise set it to no
@REM Leave QSCENE blank if you want Scene Detection turned on, otherwise set it to no
@REM Leave QTRELL blank if you want Trellis Quant turned on, otherwise set it to no
@REM Leave QCGOP blank if you want to use Closed GOP, otherwise set it to no
@SET QInterlace=no
@REM Leave QInterlace blank if you want to use Interlaced Encoding, otherwise set it to no
@SET QExtreme=no
@REM Leave QExtreme blank if you want to use Extreme encoding, otherwise set it to no
@SET QCMatrix=no
@SET QCMatrixFile=
@REM If you wish to use a Custom Matrix, leave QCMatrix blank and add your file path to QCMatrixFile,
@REM otherwise set QCMatrix to no, and leave QCMatrixFile blank.

@SET QCMD=-auto -close
@REM Use for additional parameters such as Silent, Auto, Close and Shutdown

FOR /F “usebackq delims==” %%I IN (`dir /b “%MP4SourceFolder%\*.mp4″`) DO (
@REM Make AVS file
@echo Import^(“ConvertCommon.avsi”^) 1> temp.avs
@echo Convert^(“%MP4SourceFolder%\%%~nxI”^) 1>> temp.avs

@REM Compress AVS file to MPEG2 using QuENC
“%quenc%\quenc.exe” -i “.\temp.avs” -o “%MPG2OutputFolder%\%%~nI.mpg” -b %QBitrate% -maxbitrate %QMaxBitrate% -dc %QDC% -%QPasses% -%QHQ%hq -%QVBR%vbr -%QSCENE%scene -%QTRELL%trell -%QCGOP%cgop -%QInterlace%interlaced -%QExtreme%extreme -gopsize %QGOPSize% -maxbframes %QMaxBFrames% -%QCMatrix%cmatrix %QCMatrixFile% -aspectratio %QAspectRatio% -mpeg%QMPEGType%mux %QMPEGMuxProfile% -audiocodec %QAudioCodec% -audiobitrate %QAudioBitrate% %QCMD%


del temp.avs

And after running the batch script over a few days, my transcoding is now complete!