<HTML>
<BODY>
<H1>TODO/TOC</H1>
<UL>
<LI>deinterlacing</LI>
<LI>Quality check -^</LI>
<LI>3.pass Final encoding</LI>
<LI>videorate/etc are preparations <-> encoding opts</LI>
<LI>b&w: check pass 1, vqmin, vqmax in 2-pass</LI>
<LI>3rd pass</LI>
<LI>b frame section</LI>
<LI>DVD -> (S)VCD / DVD</LI>
<LI>Add more documentation about what to clip when you have letterboxed material</LI>
<LI>Add information about using DivX5 despite its inferior quality because a lot of people want to use it :)</LI>
<LI>Add chapter about including multiple audio tracks in an AVI</LI>
<LI>http://www.mplayerhq.hu/pipermail/mplayer-users/2002-February/011057.html</LI>
<LI>ofps/fps, 3:2, etc issue</LI>
<LI>-info</LI>
<LI>lavcopts test mail</LI>
<LI>delay</LI>
<LI>cbr audio (lame) may cause problems in win32</LI>
<LI>subpos etc<LI>
Tip: u can try to use constant quantizer mode for pass1 (vqscale=<quantizer>)
for huffyuv:
        pass 1 saves statistics
pass 2 encodes with a optimal huffman table based upon the pass 1 stats
</UL>
<H1>Detailed DVD Ripping Guide using MEncoder and libavcodec (mpeg4)</H1>
<H2>1. Introduction</H2>
<H3>1.1 About this guide</H3>
<H3>1.2 Dependencies</H3>
<UL>
<LI>mplayer, mencoder ;)</LI>
<LI>libmpdvdkit2 (dvd input)</LI>
<LI>libavcodec (mpeg4 encoding)</LI>
<LI>lame (mp3lame encoding)</LI>
<LI>libfreetype or normal font support (subtitle rendering)</LI>
<LI>subrip (Vobsub to mpsub conversion)</LI>
</UL>
<H2>2. Documentation</H2>
<H3>2.1 Links</H3>
<UL>
<LI>encoding.html</LI>
<LI>tech/encoding-tips.txt</LI>
<LI>tech/libavc-options.txt</LI>
<LI>man page of mplayer</LI>
<LI>cd-dvd.html</LI>
<LI>DOCS/</LI>
<LI>doom9<BR>
The best place on the 'net for DVD ripping and conversion is
<a href="http://www.doom9.org/" target="_blank">doom9</a>. Although it
focuses on Windows it still provides a lot of insight on DVDs, codecs in
general. Also the forum is quite excellent. If your really interested
in video processing then this is the place for you.</LI>
<LI>transcode: <a href="http://www.theorie.physik.uni-goettingen.de/~ostreich/transcode/"
target="_blank">http://www.theorie.physik.uni-goettingen.de/~ostreich/transcode/
</a></LI>
<LI>dvd::rip (GUI for transcode): <a href="http://www.exit1.org/dvdrip/"
target="_blank">http://www.exit1.org/dvdrip/</a></LI>
<LI>some Perl scripts for transcode:
<a href="http://www9.informatik.uni-erlangen.de/~Vogelgsang/bp/tctools.html"
target="_blank">http://www9.informatik.uni-erlangen.de/~Vogelgsang/bp/tctools.html
</a></LI>
<LI>ffmpeg (the home of <em>libavcodec</em>):
<a href="http://ffmpeg.sourceforge.net/" target="_blank">
http://ffmpeg.sourceforge.net/</a></LI>
<LI>kmencoder</LI>
<LI><A HREF="http://www.virtualdub.org/">VirtualDub</A></LI>
<LI><A HREF="http://fixounet.free.fr/avidemux/">avidemux</A></LI>
</UL>
<H3>2.2 LAVC vs. Codecs</H3>
<UL>
<LI>codecs.html</LI>
<LI>codec-status.html (web)/LI>
<LI>comparison</LI>
</UL>
<H3>2.3 About (MPEG4) Encoding</H3>
<H4>2.3.1 Interlaced video</H4>
<P>What you see on television are not 25 frames per second. In fact you see
50 half-frames per second (I'm talking about PAL here - NTSC has 29,97 fps
or nearly 60 half-frames per second). One frame contains the odd rows, one
frame contains the even rows. That way you'll get 25 fps. The problem is
that those half frames are taken from different times. If you have fast
horizontal movement you will then see that an object (or person) is at one
position for the odd rows and at another for the even rows. These are
artifacts you don't want to have in your final video. With the appropriate
filters you can eliminate the effect of interlacing (called
de-interlacing).</P>
<P>See <A HREF="www.100fps.com">www.100fps.com</A> for more details.</P>
<H4>2.3.2 Aspect ratios, Scaling, cropping</H4>
<P>DVDs are always encoded at 720x576 (5:4) although the picture will be
scaled at playback. Common video formats are 4:3 = 1.33 for TV, 16:9 = 1.78
for normal cinema movies and 2.35:1 for Cinemascope. The correct aspect flag
is set in the header of MPEG2. Unfortunately MPlayer and MEncoder are
(almost) the only players/encoders that are capable to set the aspect flag
in AVI/DivX files. Of course it is possible to create a correct aspect
ratio with black bars or with scaling. But black bars are just evil and
scaling always decreases the quality. To set the aspect ratio in
the header should be the preferred methode.</P>
<P>About <B>Letterboxing</B>:<BR>
Perhaps you've noticed that video is sometimes surrounded by black bars
prior to encoding. This process is called letterboxing. It can help to
maintain the correct aspect ratios for some players. Others use the black
bars for displaying subtitles. The disadvantage is that these bars enlarge
the picture and therefore require more bandwidth to encode although completely
black parts shouldn't need much space - but the transition between the picture
and the black bars does need a lot of bits that could/should be used
elsewhere.</P>
<P>In such cases the black bars should be cropped to a final
resolution that is a multiply of 16. Another rule is to crop rather more than
less. Here is a list of common szenarios:</P>
<UL>
<LI>4:3 movies that fill the whole picture (no letterboxing):
aspect=4/3 or scale 5:4 to 4:3</LI>
<LI>16:9 movies that fill the whole picture (again no letterboxing):
aspect=16/9 or scale 5:4 to 16:9</LI>
<LI>16:9 movies that are letterboxed:
scale 5:4 to 4:3 and cut away some of the black bars</LI>
<LI>2.35 movies that are letterboxed:
scale 5:4 to 16:9 and cut away some of the black bars</LI>
</UL>
<H4>2.3.3 CBR, VBR and ABR</h3>
<P>These three acronyms stand for <I>Constant Bit Rate</I>, <I>Variable
Bit Rate</I> and <I>Average Bit Rate</I>.</P>
<UL>
<LI><B>CBR</B>: The encoder spends the same amount of bits on each
frame, regardless of how much it would really need. This has the
disadvantage that for example complete black pictures or complete silence in an
audio frame would waste a lot of bits while pictures or audio frames with a lot
of elements would benefit from these bits.</LI>
<LI><B>VBR</B>: The encoder takes as much bits as it needs. So black pictures get
very few bits and complex pictures get as much as they need. While this
provides the best quality the final size can't be predicted at all.</LI>
<LI><B>ABR</B>: You specify an average bit rate you want to achieve (too
be able to predict the final size in some way) and the encoder distributes the
bits it has available. Those bits it saves on black pictures it spends on complex
pictures. It is a compromise between quality and size
predictability.</LI>
</UL>
<H4>2.3.4 P/I/B-Frames, GOPs</H4>
<P>This is a quick explanation of an MPEG "GOP", or, "Group Of
Pictures".</P>
<UL>
<LI><B>GOP</B>:
<UL>
<LI>begins with an "I" frame, followed usually by a number of "P"
and "B" frames</LI>
<LI>each GOP is independent: all frames needed for predictions are
contained within each GOP</LI>
<LI>GOPs can be as small as a single I frame, or as large as
desired, but usually no more than 15 frames in length for MPEG2.
With MPEG4 GOPs can be as large as the maximal keyframe interval
(usually 200 - 300 frames). Most codecs permit GOPs of unlimited
length.</LI>
<LI>The longer the GOP, the more efficient, but less robust the
coding</LI>
</UL></LI>
<LI><B>I frame</B>:
<UL>
<LI>"Intra-coded" frames : average 7:1 reduction.</LI>
<LI>like JPEG, every video frame is broken into blocks of 8x8
pixels of Y, R-Y, and B-Y</LI>
<LI>blocks are grouped into "macroblocks" of 16x16</LI>
<LI>The standards say that macroblocks are grouped horizontally into slices
which have similar average block levels although real life codecs
don't necessarily follow that rule.</LI>
<LI>Again the standards say that multiple slices form a frame, and that
these frames are the resulting "I" frames, although codecs don't
really follow that either.</LI>
</UL></LI>
<LI><B>P frame</B>:
<UL>
<LI>P frames are predicted based on prior I or P frames plus
the addition of data for changed macroblocks.</LI>
<LI>average about 20:1 reduction, or about half the size of I
frames</LI>
</UL></LI>
<LI><B>B frame</B>:
<UL>
<LI>Bidirectionally predicted frames based on appearance and
positions of past and future frames macroblocks.</LI>
<LI>B frames require less data than P frames, averaging about
50:1 reduction.</LI>
<LI>B frames require more decoder buffer memory because 2
frames are compared during the reconstruction process.</LI>
<LI>B frames also require manipulation of the coding order:
frames moving from the coder to the decoder are NOT in
presentation sequence.</LI>
</UL>
</UL>
<P>For every macroblock in a P frame the encoder decides whether it
already knows this block from the preceding frame or whether it's completely
new. In the former case it only encodes the differences (<I>INTER mode</I>). In
the latter case it encodes the whole macroblock (<I>INTRA mode</I>).</P>
<P>The situation for B frames is like this: "I don't know that block" (INTRA
mode), "I know that block from the preceding I or P frame (backward mode)",
"this block looks like the block in the following frame (forward mode)" or
"this block looks like a mixture of the blocks from the preceding and the
following frames (bidirectional mode)".</P>
<P>MPEG1 GOPs are always "IBBPBBPBBPBB" (this is the order in which the frames
are displayed, not necessarily the order in which they were encoded/saved).
MPEG2 GOPs look like that as well. They may as well contain three B frames.
The DivX5 MPEG4 codec only uses "IBPBPBPBPB" because it's easier to implement
and because B frame support for MPEG4 codecs is rather new. You may expect
to see more B frames in the future.</P>
<H4>2.3.5 Quantizers</H4>
<H4>2.3.6 Global Motion Compensation (GMC)</H4>
<P>Global motion compensation (GMC) helps in zooming (bringing the camera closer)
and panning (swiveling the camera), if the objects remain static in type and
only change in size and position within an image. Activation of GMC is
advisable if nature films or landscape documentation are to be
compressed.</P>
<H4>2.3.7 Quarter Pixel (QP)</H4>
<P>Quarter pel or quarter pixel affects the precision in filtering of macro
blocks. DivX 4 works with half pel (1.5, 1.5); 1.25, 1.75, etc. are possible
starting with DivX 5. Conventional division of an image into macro blocks is
refined on the basis of integers - 16x16 or 8x8 - using the supplementary
information from what are known as virtual blocks. This allows the motions of
objects in images to be reproduced more realistically.</P>
<H3>2.4 lavcopts</H3>
<P>Description and hints to a few libavcodec encoding options. Check the man
page for (almost) complete list of the options!</P>
<UL>
<LI>vqmin=<1-31> (pass 1/2)<BR>
Specifies the minimum quantizer.
<UL>
<LI>1 is not recommended (much larger file, little (or no) quality
difference and sometimes other weird things: msmpeg4 and h263 will be
very low quality, ratecontrol will be confused (results in lower
quality), some decoders will not be able to decode it.</LI>
<LI>2 is recommended for normal mpeg4/mpeg1video encoding
(default).</LI>
<LI>3 is recommended for h263(p)/msmpeg4. The reason for 3 instead of 2
is that 2 could lead to overflows. This will be fixed for h263(p) by
changing the quanizer per MB in the future, but msmpeg4 doesnt support
that so it cant be fixed for that.</LI>
</UL></LI>
<LI>vqmax=<1-31> (pass 1/2)<BR>
Specifies the maximum quantizer. 31 is default, 10-31 is a sane range
(lower settings will increase the minimal quality).</LI>
<LI>vqscale=<1-31><BR>
Specifies the quantizer for constant quantizer (constant quality)
encoding. 1 is not recommended (much larger file, little quality
difference and possible other weird things). Lower values result in better
quality but larger files (see vqmin).
<LI>mbqmin/mbqmax=<1-31> (pass 1/2)<BR>
Specifies the minimum/maximum macroblock quantizers (2/31 is
default).</LI>
<LI>vmax_b_frames=<0-4><BR>
Uses B frames (0 is default). It sets the maximum number of B frames
between non B frames. 0-2 is a sane range for mpeg4. B frames may help to
increase the quality. They should be used together with
<CODE>vhq:v4mv</CODE>, <CODE>precmp=2:cmp=2:subcmp=2</CODE> (SATD) may help
too. It's recommended to use one B frame only. Don't use them with
animes.</LI>
<LI>vhq<BR>
Activates the high quality mode. It encodes each MB as in all modes and
choose the best. It should <B>always</B> be used if possible (disabled
by default).</LI>
<LI>v4mv<BR>
Allow 4 motion vectors per MB. If used with <CODE>vhq</CODE> it will
increase the quality but the performance price is there. Never use it
without <CODE>vhq</CODE> (disabled by default).
<LI>keyint <0-300><BR>
Maximum interval between keyframes. Keyframes (I frames) are needed for
seeking as seeking is only possible to a keyframe but they need more space
than non-keyframes, so larger numbers here mean slightly smaller files,
but less precise seeking.
<UL>
<LI>0: no keyframes</LI>
<LI><=132: for strict mpeg1/2/4 compliance.</LI>
<LI>150: default</LI>
<LI>>300: not recommended as the quality might be bad (depends upon
decoder, encoder and luck).</LI>
</UL></LI>
<LI>vpass=<1/2><BR>
Activate first (1) or second (2) video encoding pass. 2/3 pass mode is
recommended.</LI>
<LI>vbitrate=<value> (pass 1/2)<BR>
Specifies the avaraged bitrate to be used in kbits per second or bits per
seconds for values bigger then 16000, 800 is default. MEncoder will show
you the bitrates that should be used to get a specific file size (after the
first pass (audio)). Note that the quantizer settings have priority. If you
set the bitrate too high, the file size may not be reached as mencoder
doesn't know what to do with the remaining bits. If you set the maximum
quantizer too low the file size may be exceeded.</LI>
............
<H2>3. Preparations</H2>
<UL>
<LI>Reserve enough diskspace! ;)</LI>
<LI>Remove conflicting file names (may be called different):<BR>
<CODE>rm frameno.avi divx2pass.log subtitles.idx subtitles.sub</CODE></LI>
<LI>Read the <A HREF="cd-dvd.html#drives">drive section</A> if you got
problems with your DVD drive. You can specify the device with
<CODE>-dvd-device <device></CODE>.</LI>
<LI>Read the <A HREF="cd-dvd.html#dvd">dvd section</A> if you got
region problems (RPC2 drive) or if you want to learn more about the DVD
structure.</LI>
<LI><CODE>mplayer -dvd 1 -ao null -vo null -nocache -v -frames 0
2>/dev/null | grep -E " DVD"\|"\[open\]"</CODE> should give you a
usefull summary about the DVD.
<LI>The <A HREF="cd-dvd.html#dvd">dvd section</A> and the man page will
help you too to select the right inputs (we presume that you use
<CODE>libmpdvdkit2</CODE> as it's the easiest way).<BR>
Video:
<UL>
<LI><CODE>-dvd <title id></CODE>:<BR>
This tells MPlayer which movie (specified by <I>title id</I>) should
be encoded. Most times the main movie is just title '1'
(<CODE>-dvd 1</CODE>) but sometimes title '1' is the trailer and '2'
the real movie, sometimes you have to try over 30 title ids to find
the main movies. There even exist movies which separate the main movie
over multiple title ids (check the section about merging in this case).
We assume that you found the one right title id.</LI>
<LI><CODE>-chapter <chapter id>[-<end chapter id>]</CODE>:<BR>
Use this option if you want to encode just a certain chapter range
(titles are divided into several chapters).</LI>
<LI><CODE>-dvdangle <angle id></CODE>:<BR>
Some DVD discs contain scenes that can be viewed from multiple angles.
This option specifies the angle that should used (default: 1).</LI>
</UL>
Audio:
<UL>
<LI><CODE>-alang <two letter country code></CODE>:<BR>
This selects the audio language to be used, specified by a two-letter
country code (like <CODE>en</CODE> for English or <CODE>ja</CODE> for
Japanese). You can get a list with the summary command mentioned
above (<CODE>mplayer -v</CODE>).</LI>
<LI><CODE>-aid <id></CODE>:<BR>
Same as <CODE>-alang</CODE>. It selects the audio channel. The audio
id range of DVDs (AC3) is <CODE>128-159</CODE> (default: 128). The
id is mentioned in the list too.
</UL>
Subtitles:
<UL>
<LI>You usually shouldn't activate subtitle support for encoding
(see section 7. for more informations).</LI>
<LI><CODE>-slang <two letter country code></CODE>:<BR>
Same as <CODE>-alang</CODE> for subtitles (see above).
It selects the subtitle language to be used.
<LI><CODE>-sid <id></CODE>:<BR>
Same as <CODE>-aid</CODE> for subtitles. The subtitle id
range of DVDs is <CODE>0-31</CODE>. There deactivated by
default.</LI>
</UL></LI>
<LI>Check whether your DVD contains interlaced material or not (see
section above).</LI>
<LI>What format is the original movie?<BR>
<UL>
<LI>normal PAL (25 fps): nothing to do</LI>
<LI>field-encoded PAL (50 fps): 3-pass encoding <B>won't work!</B>
Skip the first pass (<CODE>-ovc frameno</CODE>) and add
all audio encoding parameters to the command line of the
next 2 passes. You need to encode audio in some form to
avoid A/V desynchronisations.</LI>
<LI>NTSC for tv (29.97 fps): nothing to do</LI>
<LI>NTSC ("real": 23.976 fps, header: 29.976 fps): You
need to specify <CODE>-ofps 23.976</CODE> on the command line
to avoid tons of "Dulicated frame!" messages and to
improve the final quality.</LI>
</UL></LI>
</UL>
<H2>4. Audio</H2>
<P></P>
<H3>4.1 mp3lame (1. pass)</H3>
<H4>4.1.1 mp3</H4>
<P><I>MP3</I> is short for <I>MPEG1 layer 3</I> and is an official
audio compression standard. If you talk about MP3 you actually talk
about a compression technique, not about a codec in particular.
<I>lame</I> is an acronym for <I>Lame Ain't an MP3 Encoder</I> (although
it is). It provides a encoder that produces high quality MP3 files.<BR>
<B>Advantages</B>: small, mencoder support (with command line options),
acceptable quality, well known, <B>good choice</B><BR>
<B>Usage</B>: <CODE>mencoder <DVD input> -ovc frameno -oac mp3lame
-lameopts <options> -o frameno.avi</CODE></P>
<H4>4.1.2 lameopts</H4>
<H4>4.1.3 Example</H4>
Now call <I>MEncoder</I> to encode the audio for you:<br>
<code>nice -+19 cat /space/*vob | nice-+19 mencoder -ovc frameno
-o frameno.avi -oac mp3lame -lameopts abr:br=128 -alang de -</code><br>
Here's an explanation for the command line arguments used:
<ul>
<LI><code>nice -+19</code> - Gives <I>MEncoder</I> the lowest process
priority so it won't disrupt your normal work.</LI>
<LI><code>-ovc frameno</code> - Output Video Codec is <code>frameno</code>
which means that no video data is actually written or processed at all.
</LI>
<LI><code>-o frameno.avi</code> - Write the output to the file called
<code>frameno.avi</code>. The audio <b>must</b> be written to a file
with that name as <I>MEncoder</I> will read the audio from exactly
this file with the exact name in the next step.</LI>
<LI><code>-oac mp3lame</code> - Output Audio Codec is the <I>lame</I>
encoder library.</LI>
<LI><code>-lameopts abr:br=128</code> - options for <en>lame</en>. It tells
<en>lame</en> to produce a ABR (average bitrate, it's a variable bitrate
setting with the average supplied by the user) with the average bitrate
of 128kbit/s.</LI>
<LI><code>-alang de</code></LI> - use the German sound track. An alternative
is using <code>-aid 129</code>.</LI>
</ul>
Wait some time and you'll have your audio. <I>MEncoder</I> even prints
some suggestions for the video bitrate:<br><code>
Recommended video bitrate for 650MB CD: 1845<br>
Recommended video bitrate for 700MB CD: 1992<br>
Recommended video bitrate for 800MB CD: 2287</code><br>
As you can see these values are rather high - but that's because an Ally
McBeal episode is only 41minutes long. So I'd go for two episodes per
CD and give the video a bitrate of about 1000.
<H3>4.2 copy</H3>
<P><CODE>-oac copy</CODE> will just copy the audio stream to the
resulting AVI. Almost all DVDs contain AC3 encoded audio. <I>AC3</I>
is again an official audio compression standard. Today there are decoders
for both, Windows and Linux that work with AC3 sound inside AVIs.
Therefore it is often enough to <I>copy</I> the audio stream to create an
AVI with AC3 audio.<BR>
<B>Advantages</B>: very fast, no recompression necessary, very good
quality (multi channel sound are preserved), known<BR>
<B>Disadvantages</B>: <B>big</B><BR>
<B>Usage</B>: If you use <CODE>-oac copy</CODE> you can just skip the
first pass.</P>
<H3>4.3 Vorbis</H3>
<P><I>Vorbis</I> is a new Open Source audio compression. It is available
for both Windows and Linux. There is no mencoder support yet.<BR>
<B>Advantages</B>: very small, good quality, multi channel support<BR>
<B>Disadvantages</B>: <B>unsupported</B> (see 4.4)</P>
<H3>4.4 Separate file</H3>
<P>In some cases it may be needed to encode from a separate file (Vorbis
audio for example).<BR>
<B>Advantages</B>: independent of mencoder's capabilities<BR>
<B>Disadvantages</B>: A/V Desynchronisation, may be unplayable<BR>
<B>Usage</B>: Skip the first pass and use <CODE>-audiofile
<filename></CODE> later on.</P>
<H2>5. Statistics (2. pass)</H2>
<H3>5.1 cropping</H3>
<UL>
<LI>cropdetect</LI>
<LI>crop with value dividable to 16 (crop more than needed)</LI>
</UL>
<H3>5.2 videobitrate, aspect, bpp, bpb</H3>
<H4>5.2.1 General</H4>
<UL>
<LI>desired size? how many cds? xmode2?</LI>
<LI>videobitrate = videosize in bytes * 8 / (length in s * 1000)</LI>
<LI>aspect: "prescaling" from mplayer output</LI>
</UL>
<H4>5.2.2 bpp</H4>
<UL>
<LI>bpp = videobitrate in bits * 1000 / (width * height * fps)</LI>
<LI>calcbpp.pl <unscaled cropped resolition> <aspect>
<videobitrate> <fps><BR>
Example: <CODE>calcbpp.pl 720x440 16/9 896 25</CODE></LI>
<LI>bpp values:<BR>
<B>< 0.10</B>: don't do it. Please. I beg you!<BR>
<B>< 0.15</B>: It will look bad.<BR>
<B>< 0.20</B>: You will notice blocks, but it will look ok.<BR>
<B>< 0.25</B>: It will look really good.<BR>
<B>> 0.25</B>: It won't really improve visually.<BR>
<B>> 0.30</B>: Don't do that either - try a bigger resolution
instead.</LI>
<LI>Action scenes increase, slow motion decrease the needed bpp
(from 0.15: very good to 0.25 not perfect)</LI>
</UL>
<H4>5.2.3 bpb</H4>
<UL>
<LI>bpb = videobitrate in bits / (fps * (width * heigth) / (16*16))
<LI>bpb values:<BR>
<B>~40</B>: minimum bpb<BR>
<B>~50</B>: very good results<BR>
<B>>60</B>: waste of bandwith</LI>
<LI>Smaller resoltutions need more bpb to look good</LI>
<LI>Independant of codec used</LI>
</UL>
<H3>5.3 lavcopts</H3>
<H4>5.3.1 General Hints</H4>
<UL>
<LI>vhq (never forget)</LI>
<LI>vcodec=mpeg4 (mandatory)</LI>
<LI>Don't scale down too much!</LI>
<LI>width >= 576 (DVDs)</LI>
<LI>final image dimensions after cropping and scaling should be
multiplies of 16. If not -> drastically reduced quality</LI>
<LI>deinterlacing (see above)</LI>
</UL>
<H4>5.3.2 Tuning</H4>
<UL>
<LI>vqmin=2, vqmax=31 (default): per frame quantizer, vqscale for fix
quantizer</LI>
<LI>mbqmin=2, mbqmax=31 (default): mb based quantizers</LI>
<LI>v4mv (slow!)</LI>
<LI>fair distribution of bits between low-detail and high-detail
scenes -> vqcomp 0.6-0.8 (default: 0.5)</LI>
<LI>Still bad quality:<BR>
<UL>
<LI>scale down</LI>
<LI>add a weak gaussian blur to reduce the amount of detail</LI>
</UL></LI>
</UL>
<H4>5.3.3 Fine Tuning</H4>
<UL>
<LI>non-animated movies (to save bits for use elsewhere):<BR>
<UL>
<LI>vlelim=-4</LI>
<LI>vcelim=9</LI>
<LI>lumi_mask=0.05</LI>
<LI>dark_mask=0.01</LI>
<LI>Make the nubmers closer to 0 if it looks too muddy</LI>
</UL></LI>
<LI>animated movies:<BR>
<UL>
<LI>predia=3:dia=3</LI>
</UL></LI>
<LI>old b&w movies:<BR>
<UL>
<LI>vlelim=-2:vcelim=3<BR>
vlelim=-3:vcelim=5<BR>
vlelim=-4:vcelim=7</LI>
<LI>lumi_mask=0.07:dark_mask=0.10:naq:<BR>
lumi_mask=0.10:dark_mask=0.12:naq:<BR>
lumi_mask=0.12:dark_mask=0.15:naq:<BR>
lumi_mask=0.13:dark_mask=0.16:naq:</LI>
<LI>keyint=300-350 (not below 300)</LI>
<LI>480x320 <= Resolution <= 512x384</LI>
<LI>no vhq (faster)</LI>
<LI>or vhq:v4mv</LI>
<LI>vqscale=2 (1-pass only!)</LI>
<LI>mpeg_quant (less blocks on large zones, more bits)</LI>
<LI>gray (faster), needs <CODE>-lavdopts gray</CODE> for
decoding.</LI>
<LI>ogg vorbis with ogm format recomended:
45 kbits/s for typical old mono-only audio stream</LI>
<LI>Example:<BR>
<CODE>mplayer -dvd 1 -dumpstream<BR>
rm -f audiodump.pcm ; mkfifo -m 600 audiodump.pcm<BR>
mplayer -quiet -vc null -vo null -aid 128 -ao pcm
-nowaveheader stream.dump &<BR>
oggenc --raw --raw-bits=16 --raw-chan=2 --raw-rate=48000 -q 1
         -o audio-us.ogg +audiodump.pcm &<BR>
wait<BR></CODE></LI>
</UL></LI>
<LI>smoke, clouds:<BR>
<UL>
<LI>vqcomp=0.7:vqblur=0.2:mpeg_quant, if still bad:</LI>
<LI>add vlelim=-4:vcelim=-7:vqmin=1, maybe:</LI>
<LI>add -sws 10 -ssf ls=1, if problem remains:</LI>
<LI>Force q=1 with vrc_override for the scene</LI>
<LI>vqmin=2: ok for cigarette smoke (sharp) and red/yellow cloud
        (explosion, coud of fire), not ok with grey/ver fuzzy cloud</LI>
</UL></LI>
</UL>
<H4>5.3.4 Explanation</H4>
<UL>
<LI><B>macroblock</B> (16x16 regions):<BR>
2 8x8 luma blocks (2 or 4 (????))<BR>
2 8x8 chroma blocks<BR>
Each mb is scored with a complexity value which
weights the quantizer accordingly<BR></LI>
<LI><CODE>scplx_mask</CODE>:<BR>
Raise quantizer on mb's with lots of spacial complexity.
Spacial complexity is measured by variance of the texture
(this is just the actual image for I blocks and the difference
from the previous coded frame for P blocks).</LI>
<LI><CODE>tcplx_mask</CODE>:<BR>
Raise quantizer on mb's with lots of temporal complexity.
Temporal complexity is measured according to motion vectors.</LI>
<LI><CODE>dark_mask</CODE>:<BR>
Raise quantizer on very dark mb's.</LI>
<LI><CODE>lumi_mask</CODE>:<BR>
Raise quantizer on very bright mb's.</LI>
<LI><CODE>vlelim</CODE>:<BR>
No change for luma differences below specified threshold</LI>
<LI><CODE>vcelim</CODE>:<BR>
No change for chroma differences below specified threshold</LI>
<LI><B>adaptive quantization</B>:<BR>
Per-mb basis, leads to strange bitrate results (naq)</LI>
<LI><CODE>naq</CODE>:<BR>
normalize adaptive quantization,
rescaling of all quantizers at the end to get the requested
avarage quantizer (qp)</LI>
</UL>
<H3>5.4 Encoding example</H3>
<P><CODE>mencoder -dvd 1 -o /dev/null -oac copy -ovc lavc
-lavcopts vcodec=mpeg4:vbitrate=1000:vhq:vqmin=2:
vlelim=-4:vcelim=9:lumi_mask=0.05:dark_mask=0.01:vpass=1
-vop scale=640:480,crop=716:572:2:2</CODE><BR>
Let's assemble the command line options one by one:
<ul>
<LI><code>-oac copy</code> - <I>MEncoder</I> needs the audio information
from the first pass in order to keep audio/video synchronization.
You mustn't use <code>-nosound</code>, although I suggested it in earlier
versions of this guide. If you want the technical details then have a look
at <a href="http://mplayerhq.hu/pipermail/mplayer-users/2002-August/019551.html"
target="_blank">the MPlayer mailing list</a>.</LI>
<LI><code>-o /dev/null</code> - The output of the first pass isn't needed
either, so just dump it.</LI>
<LI><code>-ovc lavc</code> - chose <I>lavc</I> as the Output Video
Codec.</LI>
<LI><code>-lavcopts vcodec=mpeg4:vbitrate=1000:vhq:vqmin=2:vqmax=31:vpass=1
</code> -
<I>libavcodec</I> actually supports a handful of different video codecs.
With <code>vcodec=mpeg4</code> we chose the MPEG4 compliant encoder.
<code>vbitrate=1000</code> is the desired bitrate. <code>vhq</code>
(very high quality) tells the encoder to spend a lot of time
optimizing the results. It is worth it. <code>vpass=1</code> finally
tells the encoder that this is the first pass only. Using
<code>vqmin</code> and <code>vqmax</code> tells the codec how much
it may compress the video at least and at most. Keeping these
at their defaults (see <code>man mencoder</code>) restricts the
achievable output bitrate a lot so I advice choosing a broader range.
Note that I'm no expert on quantizers. If I'm wrong here please
<a href="mailto:moritz@bunkus.org">drop me a line</a>.</LI>
<LI><code>-vop scale=640:480</code> - scale the picture down to
640 by 480 pixels. I've read on the mailing list that a new switch
has been added that automatically calculates the height from the width
or the other way round so that the aspect ratio will be kept. I haven't
tried it myself but it should look like this: <code>-vop scale -xy 640
</code>. Note that while it is possible to actually enlarge the picture
I strongly advise against it. There are several drawbacks: the encoder
needs a lot more bits to achieve the same quality you'd get when
encoding a smaller picture and then scaling it up while playing it back,
and you might even get distorted pictures (Peter Schuller reported
such a case to me). Always scale down.</LI>
<LI>If your picture is interlaced you can deinterlace it with
<code>-npp lb</code>.</LI>
<LI>We only want a preview so we provide the start position and the length:
<code>-ss 0:20</code> specifies 20seconds into the movie as our beginning,
and <code>-endpos 0:30</code> says that we want 30seconds to be processed.
This parameter is named badly as it's not the end position but the
duration that it specifies.</LI>
</ul>
Put together the complete command line looks like this:<br>
<code>nice -+19 mencoder -oac copy -o /dev/null -ovc lavc -lavcopts
vcodec=mpeg4:vbitrate=1000:vhq:vpass=1 -vop scale=640:480 -npp lb
-ss 0:20 -endpos 0:30 /space/first.vob
</code><br>
Final (both pass):
For this step we can keep most of our previous command line. Of course we
change the <code>vpass=1</code> to <code>vpass=2</code> to tell the encoder
that it's the second pass this time. Of course we should save the final output
and replace <code>-o /dev/null </code> with something useful, e.g. <code>-o
ally-preview.avi</code>.<p>
Here's the command line:<br>
<code>nice -+19 mencoder -oac copy -o ally-preview.avi -ovc lavc -lavcopts
vcodec=mpeg4:vbitrate=1000:vhq:vpass=2 -vop scale=640:480 -npp lb
-ss 0:20 -endpos 0:30 /space/first.vob
</code><br>
After <I>MEncoder</I> finishes take a look at your preview. Adjust parameters
if needed. Redo the preview until you're happy with it.
<a name="tc_mplayer_final">
<h4>5.3.4. Generating the final AVI (both passes)</h4>
The calls for the final options simply leave out <code>-ss</code> and
<code>-endpos</code>. Unfortunately <I>MEncoder</I> can't handle multiple
input files on the command line. That's what the <code>cat</code> is for.
Just enter:<br><code>
cat /space/*vob | nice -+19 mencoder -oac copy -o /dev/null -ovc lavc -lavcopts
vcodec=mpeg4:vbitrate=1000:vhq:vpass=1:vqmin=2:vqmax=31 -vop scale=640:480
-npp lb -<br>
cat /space/*vob | nice -+19 mencoder -oac copy -o ally.avi -ovc lavc -lavcopts
vcodec=mpeg4:vbitrate=1000:vhq:vpass=2:vqmin=2:vqmax=31 -vop scale=640:480
-npp lb -</code><br>
Note that <code>-ss</code> does not seem to work with the <code>cat *vob |
mencoder...</code> variant. So for the preview just specify the first
vob as the last argument on the command line (just look up).<p>
<H2>6. Final encoding (3. pass)</H2>
<H3>6.1 Quality check</H3>
<UL>
<LI>play movie, check quality</LI>
<LI>check synchronisation</LI>
<LI><CODE>countquant.pl < divx2pass.log</CODE></LI>
<LI>If the lowest quantizer is almost always used, the
picture size should be increased.</LI>
<LI>If there where too many quantizers >=4, the picture size should be
decreased.</LI>
<LI>In those cases step 3 (2. pass) should be repeated.</LI>
</UL>
<H2>7. Subtitles</H2>
<P>On a DVD subtitles are stored as pictures that are shown on top of the movie
by your movie player. That way the authors have a wide choice of how their
subtitles look like (and it makes subtitles in Asian languages much easier
to implement).</P>
<H3>7.1 Text subtitles</H3>
<UL>
<LI>Download text subtitles and keep them in a separate file.<BR>
There may be synchronisation problems (try <CODE>-subdelay</CODE>).
You can convert them to MPlayers subtitle format (MPsub) using the
<CODE>-dumpmpsub</CODE> option.</LI>
<LI>Convert the Vobsubs to text subtitles (MPsub) using
<CODE>TOOLS/subrip.c</CODE>.<BR>
To convert the VobSubs to text subtitles, OCR (<CODE>gocr</CODE> is beeing
used to guess the letters. It is therefore often a time intense way as you
first have to tune subrip to the used VobSubs. Also the accuracy isn't always
very good.</LI>
<LI>The main advantages of text subtitles are that MPlayer's subtitle
renderer can be used with customized fonts which produces much better
quality then Vobsub subtitles. There is even FreeType support available.
Additionaly the created subtitles can be changed easy.</LI>
</UL>
<H3>7.2 Vobsub</H3>
<UL>
<LI>Extract one vobsub to an external file. Example:
Add the following to any encoding step:<BR>
<CODE>-vobsubout subtitles -vobsuboutindex 0 -sid 2</CODE></LI>
<LI>Extract all vobsubs to one single file:<BR>
<OL>
<LI>Rip the DVD to harddisk. Example: <CODE>mplayer -dvd 1
-dumpstream</CODE></LI>
<LI>Mount the DVD and copy the .ifo file</LI>
<LI>Extract all vobsubs to one single file. Example (12 subtitles):
<CODE>for f in 0 1 2 3 4 5 6 7 8 9 10 11 ; do
mencoder -ovc copy -oac copy -o /dev/null -sid $f -vobsubout subtitles
-vobsuboutindex $f -ifo vts_01_0.ifo stream.dump; done</CODE></LI>
</OL>
<LI>Insert the extracted Vobsubs in a container that allows them (e.g. OGM).
At the moment there is no such support included in MEncoder.</LI>
</UL>
<H3>7.3 Subtitle Rendering</H3>
<P>Subtitles can be rendered inside the created image. This solution is
to avoid if possible as the process can't be reversed (fixed subtitles).
It can be done with text or vobsub subtitles. An additional problem is that
you either have to render the subtitles over the image itself or create black
bands to render the subtitles there (really ugly as it will unnecessary
enlarge the movie size and it probably effects quality/bitrate/etc too...)</P>
<H2>7. Additional tricks</H3>
<UL>
<LI>desynched audio? Just CBR?</LI>
<LI>splitting, merging</LI>
<LI> Extracting audio from VOBs/MPEGs/AVIs
<I>MPlayer</I> can be used easily to extract sound. The <code>-vo null</code>
options tells <I>MPlayer</I> not to show the video.
<ul>
<LI>VOB -> AC3: <code>mplayer -vo null -nogui -alang de -dumpaudio
-dumpfile lang-de.ac3 *vob</code><br>This will extract the German audio to
a file called <code>lang-de.ac3</code>.</LI>
<LI>VOB -> WAV: <code>mplayer -vo null -nogui -aid 128 -ao pcm
-aofile lang-de.wav *vob</code><br>This will extract audio track no. 128,
downmix the AC3 sound to PCM and write the results to
<code>lang-de.wav</code>.</LI>
<LI>MPG -> MP2: <code>mplayer -vo null -nogui -dumpaudio
-dumpfile audio.mp2 myvideo.mpg</code><br>This will extract the audio to
a file called <code>audio.mp2</code>.</LI>
<LI>AVI -> WAV: <code>mplayer -vo null -nogui -ao pcm -aofile audio.wav
myvideo.avi</code><br>This will extract the audio, convert it to PCM
and write the resulting .WAV to <code>audio.wav</code>.</LI>
</ul>
I'm not listing all possible options. Generally use <code>-dumpaudio -dumpfile
newaudio.extension</code> if you want to extract the audio without processing
and <code>-ao pcm -aofile newaudio.wav</code> if you want the WAV
itself.</LI>
</UL>
</BODY>
</HTML>