summaryrefslogtreecommitdiffstats
path: root/DOCS/tech/encoding-guide.txt
blob: 0393bcbb5f7186486bd0581fedad264f78281965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Topics:


I. Preparing to encode
   1. Identifying source material and framerate
   2. Selecting the quality you want
   3. Constraints for efficient encoding
   4. Cropping and scaling
   5. Choosing resolution and bitrate

II. Containers and codecs
   1. Where the movie will be played
   2. Constraints of DVD, SVCD, and VCD
   3. Limitations of AVI container

III. Basic MEncoder usage
   1. Selecting codecs & format
   2. Selecting input file or device
   3. Loading video filters
   4. Notes on A/V sync

IV. Encoding procedures
   1. Encoding progressive video
   2. Two-pass encoding
   3. Encoding interlaced video
   4. Deinterlacing
   5. Inverse telecine
   6. Capturing TV input
   7. Dealing with mixed-source content
   8. Low-quality & damaged sources

V. Optimizing encoding quality
   1. Noise removal
   2. Pure quality-gain options
   3. Questionable-gain options
   4. Advanced MPEG-4 features




II. Containers and codecs

II.1. Where the movie will be played

Perhaps the most important factor to choosing the format in which you
will encode your movie is where you want to be able to play it.
Usually this involves a tradeoff between quality and features, since
the formats supported by the widest variety of players are also the
worst in regards to compression.

If you want to be able to play your encode on standalone/set-top
players, your primary choices are DVD, VCD, and SVCD. There are also
extensions such as KVCD and XVCD which violate the standards but work
on many players and deliver higher quality. Modern players are
beginning to support MPEG-4 ("DivX") movies in AVI and perhaps other
containers as well, but these are often buggy and require you to
restrict your encodes to certain subsets of the full MPEG-4
functionality.

If you wish to be able to share your movies with Windows or Macintosh
users, without them having to install additional software, your
choices are very limited. The ancient MPEG-1 format with MP2 or PCM
audio is probably the only choice that is universally supported.
Interoperability with Windows/Mac also comes into play when deciding
how to encode and whether to scale to preserve aspect, since popular
media player applications for these systems do not honor the aspect
ratio encoding stored in MPEG-4 avi files.



IV.2. Two-pass encoding

The complexity (and thus the number of bits) required to compress the
frames of a movie can vary greatly from one scene to another. Modern
video encoders can adjust to these needs as they go and vary the
bitrate. However, they cannot exceed the requested average bitrate for
long stretches of time, because they do not know the bitrate needs of
future scenes.

Two-pass encoding solves this problem by encoding the movie twice.
During the first pass, statistics are generated regarding the number
of bits used by each frame and the quantization level (quality) at
which it was encoded. Then, when the second pass begins, the encoder
reads these statistics and redistributes the bits from frames where
they are in excess to frames that are suffering from low quality.

In order for the process to work properly, the encoder should be given
exactly the same sequence of frames during both passes. This means
that the same filters must be used, the same encoder parameters must
be used (with the possible exception of bitrate), and the same frame
drops and duplications (if any) must take place.

In theory it's possible to use -oac pcm or -oac copy during the first
pass to avoid spending time encoding the audio. However, this can
result in slight variations in which frames get dropped or duplicated,
so it may be preferable to encode the audio during the first pass as
well as the second. This also allows you to examine the final audio
bitrate and filesize, and to adjust the audio or video bitrate
slightly between passes if you don't meet your target size.

Here is an example:

  Encoding from an existing AVI file
  500 kbit/sec MPEG-4 video
  96 kbit/sec average-bitrate MP3 audio

  mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
  abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=1

  mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
  abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=2

If you do not want to overwrite the output from the first pass when
you begin the second, you can use the -o option to choose a different
output filename. Note the addition of the vpass option in this
example. If vpass is not specified, single-pass encoding is performed.
If vpass=1, a log file is written with statistics from the first pass.
If vpass=2, the log file is read and the second pass is encoded based
on those statistics. If you are short on disk space or don't want the
extra disk wear from writing the file twice, you can use -o /dev/null
during the first pass. However, sometimes it is beneficial to watch
the first-pass file before beginning the second pass to make sure
nothing went wrong in the encoding.

Next, an example using Xvid instead of libavcodec:

  Encoding from an existing AVI file
  500 kbit/sec MPEG-4 video
  Copying the existing audio stream unmodified

  mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
  -xvidencopts bitrate=400:pass=1

  mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
  -xvidencopts bitrate=400:pass=2

The options used are slightly different, but the process is otherwise
the same.