Linux/*nix Video -> H.264 with Mencoder HOWTO

This is a guide designed to help you convert video to the new H.264 codec. Special attention is paid to keep the video stream compatible with QuickTime, which is something I find sorely lacking in other HOWTO’s. This HOWTO has been updated! The quality of the result is now far superior. Check it out. The following tools are required:

  • faac
  • gpac
  • mpeg4ip
  • x264-svn
  • libdvdread
  • libdvdcss
  • mplayer

Audio


rm audiodump.pcm && mkfifo audiodump.pcm
faac --mpeg-vers 4 -q 100 -P -R 48000 -C 6 -I 5,6 -X audiodump.pcm -o audio.aac & mplayer dvd:// -dvd-device ./temp/VIDEO\_TS/ -aid 128 -vc null -vo null -ao pcm:nowaveheader:fast  -channels 6

Crop Letterboxing


mplayer -vo null -vf cropdetect dvd:// -dvd-device temp/VIDEO\_TS/

This will give you the video dimensions you need to feed to the next command. Replace -vf crop=xxx:xxx:xx:xx in subsequent commands with the output of this! Pass 1


mencoder -nosound -of rawvideo -ovc x264 -vf crop=xxx:xxx:xx:xx,scale,harddup -x264encopts bitrate=1000:frameref=6:analyse=all:me=umh:subme=7:trellis=2:bframes=1:subq=7:brdo:mixed\_refs:weight\_b:bime:no\_fast\_pskip:direct\_pred=auto:mixed\_refs:nr=200:threads=auto:turbo=2:pass=1 -noskip dvd:// -dvd-device file.iso -o /dev/null

Pass 2


mencoder -nosound -of rawvideo -ovc x264 -vf crop=xxx:xxx:xx:xx,scale,harddup -x264encopts bitrate=1000:frameref=6:analyse=all:me=umh:subme=7:trellis=2:bframes=1:subq=7:brdo:mixed\_refs:weight\_b:bime:no\_fast\_pskip:direct\_pred=auto:mixed\_refs:nr=200:threads=auto:pass=2 -noskip dvd:// -dvd-device file.iso -o movie.264

Merge into one file:


mp4creator -rate 23.976 -a movie.264 movie.mp4

MP4Box -add audio.aac movie.mp4

Note the -rate flag passed to mp4creator. This is important. This must match the frame rate of the source material! This method can be used on any type of source media, not just a DVD.

-vf crop needs to be adjusted according to the results given by cropdetect, and the desired final size. Aspect will be preserved within the video stream itself.

The Result:

A high-quality MP4 file with 6 channel surround audio and video stored in anamorphic widescreen. Enjoy!

Samples:

Opening Scenes of Star Wars Episode III - shot 1

Opening Scenes of Star Wars Episode III - shot 2

Opening Scenes of Star Wars Episode III - shot 3

Opening Scenes of Star Wars Episode III - shot 4

Click for larger.