UNIX Scripts Bash Tips: transcoding converting video files Part 3
If you’ve read part 2, then you should now be familiar with mencoder.conf and using profiles when converting video files using Mencoder. We now take it a step further with Xvid two pass encoding using a shell script.
I find doing Xvid two pass encoding on the command line a pain, since you need to do the transcoding twice, i.e. once for each pass. It’s even more painful when your computer is over 8 years old like mine is. This is when a simple shell script comes in handy.
Configure Mencoder.conf Xvid two pass encoding
Open up ~/.mplayer/mencoder.conf in a text editor, then paste this in:
[pass1]
vf=scale=-1:-1
passlogfile=pass.log
oac=copy=yes
ffourcc=XVID
ovc=xvid=yes
###by final file size, usually works: either 700 MB or 400 MB
xvidencopts=pass=1:quant_type=mpeg
###by constant bitrate
#xvidencopts=pass=1:bitrate=900:quant_type=mpeg:nogmc=yes:noqpel=yes:threads=2
[pass2]
vf=scale=-1:-1
passlogfile=pass.log
oac=copy=yes
ffourcc=XVID
ovc=xvid=yes
xvidencopts=pass=2:bitrate=-699000:quant_type=mpeg
#xvidencopts=pass=2:bitrate=900:quant_type=mpeg:nogmc=yes:noqpel=yes:threads=2
If you run
mencoder -profile help
You should now see “pass1″ and “pass2″ in your list of encoding profiles.
We’re not going to do any scaling so we’ll keep the original resolution.
vf=scale=-1:-1
During the first pass, we’ll use ” pass.log ” as the name of our log file. This may or may not work, depending upon your MPlayer install.
passlogfile=pass.log
For the audio part, we’ll stick with the original.
oac=copy=yes
The video part will be encoded based on an estimate of the final file size of 700MB. However, we can also do so based on a given constant bitrate.
xvidencopts=pass=2:bitrate=-699000:quant_type=mpeg
Notice that when encoding by file size, a negative value is used. Also, you can pass along some other Xvid options like no GMC nor Qpel, and number of threads – great if you have more than one core/cpu to work with. *There are LOTS of other options available. Just take a look at the Mencoder / MPlayer man page and see.
Script for Xvid 2 Pass Encoding
Now using a script to do Xvid two pass encoding. We put this in a file called ” 2pass.sh ” in the working directory. Be sure to have enough disk space. Alternatively, put it in your $HOME/bin directory – if you’ve made it executable, and “chmod 700″ it.
#!/bin/sh
mencoder “$1″  -profile  pass1 -o  /dev/null &&
sleep  3  &&
mencoder “$1″ -profile  pass2  -o  pass2_”$1″
I put in the ” sleep ” command to give my ancient PC a rest after each run.
Then we run:
sh  2pass.sh  OLD.avi
When it’s finished running, we have two files:
pass.log
pass2_OLD.avi
The second one is the new video. You may want to test the file size setting. There are lots of tools available. Most notable here would be xvid4conf.

July 17th, 2008 at 9:15 pm
If you wanted to run with a constant quality factor in pass1 so as to determine the compressibility of the file, can you take its output and roll the resulting bitrate into pass2 using a script technique or other automated way? If so, can you provide an example? Thanks! If you ask me, the next MEncoder version should built in this functionality.
July 24th, 2008 at 3:26 pm
Hi David. Sorry for not getting back to you sooner.
In determining the compressibility of the original video, I assume you want to know how small you can make the final transcoded video file. I believe you’d have to use something like xvid4conf. (It’s available as a Debian package.) I think they’re often referred to as “bitrate calculators”.
What you’re describing in terms of “output” actually sounds like the Xvid passlog file. My understanding is that, the first pass is used to determine how much the file can be compressed, amongst other things of course. All the data is then dumped into the passlog file. The info. in the passlog file is then used in the second pass.
As an experiment, edit your script so that the transcoding from the first pass is not dumped to /dev/null :
mencoder “$1″ -profile pass1 -o pass1.avi
Run the script in near full screen and take a quick look at mencoder’s stdout. Hit cntrl + c once it actually starts the transcoding. You should notice a line where it says that it ignores bitrate settings in the FIRST pass encoded video file.
Anyway, if you’re goal is to shrink the file, but still make it look AND sound okay, you’ll need to investigate all the other Mencoder options in the man page. There’s a lot.
For starters, you can scale down the resolution, say from 720 pixels wide to 640 pixels wide or less. I think standard def TV is 480 px wide. Also, if the audio part of the video file is in AC3 or PCM, encode with libmp3lame.
However, I find using the fixed quantizer option the easiest and fastest.