Re-encode all videos in a directory using mencoder

I had ~50 videos in a directory that were encoded using on2 VP7 which was cauing some problems on some of my home media players. Heres a quick one line command that will re-encode all files suffixed with .avi to use xvid  🙂 Once the script has completed all the re-encodes will be suffixed with .new.avi. This isnt an example in uber quality re-encodes it was just good enough for me.

To re-encode a directory of avi files to use xvid and copy the audio:

for x in *.avi ; do mencoder "$x" -ovc xvid -oac copy -xvidencopts fixed_quant=4 -o "`echo $x | sed 's/\(.*\)\..*/\1/'`.new.avi" ; done & > /dev/null


After that I had a directory of WMV files (720p) I wanted to re-encode to h.264/MKV to do this in one long command would have been quite messy so I made a little bash script. You can edit the script to re-encode any supported file type.

To re-encode wmv to x264:

# Execute this script in a directory to re-encode all files to x264 with mp3 in a mkv
# There are no arguments for the script!
# Edit below for your own needs

# User to mail results to
# Files suffixed with this to be re-encoded

# Shouldnt need to edit below here
IFS=$(echo -en "\n\b")
DIR="$( cd "$( dirname "$0" )" && pwd )"
echo "x264 re-encoder" >> encodelog
echo "Re-encoding all $suffix files in $DIR"  >> encodelog
echo "x264 re-encoder"
for x in *.$suffix
        mencoder $x -ofps 30000/1001 -ovc x264 -x264encopts subq=1:frameref=1:pass=1:threads=2 -nosound -of lavf -o `echo $x | sed 's/\(.*\)\..*/\1/'`.mkv
        echo "*** Completed First Pass ***".
        mencoder $x -ofps 30000/1001 -ovc x264 -x264encopts subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:weight_b:bitrate=3500:pass=2:threads=2 -oac mp3lame -lameopts vbr=3:br=192 -of lavf -o `echo $x | sed 's/\(.*\)\..*/\1/'`.mkv
        echo "*** Completed Second Pass ***"
        echo "Now tidying up"
        echo "$x re-encoded in $((end - start)) secs." >> encodelog
        rm divx2pass.log
        rm divx2pass.log.mbtree

echo "Emailing $mailuser"
cat encodelog|mail -s "Re-encode job results" $mailuser

exit 0

The script will search for all files suffixed with wmv (can be changed) and do a two pass re-encode with h264 using lame for the sound (192kb/s ABR). Since I ran this on quite big number of files I wanted to be notified when it was done rather than checking. It will mail the user defined and list all the files re-encoded with the number of seconds taken for each one.

m00nie 🙂

Leave a Reply