Video Transcoding with Free Tools
December 4th, 2009Disclaimer: This is partly for my benefit in case I lose my batch files and have to re-derive these techniques at some point. As such, the language is pretty techy, but this might be handy for other people.
These days we need to format-shift content a million different ways. Xbox, Playstation, iPod, etc all require different formats to get the most out of the platform. The Xbox 360 can’t deal with modern container formats like .mkv and .mp4, while the Playstation and iPod don’t implement all the codec features available to .mp4 files. There are a number of specialised tools available on the ‘net for around the US$20 that can convert anything you throw at it to a particular format, but none of them give you complete control. A program I won’t name for my iPod decided to file 16×9 video to my iPod screen by chopping the sides of the image off. That’s not what I wanted!! Fortunately, open source tools I originally found in linux land can do everything I want at no charge and switch to a fresh Windows 7 install proved a good time to throw out my old way of doing things. This was also inspired by Ashton Mills’ final X-Ray in issue 104 of Atomic on transcoding (pick it up, he explains things better than I can!). Before we start, remember to only tinker with content you have the rights to and everything from here on works equally well in Windows and Linux.
I entered this with two needs, firstly to archive videos into a format that doesn’t take up too much space and secondly to have a robust method of shifting content onto my iPod. I have had a fair bit of experience with the xvid and divx video codecs in the past, but the quality isn’t so good at smaller file sizes (say, 1-1.5GB for 100 minutes of video). Fortunately, we have the x264 codec which is a new generation codec and the open source version of the h264 standard. It also just so happens to be the codec that iPods and the like expect in the .mp4 container! Another huge reason to use x264 is that it multi-threads very well, so can make the best use of my quad core processor. I find older codecs struggle to load up more than 2 of my processors. The downside of x264 is that it is not really supported in the venerable .avi container, so don’t expect .avi files with a x264 video stream to work 100% of the time unless you are using a smart media player.
The other tools we need for the job is to ensure all the codecs we need are installed (grab missing ones here at free-codecs.com) as well as mencoder and mp4creator. I already us MPlayer as my media player in Windows and Linux, so using the encoder that comes with it seems logical. Windows binaries of mplayer/mencoder are available here and in linux can grabbed from your package manager (in Fedora/Red Hat yum install mplayer mplayer-gui mplayer-doc mencoder, similar apt-get command in Ubuntu or Debian). The final puzzle piece of mp4creator is available from Sourceforge. Note that all these are command line tools, so it is a good idea to add the directories where you installed mencoder and mp4creator to your Windows PATH or you will need to type out the full command path EVERY TIME in your batch files. All the commands I describe from here on can be just added to a batch file or shell script and left to run.
So lets look at my basic archiving sequence step by step, then I will add some sample batch files for other tasks at the end. We will use 2-pass encoding for very good reasons.
mencoder -sws 9 -vf scale=768:432,hqdn3d=4:3:6,
unsharp=l7×5:1,pp=ha:128:7/va/dr/al,harddup ovc x264
-x264encopts pass=1:turbo:bitrate=600:bframes=1:
me=umh:partitions=all:trellis=1:subq=6:frameref=5:
direct_pred=auto:keyint=120:threads=auto -oac faac
-faacopts br=192:mpeg=4:object=2 -channels 2
-srate 48000 inputfile -o outputfile.avi
OK, so this command (all on one line!!!) is the first pass of encoding process. A lot of the options I copied from Ashton’s guide in Atomic and only changed stuff that didn’t work for me. Important things to note:
- scale=768:432 - This at the start of the video filter (vf) chain scales the video to the width x height you want. This value is for a 16×9 movie at DVD resolution, use scale=720:576 for a 4×3 movie. For reference, 720p is scale=1280:720.
- hqdn3d - this is the high quality denoising filter. This can help encoding a lot by cleaning out the noise and making the frames more similar
- harddup - this option at the end of vf chain forces duplicate frames to be copied. This may not be needed for every container format and codec but without this option duplicate frames can be dropped, which saves file space but causes the video and audio to drift out of sync. Not good!
- pass=1 - this tells the codec to run through the video and write out a log file.
- turbo - I don’t care about video quality on the first pass as all I want is the log file, so the codec can run in turbo mode.
- threads=auto - Tells the codec to use as many CPUs as it can find. If you want to restrict it, you can use a number instead of ‘auto’.
- inputfile - This can be any video file you like, even a DVD title. If you want title 1 from the DVD in D:, the inputfile would be -dvd-device D: dvd://1.
Other options we could have used is -aid to specify the ID number of the audio track we want, -sid for the subtitle track, -af volume=15 to add 15db to the audio track. If instead of the 2 channel AAC audio we wanted to keep source audio (like the surround sound track from a DVD), we just replace everything from -oac to inputfile with -oac copy. If the source video is interlaces (only every second line of the video is refreshed at a time), we can deinterlace it with yadif (Yet Another DeInterlacing Filter) at the start of the vf chain.
mencoder -sws 9 -vf scale=768:432,hqdn3d=4:3:6,
unsharp=l7×5:1,pp=ha:128:7/va/dr/al,harddup ovc x264
-x264encopts pass=2:bitrate=1000:bframes=1:
me=umh:partitions=all:trellis=1:subq=6:frameref=5:
direct_pred=auto:keyint=120:threads=auto -oac faac
-faacopts br=192:mpeg=4:object=2 -channels 2
-srate 48000 inputfile -o outputfile.avi
For the second pass, all we do is drop the turbo and increase the bitrate, as we actually care about the output now. We also need to set pass=2, so the codec knows to read from the logfile we created in the first pass. This leaves us with a .avi file that has x264 video and aac audio. To convert to mp4, we first need to extract the video and audio streams with mplayer:
mplayer outputfile.avi -dumpaudio -dumpfile outputfile.aac
mplayer outputfile.avi -dumpvideo -dumpfile outputfile.h264
And mux them back together with mp4creator:
mp4creator -create=outputfile.aac outputfile.mp4
mp4creator -create=outputfile.h264 -rate=25.0 outputfile.mp4
mp4creator -optimize outputfile.mp4
The -rate argument to mp4creator is the framerate, PAL content runs at 25fps.
At the end of all this, we have a roughly 1.2-1.5GB file which gives a good quality archive of video. Using the same technique, we can get a bunch of different formats. My most common first passes are below:
iPod - note that iPods are superduper picky about your x264 encoding options. The -ofps is the output fps (iPods expect 30fps) and the format=ipod will take care of the messiest configuration. We output an mp4 directly in this command, no post-processing.
mencoder -ofps 30 -sws 9 -of lavf -lavfopts
format=ipod -vf scale=-10:240,dsize=320:240:0,
harddup -ovc x264 -x264encopts bitrate=768:
vbv_maxrate=1500:vbv_bufsize=2000:nocabac:me=hex:
subq=4:frameref=2:trellis=1:level_idc=30:global_header:
threads=auto:pass=1 -oac faac -faacopts
mpeg=4:object=2:br=160:raw -channels 2 -srate 48000
inputfile -o outputfile.mp4
Xvid - Does essentially the same as the original command, but as an xvid .avi file with MP3 audio for devices that can only read that. We only need the output from the second pass, no post-processing. As this is the xvid codec, most of the options are different to the x264 command.
mencoder -sws 9 -vf scale=768:432,hqdn3d=4:3:6,
unsharp=l7×5:1,pp=ha:128:7/va/dr/al -ovc xvid
-xvidencopts bvhq=1:vhq=2:chroma_opt:quant_type=mpeg:
turbo:pass=1 -oac mp3lame -lameopts vbr=3
inputfile -o outputfile.avi
Have a play around until you find what works. Every source is different, so keep batch files for common tasks that you can customise and optimise for the current task.
Notes:
mplayer/mencoder docs: http://www.mplayerhq.hu/DOCS/HTML/en/index.html
mencoder man page: http://linux.die.net/man/1/mencoder




