mEncoder is a great video encoding tool that comes with mplayer.
I have been playing about with mEncoder and have some options I am reasonably happy with.
I have been experimenting with both the Xvid codec and mEncoder.
These options are for a high quality DVD rip, which will fit on 2 cds.
Below are the steps I have taken to rip a dvd and encode it using the mEncoder and the XviD codec.
(Optional)
Rip vobs to Hard disk
CODE
mplayer dvd:// -dumpstream -dumpfile dvd.vob
CROPPING
Run mplayer with the cropdetect option this gives us the values needed for cropping the movie.
The command below will seek to 360 seconds into the film and stop after 15 seconds so we are seeing the crop values for the movie.
CODE
mplayer dvd:// -vf cropdetect -ss 360 -endpos 15
A list of crop values for the movie are printed to the terminal so we can then pass to mEncoder.
CODE
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
[CROP] Crop area: X: 4..714 Y: 74..508 (-vf crop=704:432:8:76).0
The important values are the *:*:*:* values in brackets
CODE
704:432:8:76
These will change per dvd so you need to run this with every dvd.
Make a note of these values.
XviD Two Pass Encoding
Pass One
CODE
mencoder -v "$input" -sws 2 -vf crop="$crop",scale="$scale" -ovc xvid -xvidencopts bitrate="$bitrate" \
:pass=1:trellis:quant_type=mpeg:autoaspect:qpel:chroma_me:max_bframes=2:hq_ac:vhq=4 \
:bvhq=1:me_quality=6:turbo:min_iquant=1:max_iquant=4:min_pquant=1:max_pquant=6:min_bquant=2:max_bquant=9: \
-alang en -oac mp3lame -lameopts abr:br=128:vol=9 -force-avi-aspect "$aspect" -o /dev/null
:pass=1:trellis:quant_type=mpeg:autoaspect:qpel:chroma_me:max_bframes=2:hq_ac:vhq=4 \
:bvhq=1:me_quality=6:turbo:min_iquant=1:max_iquant=4:min_pquant=1:max_pquant=6:min_bquant=2:max_bquant=9: \
-alang en -oac mp3lame -lameopts abr:br=128:vol=9 -force-avi-aspect "$aspect" -o /dev/null
Pass Two
CODE
mencoder -v "$input" -sws 2 -vf crop="$crop",scale="$scale" -ovc xvid -xvidencopts bitrate="$bitrate" \
:pass=2:trellis:quant_type=mpeg:autoaspect:qpel:chroma_me:max_bframes=2:hq_ac:vhq=4:bvhq=1: \
me_quality=6:min_iquant=1:max_iquant=4:min_pquant=1:max_pquant=6:min_bquant=2:max_bquant=9: \
-alang en -oac mp3lame -lameopts abr:br=128:vol=9 -force-avi-aspect "$aspect" -o "$output".avi \
:pass=2:trellis:quant_type=mpeg:autoaspect:qpel:chroma_me:max_bframes=2:hq_ac:vhq=4:bvhq=1: \
me_quality=6:min_iquant=1:max_iquant=4:min_pquant=1:max_pquant=6:min_bquant=2:max_bquant=9: \
-alang en -oac mp3lame -lameopts abr:br=128:vol=9 -force-avi-aspect "$aspect" -o "$output".avi \
I will try to explain some of these options, any that I have left out below but included in the above lines are options that I am still working on/reading about
and could not explain to you adequately or accurately enough yet.
lsdvd is a nice little tool to list the contents of a dvd from the command line. It will be in most linux/unix repositories.
CODE
lsdvd
INPUT
mEncoder to use the largest title on the dvd
CODE
dvd://
or
mEncoder uses title one on the dvd.
CODE
dvd://1
you may use a file instead
CODE
movie.mpg
CROPPING AND SCALING
This is our crop values taken from the cropdetect output above.
CODE
crop=704:432:8:76
This is the resolution, I scale to around 704:304 for 16/9 or 640x480 for 4/3
CODE
scale=704:304
BITRATE OR FINAL FILE SIZE
If a negative value is given in the bitrate option then an estimated final file size is used, if a positive value is given then bitrate is used.
Final file size, here it is 1350 MB (notice the negative value)
CODE
bitrate=-1350000
Bitrate to use
CODE
bitrate=1200
VIDEO FILTERS AND XVID OPTIONS
Uses the xvid codec and the xvid options follow
CODE
-ovc xvid -xvidencopts
This controls how precise the motion estimation will be (higher is better)
CODE
me_quality=6
This option optimizes the quantization process.
CODE
trellis
Tells mencoder the pass which pass it is on, 3 passes and higher can be used but the noticeable visual difference above 3 pass is minimal.
Some video filters and xvid options are included in the 1st pass but not used (see turbo)
CODE
pass=1
CODE
pass=2
I have use -sws 2 bicubic, can be better at lower bitrates. I have experimented with lanczos scaling but am not as happy as I am with the bicubic.
(I expect my options just need tweaking)
CODE
-sws 2
This sets the quantizer to be used, I have used mpeg which preserves more detail at higher bitrates.
It is recommended to use h263 which is smoother at lower bitrates and I have used this on 700mb dvd rips
CODE
quant_type=mpeg
This option improves motion estimation by using quarter pixel precision instead of half pixel precision.
This is a cpu intensive option and is not suported by all stand alone divx players
CODE
qpel
This option improves motion estimation by looking at colour information and improved visual quality for me.
CODE
chroma_me:
This option disables some of the more cpu hungry options on the first pass, speeding thing up a bit.
CODE
turbo:
You can use more than 2 but I have had problems playing in stand alone divx players.
CODE
max_bframes=2:
From the man page
"vhq tries to find a motion vector that minimizes the difference between the reference frame and the encoded frame
With this setting activated, Xvid will also use the frequency domain (DCT) to search for a motion vector that minimizes not only the spatial difference but also the encoding length of the block"
Clever searching to me
CODE
vhq=4:
This does the same as above but on b frames
CODE
bvhq=1:
This preserves the aspect of the dvd and writes the information to the header, only mplayer takes any notice of this.
To have windows media player,vlc or xine play our movie at the correct aspect ratio we need to scale (as above) to an appropriate 16/9 or 4/3 resolution.
CODE
autoaspect
To force a specific aspect and can be used with -ovc copy to change aspect.
CODE
force-avi-aspect 16/9
LANGUAGE
This option specifies the language code to use for the audio (english) otherwise we may end up with the directors commentary.
CODE
-alang en
AUDIO/LAME OPTIONS
Encodes the audio with the given audio codec (mp3)
CODE
-oac mp3lame
Use an average bitrate of 128 for the audio track and boosts the volume.
CODE
-lameopts abr:br=128:vol=9
OUTPUTFILE 1st PASS
This sends the output of the first pass to /dev/null A file (divx2pass.log) is created on the first pass containing information
about the input video file (movements colour information etc), this is read on the 2nd pass.
CODE
-o /dev/null
OUTPUTFILE 2nd PASS
This is the output file to use
CODE
-o moviename.avi
I have added all of this into a simple shell script that automates all of this,
I have tried to leave it very simple so anyone can understand it and edit it to suit their own needs.
NOTE For compatibility with stand alone DivX players I would drop these options qpel and change max_bframes=1 and change vhq=1 and do not use gmc.
Usage
Extract script
CODE
gzip -d dvd2xvid.gz
add execute permission
CODE
chmod +x dvd2xvid
run script
CODE
./dvd2xvid
You will be asked a set of questions
INPUT
input device = enter the file or device to use as input
For the largest title on the dvd.
CODE
dvd://
or the 3rd title on the dvd.
CODE
dvd://3
or a filename
CODE
/home/synack/movies/movie.vob
CODE
/home/synack/movies/movie.mpg
OUTPUT
output file = enter the desired output filename without an extension.
CODE
New_Movie.Dakside_RG.XviD
BITRATE OR FINAL FILE SIZE
bitrate or final filesize = as explained above, using a negative value specifies an approximate file size.
adding a positive value uses the specified bitrate
Bitrate
CODE
1200
or
Approx Final File Size
CODE
-1250000
ASPECT RATIO
aspect ratio = enter 16/9 or 4/3
CODE
16/9
CROPPING AND SCALING
resolution = enter scaled resolution seperated by colons.
CODE
704:304
cropdetect will now be run, enter the values in the brackets seperated by colons.
CODE
688:432:18:68
Sit back and wait
For a single cd rip I would start with something like,
1st pass
CODE
mencoder dvd:// -vf crop=***.***.**.**,scale=704:304 -ovc xvid -xvidencopts \
bitrate=-600000:me_quality=6:pass=1:hq_ac:trellis:quant_type=h263:chroma_me:turbo: \
max_bframes=1:vhq=4:bvhq=1:autoaspect:min_iquant=1:max_iquant=31:min_pquant=1:max_pquant=31:min_bquant=1:max_bquant=31 \
-alang en -oac mp3lame -lameopts abr:br=96:vol=9 -o /dev/null
bitrate=-600000:me_quality=6:pass=1:hq_ac:trellis:quant_type=h263:chroma_me:turbo: \
max_bframes=1:vhq=4:bvhq=1:autoaspect:min_iquant=1:max_iquant=31:min_pquant=1:max_pquant=31:min_bquant=1:max_bquant=31 \
-alang en -oac mp3lame -lameopts abr:br=96:vol=9 -o /dev/null
2nd pass
CODE
mencoder dvd:// -aspect 16/9 -vf crop=***:***:**.**,scale=704:304 -ovc xvid -xvidencopts \
bitrate=-600000:me_quality=6:pass=1:hq_ac:trellis:quant_type=h263:chroma_me:max_bframes=1:hq=4:bvhq=1:autoaspect \
:min_iquant=1:max_iquant=31:min_pquant=1:max_pquant=31:min_bquant=1:max_bquant=31 -alang en -oac mp3lame -lameopts \
abr:br=96:vol=9 -o MOVIE_NAME.avi \
bitrate=-600000:me_quality=6:pass=1:hq_ac:trellis:quant_type=h263:chroma_me:max_bframes=1:hq=4:bvhq=1:autoaspect \
:min_iquant=1:max_iquant=31:min_pquant=1:max_pquant=31:min_bquant=1:max_bquant=31 -alang en -oac mp3lame -lameopts \
abr:br=96:vol=9 -o MOVIE_NAME.avi \
RANDOM MENCODER STUFF
OTHER VIDEO FILTERS
Deinterlace
for interlaced content,
I have used the -kerndeint and -lavcdeint on a dvb-t source and have found lavcdeint option a lot smoother.
CODE
-kerndeint
-lavcdeint
-pp=lb
-lavcdeint
-pp=lb
noise filter (good for grainy source)
CODE
hqdn3d=2:1:2.
Optimize mencoder for cartoons.
CODE
cartoon
Global Motion Compensation for help with movies with lots of panning, zooming and rotating images.
CODE
gmc
MENCODER COMMAND LINES
Joining 2 files
CODE
mencoder -ovc copy -oac copy -o Joinedfilm.avi filmCD1.avi filmCD2.avi
Splitting a large file
CODE
mencoder large_file.avi -ss 0 -endpos 700mb -ovc copy -oac copy -o cut_file_Part1.avi
CODE
mencoder large_file.avi -ss 700mb -ovc copy -oac copy -o cut_file_Part2.avi
Fixing broken index and interleaving
CODE
mencoder -idx broken_file.avi -ovc copy -oac copy -o fixed_file.avi
Watch movie in ascii art
CODE
mplayer -vo aa:width=***:height=*** movie.avi
TESTING
These options are good for test encoding/playing part of a movie.
seek to 10 minutes into the input file
CODE
-ss 10:00
seek to 120 seconds into the input file
CODE
-ss 120
seek to 100mb into the film
CODE
-ss 100mb
end after 10 minutes
CODE
-endpos 10:00
end after 120 seconds
CODE
-endpos 120
end after 100 MB
CODE
-endpos 100mb
Verbose output
CODE
mencoder -v
CODE
mplayer -v
Another cool thing about this tool is that some of the options/switches are interchangable
for example
Will play track 1 on the dvd at a scaled resolution of 704x304, staring after 10 minutes and ending after 120 seconds.
CODE
mplayer dvd://1 -vf scale=704:304 -ss 10:00 -endpos 120
Will encode track 1 on the dvd at a scaled resolution of 704:304, staring after 10 minutes and ending after 120 seconds.
CODE
mencoder dvd://1 -vf scale=704:304 -ss 10:00 -endpos 120
