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.

3 thoughts on “Linux/*nix Video -> H.264 with Mencoder HOWTO

  1. I like your guide a lot but I can’t use your first command as-is. It tries to open the fifo too soon so I had to modify it as such:

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

  2. Reservoir Dogs DVD is not “pure progressive” from mencoder help–> About the performance of pullup: It is safe to use pullup (along with softskip ) on progressive video, and is usually a good idea unless the source has been definitively verified to be entirely progressive. The performace loss is small for most cases. On a bare-minimum encode, pullup causes MEncoder to be 50% slower. Adding sound processing and advanced lavcopts overshadows that difference, bringing the performance decrease of using pullup down to 2%

Comments are closed.