Recoding of videos to H.264 using mencoder

INPUT=video.avi
OUTPUT=video_h264.avi

# a few possible video filters
VF="-vf crop=712:568:4:4,pp=fd"
VF="-vf crop=688:568:18:4,pp=fd"
VF="-vf spp,scale"
VF="-sws 9 -vf scale=720:-11,expand=:-48::0"
VF="-vf expand=0:-32::0"
VF=

# audio filters
AF="-af stats,volume=6 -oac lavc -lavcopts acodec=ac3 -aid 128"
AF="-af stats,volume -oac pcm -aid 129"
AF="-aid 128 -af volume=20:0"
AF=

################
# H.264 settings
BITRATE=1200
ENCOPTS="bitrate=$BITRATE:threads=6:8x8dct:cabac:deblock:frameref=3:bframes=2:b_adapt:weight_b:chroma_me"
PASS1="-ovc x264 -x264encopts pass=1:$ENCOPTS"
PASS2="-ovc x264 -x264encopts pass=3:$ENCOPTS"
mencoder "$INPUT" \
         -o "$OUTPUT" \
         -nosub \
         $VF $PASS1 \
         -nosound \
         ;

mencoder "$INPUT" \
         -o "$OUTPUT" \
         -nosub \
         $VF $PASS2 \
         -nosound \
         ;

# extract the (AC3) audio track
mencoder -of rawaudio -ovc copy $VF $AF $INPUT -o en.ac3

Notes

  • I do not really understand the encoding options ENCOPTS. I’ve picked them up from various other posts and it looks to me, I can get reasonable results.
  • Using the option -nosound may desynchronize the video from the audio stream, the the video output-framerate is wrong.Watch out for warnings “duplicate frame” and check the length of the video stream and audio track!
  • For sharpening, use: -vf unsharp=l5x5:1.0:c5x5:1.0, but test the result!