"Never use the second pass encoding again" - Using 'Constant Ratefactor' Instead of Average Bitrate in x264

Okay, I may not be able to completely fulfill that promise, but you will most likely be saved from the second pass and thus a lot of encoding time quite often with this tip. If you don't need the resulting file to e.g. fit on a CD, Constant Ratefactor aka Constant Quality is probably perfect for you. Let me supply you with a quick overview and my quick mencoder script that does the job.

Did you know you can use a roughly vorbis-like quality selection, constant quality crf even in x264? This will save you a lot of encoding time with more or less the same quality. Or maybe not? Well it will look different, but it should be fine. For x264 a range of 18-26 is recommended. The higher the number the lower the bitrate and thus the file size will be.

Nice! Finally I don't need to adjust and calculate bitrates myself anymore! The algorithms really seem to work, because when I now change the codec settings to more efficient ones (e.g. increase the frameref, subq or enable mixed_refs) the quality (SSIM) remained pretty similar, while the bitrate sank.

Some experience with the parameters: The mixed_refs parameter did regularly increase the SSIM for me, but also took a lot more time to encode. Frameref=2 increases the efficiency with little performance impact in my experience. I wonder why it's not the default. Bframes decrease the quality slightly but shave a lot off the bitrate and even increase performance a bit for me.

Let me show you my "quick" example script for mencoder.
#!/bin/bash

CRF=23.5 # 21-26 recommended, the higher the smaller the resulting file, see http://mewiki.project357.com/wiki/X264_Settings#crf

INPUT="$1"; shift
X264OPTS="$1"; shift
OUT=`basename "$INPUT"`"-x264.avi"
echo Encoding $INPUT to $OUT

mencoder="time nice /usr/bin/mencoder -quiet -cache 16384"
enc="$mencoder -ovc x264 -oac copy -x264encopts crf=$CRF:trellis=1:frameref=2:bframes=2:8x8dct:psnr:ssim:$X264OPTS"
# I suggest nr= of no more than 2 or 3 with current x264 from svn.
# something along the lines of -vf
# eq=contrast=20:brightness=2,hue=saturation=1.25 for bleak videos
# denoise3d for noisy and unsharp=l:3x3:1.045 for blurry videos.
# I recommend to try out the result with mplayer first.
# A combination of all may work well, too.

echo $enc -o "$OUT" "$INPUT" $*

[ -f "$OUT" ] && echo File exists && exit 1;

$enc -o "$OUT" "$INPUT" $*

# You can call the script like this:
# script.sh input [x264encopts] [mencoder parameters]
# $ sh menc.sh somefile.mpg "interlaced" "-vf crop=..." or
# $ sh menc.sh somefile.mpg "" "-vf crop=..." or simply
# $ sh menc.sh somefile.mpg
# to get the right crop settings try $ mplayer -vf cropdetect


Feel free to get out the flame thrower - or just make some productive comments.

No comments:

Post a Comment

I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.