comparison DOCS/tech/encoding-guide.txt @ 15796:5a39a40aa702

Encoding guide featured by Richard Felker III, and updated by Jeff Clagg. Part of this guide is already in the XML docs.
author gpoirier
date Wed, 22 Jun 2005 09:39:27 +0000
parents
children cfe186231e2d
comparison
equal deleted inserted replaced
15795:546b49d7147d 15796:5a39a40aa702
1 Topics:
2
3
4 I. Preparing to encode
5 1. Identifying source material and framerate
6 2. Selecting the quality you want
7 3. Constraints for efficient encoding
8 4. Cropping and scaling
9 5. Choosing resolution and bitrate
10
11 II. Containers and codecs
12 1. Where the movie will be played
13 2. Constraints of DVD, SVCD, and VCD
14 3. Limitations of AVI container
15
16 III. Basic MEncoder usage
17 1. Selecting codecs & format
18 2. Selecting input file or device
19 3. Loading video filters
20 4. Notes on A/V sync
21
22 IV. Encoding procedures
23 1. Encoding progressive video
24 2. Two-pass encoding
25 3. Encoding interlaced video
26 4. Deinterlacing
27 5. Inverse telecine
28 6. Capturing TV input
29 7. Dealing with mixed-source content
30 8. Low-quality & damaged sources
31
32 V. Optimizing encoding quality
33 1. Noise removal
34 2. Pure quality-gain options
35 3. Questionable-gain options
36 4. Advanced MPEG-4 features
37
38
39
40 I. Preparing to encode
41
42 Before you even think about encoding a movie, you need to take several
43 preliminary steps to
44
45
46 I.1. Identifying source material and framerate
47
48 The first and most important step before you encode should be
49 determining what type of content you're dealing with. If your source
50 material comes from DVD or broadcast/cable/satellite TV, it will be
51 stored in one of two formats: NTSC for North America and Japan, and
52 PAL for Europe, etc. But it's important to realize that this is just
53 the formatting for presentation on a television, and often does NOT
54 correspond to the original format of the movie. In order to produce a
55 suitable encode, you need to know the original format. Failure to take
56 this into account will result in ugly combing (interlacing) artifacts
57 in your encode, and will greatly reduce the quality/bitrate ratio of
58 the encoder!
59
60 Here is a list of common types of source material, where you're likely
61 to find them, and their properties:
62
63 Standard Film: Produced for theatrical display at 24fps.
64
65 PAL video: Recorded with a PAL video camera at 50 fields per second. A
66 field consists of just the even or odd numbered lines of a frame.
67 Television was designed to refresh these in alternation as a cheap
68 form of analog compression. The human eye supposedly compensates for
69 this, but once you understand interlacing you'll learn to see it on TV
70 too and never enjoy TV again. Two fields do NOT make a complete frame,
71 because they are captured 1/50 of a second apart in time, and thus
72 they do not line up unless there is no motion.
73
74 NTSC Video: Recorded with an NTSC video camera at 59.94 fields per
75 second, or 60 fields per second in the pre-color era. Otherwise
76 similar to PAL.
77
78 Animation: Usually drawn at 24fps, but animation also comes in
79 mixed-framerate varieties.
80
81 Computer Graphics (CG): Can be any framerate, but 24 and 30 fps are
82 the most frequently encountered in NTSC regions, and 25 fps in PAL
83 regions.
84
85 Old Film: Various lower framerates.
86
87 Movies consisting of frames are referred to as progressive, while
88 those consisting of independent fields are called interlaced, or
89 sometimes video, although this latter term is ambiguous.
90
91 To further complicate matters, some movies will be a mix of several of
92 the above.
93
94 The most important distinction to make between all of these formats is
95 that some are frame-based, while others are field-based. WHENEVER a
96 movie is prepared for display on television (including DVD), it is
97 converted to a field-based format. The various methods by which this
98 can be done are collectively referred to as "pulldown", of which the
99 infamous NTSC "3:2 telecine" is one variety. Unless the original
100 material was also field-based (and the same fieldrate), you are
101 getting the movie in a format other than the original.
102
103 There are several common types of pulldown:
104
105 PAL 2:2 pulldown: The nicest of them all. Each frame is shown for two
106 fields duration, by extracting the even and odd lines and showing them
107 in alternation. If the original material is 24fps, this process speeds
108 up the movie by 4%.
109
110 PAL 2:2:2:2:2:2:2:2:2:2:2:3 pulldown: Every 12th frame is shown for
111 three fields duration, instead of just two. This avoids the 4% speedup
112 issue, but makes the process much more difficult to reverse. It is
113 usually seen in musical productions where adjusting the speed by 4%
114 would seriously damage the musical score.
115
116 NTSC 3:2 telecine: Frames are shown alternatively for 3 fields or 2
117 fields duration. This gives a fieldrate 5/2 times the original
118 framerate. The result is also slowed down very slightly from 60 fields
119 per second to 59.94 fields per second to maintain NTSC fieldrate.
120
121 NTSC 2:2 pulldown: Used for showing 30fps material on NTSC. Nice, just
122 like 2:2 PAL pulldown.
123
124 There are also methods for converting between NTSC and PAL video. Such
125 topics are beyond the scope of this guide. If you encounter such a
126 movie and want to encode it, your best bet is to find a copy in the
127 original format. NTSC/PAL conversion is highly destructive and cannot
128 be reversed cleanly, so your encode will greatly suffer if it is made
129 from a converted source.
130
131 When video is stored on DVD, consecutive pairs of fields are grouped
132 as a frame, even though they are not intended to be shown at the same
133 moment in time. The MPEG2 standard used on DVD and digital TV provides
134 a way to encode the original progressive frames, and store the number
135 of fields for which each should be shown in the frame headers. If this
136 method has been used, the term "soft telecine" will often be used to
137 describe the movie, since the process only directs the DVD player to
138 apply pulldown to the movie rather than altering the movie itself.
139 This case is highly preferable since it can easily be reversed
140 (actually ignored) by the encoder, and since it preserves maximal
141 quality. However, many DVD and broadcast production studios do not use
142 proper encoding techniques, and instead produce movies with "hard
143 telecine", where fields are actually duplicated in the encoded MPEG2.
144
145 The procedures for dealing with these cases will be covered later in
146 this guide. For now, we leave you with some guides to identifying
147 which type of material you're dealing with:
148
149 NTSC regions:
150
151 - If MPlayer prints that the framerate has changed to 23.976 when
152 watching your movie, and never changes back, it's almost certainly
153 24fps content that has been "soft telecined".
154
155 - If MPlayer shows the framerate switching back and forth between
156 23.976 and 29.97, and you see "combing" at times, then there are
157 several possibilities. The 23.976 fps segments are almost certainly
158 24fps progressive content, "soft telecined", but the 29.97 fps parts
159 could be either hard-telecined 24fps content or NTSC video content.
160 Use the same guidelines as the following two cases to determine
161 which.
162
163 - If MPlayer never shows the framerate change, and every single frame
164 with motion appears combed, your movie is NTSC video at 59.94 fields
165 per second.
166
167 - If MPlayer never shows the framerate change, and two frames out of
168 every five appear combed, your movie is "hard telecined" 24fps
169 content.
170
171 PAL regions:
172
173 - If you never see any combing, your movie is 2:2 pulldown.
174
175 - If you see combing alternating in and out every half second, then
176 your movie is 2:2:2:2:2:2:2:2:2:2:2:3 pulldown.
177
178 - If you always seem combing during motion, then your movie is PAL
179 video at 50 fields per second.
180
181 Hint: MPlayer can slow down movie playback with the -speed option. Try
182 using -speed 0.2 to watch the movie very slowly and identify the
183 pattern, if you can't see it at full speed.
184
185
186 I.2. Selecting the quality you want
187
188 It's possible to encode your movie at a wide range of qualities. With
189 modern video encoders and a bit of pre-codec compression (downscaling
190 and denoising), it's possible to achieve very good quality at 700 MB,
191 for a 90-110 minute widescreen movie. And all but the longest movies
192 can be encoded with near-perfect quality at 1400 MB.
193
194 If you do not plan to store your movies on CD or other size-limited
195 media, and you want maximal quality at all costs, you can encode in
196 constant quantizer mode, which will not aim to meet a specific target
197 bitrate or filesize but instead use the maximal accuracy encoding for
198 all frames. This is not recommended in most cases, because you can
199 achieve significantly smaller file sizes without noticeable loss.
200 However, it may be desirable for the hardcore archivists out there.
201
202
203 I.4. Cropping and scaling
204
205 Recall from the previous section that the final picture size you
206 encode should be a multiple of 16 (in both width and height). This can
207 be achieved by cropping, scaling, or a combination of both.
208
209 When cropping, there are a few guidelines that must be followed to
210 avoid damaging your movie. The normal YUV format, 4:2:0, stores chroma
211 (color) information subsampled, i.e. chroma is only sampled half as
212 often in each direction as luma (intensity) information. Observe this
213 diagram, where L indicates luma sampling points and C chroma.
214
215 L L L L L L L L
216 C C C C
217 L L L L L L L L
218
219 L L L L L L L L
220 C C C C
221 L L L L L L L L
222
223 As you can see, rows and columns of the image naturally come in pairs.
224 Thus your crop offsets and dimensions MUST be even numbers. If they
225 are not, the chroma will no longer line up correctly with the luma. In
226 theory, it's possible to crop with odd offsets, but it requires
227 resampling the chroma which is potentially a lossy operation and not
228 supported by the crop filter.
229
230 Further, interlaced video is sampled as follows:
231
232 TOP FIELD BOTTOM FIELD
233
234 L L L L L L L L
235 C C C C
236 L L L L L L L L
237
238 L L L L L L L L
239 C C C C
240 L L L L L L L L
241
242 L L L L L L L L
243 C C C C
244 L L L L L L L L
245
246 L L L L L L L L
247 C C C C
248 L L L L L L L L
249
250 As you can see, the pattern does not repeat until after 4 lines. So
251 for interlaced video, your y-offset and height for cropping must be
252 multiples of 4.
253
254 So how do you determine a crop rectangle to begin with? Sometimes you
255 can guess, but the cropdetect filter in MPlayer can make it easy. Run
256 MPlayer with -vf cropdetect and it will print out the crop settings to
257 remove the borders. You should let the movie run long enough that the
258 whole picture area is used, in order to get accurate crop values.
259 Then, test the values you get with MPlayer, using the command line
260 cropdetect printed, and adjust the rectangle as needed. The rectangle
261 filter can help by allowing you to interactively position the crop
262 rectangle over your movie. Remember to follow the above divisibility
263 guidelines so that you do not misalign the chroma planes.
264
265 If you will be scaling your movie, it's usually best to crop only the
266 black borders and noise, then scale so that the resulting dimensions
267 are multiples of 16. This can slightly distort the aspect ratio of
268 your movie, but in practice the error cannot be seen. It's certainly
269 much less visible than the MPEG artifacts you will see from failing to
270 crop & scale well.
271
272 In certain cases, scaling may be undesirable. Scaling in the vertical
273 direction is difficult with interlaced video, and if you wish to
274 preserve the interlacing, you should usually refrain from scaling. If
275 you will not be scaling but you still want to use multiple-of-16
276 dimensions, you will have to overcrop. Do not undercrop, since black
277 borders are very bad for encoding!
278
279
280
281
282 I.5. Choosing resolution and bitrate
283
284 If you will not be encoding in constant quantizer mode, you need to
285 select a bitrate. The concept of bitrate is quite simple. It's the
286 (average) number of bits that will be consumed to store your movie,
287 per second. Normally bitrate is measured in kilobits (1000 bits) per
288 second. The size of your movie on disk is the bitrate times the length
289 of the movie in time, plus a small amount of "overhead" (see the
290 section on codecs and containers). Other parameters such as scaling,
291 cropping, etc. will NOT alter the file size unless you change the
292 bitrate as well!
293
294 Bitrate does NOT scale proportional to resolution. That is to say, a
295 320x240 file at 200 kbit/sec will not be the same quality as the same
296 movie at 640x480 and 800 kbit/sec! There are two reasons for this:
297
298 1. Perceptual: You notice MPEG artifacts more if they're scaled up
299 bigger! Artifacts appear on the scale of blocks (8x8). Your eye
300 will not see errors in 4800 small blocks as easily as it sees
301 errors in 1200 large blocks (assuming you'll be scaling both to
302 fullscreen).
303
304 2. Theoretical: When you scale down an image but still use the same
305 size (8x8) blocks for the frequency space transform, you move more
306 data to the high frequency bands. Roughly speaking, each pixel
307 contains more of the detail than it did before. So even though your
308 scaled-down picture contains 1/4 the information in the spacial
309 directions, it could still contain a large portion of the
310 information in the frequency domain (assuming that the high
311 frequencies were underutilized in the original 640x480 image).
312
313 Past guides have recommended choosing a bitrate and resolution based
314 on a "bits per pixel" approach, but this is usually not valid due to
315 the above reasons. A better estimate seems to be that bitrates scale
316 proportional to the square root of resolution, so that 320x240 and 400
317 kbit/sec would be comparable to 640x480 at 800 kbit/sec. However this
318 has not been verified with theoretical or empirical rigor. Further,
319 given that movies vary greatly with regard to noise, detail, degree of
320 motion, etc., it's futile to make general recommendations for bits per
321 length-of-diagonal (the analogue of bits per pixel, using the square
322 root).
323
324 So far we have discussed the difficulty of choosing a bitrate and
325 resolution.
326
327 .................
328
329
330
331
332
333
334
335
336
337
338 II. Containers and codecs
339
340 II.1. Where the movie will be played
341
342 Perhaps the most important factor to choosing the format in which you
343 will encode your movie is where you want to be able to play it.
344 Usually this involves a tradeoff between quality and features, since
345 the formats supported by the widest variety of players are also the
346 worst in regards to compression.
347
348 If you want to be able to play your encode on standalone/set-top
349 players, your primary choices are DVD, VCD, and SVCD. There are also
350 extensions such as KVCD and XVCD which violate the standards but work
351 on many players and deliver higher quality. Modern players are
352 beginning to support MPEG-4 ("DivX") movies in AVI and perhaps other
353 containers as well, but these are often buggy and require you to
354 restrict your encodes to certain subsets of the full MPEG-4
355 functionality.
356
357 If you wish to be able to share your movies with Windows or Macintosh
358 users, without them having to install additional software, your
359 choices are very limited. The ancient MPEG-1 format with MP2 or PCM
360 audio is probably the only choice that is universally supported.
361 Interoperability with Windows/Mac also comes into play when deciding
362 how to encode and whether to scale to preserve aspect, since popular
363 media player applications for these systems do not honor the aspect
364 ratio encoding stored in MPEG-4 avi files.
365
366
367 II.2. Constraints of DVD, SVCD, and VCD
368
369 Unfortunately, the DVD, SVCD, and VCD formats are subject to heavy
370 constraints. Only a small selection of encoded picture sizes & aspect
371 ratios are available. If your movie does not meet one of these, you
372 must scale and crop or add black borders (which are bad for quality!)
373 to make it compliant.
374
375 Format Resolution V.Codec A.Codec FPS Aspect
376 NTSC DVD 720x480 MPEG-2 AC3,PCM 24,30 4:3,16:9
377 NTSC DVD 352x240 * MPEG-1 AC3,PCM 24,30 4:3
378 NTSC SVCD 480x480 MPEG-2 MP2 30 4:3
379 NTSC VCD 352x240 MPEG-1 MP2 24,30 4:3
380 PAL DVD 720x576 MPEG-2 MP2,AC3,PCM 25 4:3,16:9
381 PAL DVD 352x288 * MPEG-1 MP2,AC3,PCM 25 4:3
382 PAL SVCD 480x576 MPEG-2 MP2 25 4:3
383 PAL VCD 352x288 MPEG-1 MP2 25 4:3
384
385 * These resolutions are rarely used in DVD because they are fairly low
386 quality.
387
388 DVD, VCD, and SVCD also constrain you to relatively low GOP sizes. 18 is
389 supposed to be the largest allowed GOP size for 30 fps NTSCP material;
390 for 25 or 24 fps, the GOP size should be 15.
391
392 VCD video is required to be CBR at 1152 kbps. This highly limiting
393 constraint also comes along with an extremly low vbv buffer size of
394 327 kilobits. SVCD allows varying video bitrates up to 2500 kbps, and
395 a somewhat less insane vbv buffer size of 917 kilobits is allowed. DVD
396 video bitrates may range anywhere up to 9800 kbps (though typical bitrates
397 are about half that), and the vbv buffer size is 1835 kilobits.
398
399 Here is a list of fields in lavcopts that you may be required to change
400 in order to make usable video for VCD, SVCD, or DVD:
401
402 acodec: mp2 for VCD, SVCD, or PAL DVD; ac3 is most commonly used for DVD.
403 PCM audio may also be used for DVD, but this is mostly a big
404 waste of space. Note that mp3 audio isn't spec-compliant for
405 any of these formats, but players often have no problem playing
406 it anyway.
407
408 abitrate: 224 for VCD; user-selectable for DVD and SVCD, but commonly used
409 values range from 192 to 384 kbps.
410
411 vcodec: mpeg1video for VCD; mpeg2video for SVCD; mpeg2video is usually
412 used for DVD but you may also use mpeg1video for CIF resolutions.
413
414 keyint: 18 for 30fps material, or 15 for 25/24 fps material. Commercial
415 producers seem to prefer keyframe intervals of 12.
416
417 vrc_buf_size: 327 for VCD, 917 for SVCD, and 1835 for DVD.
418
419 vrc_minrate: 1152, for VCD. May be left alone for SVCD and DVD.
420
421 vrc_maxrate: 1152 for VCD; 2500 for SVCD; 9800 for DVD. For SVCD and DVD,
422 you might wish to use lower values depending on your own
423 personal preferences and requirements.
424
425 vbitrate: 1152 for vcd; up to 2500 for SVCD; up to 9800 for DVD. For the
426 latter two formats, vbitrate should be set based on personal
427 preference. For instance, if you insist on fitting 20 or so
428 hours on a DVD, you could use vbitrate=400. The resulting
429 video quality would probably be quite bad. If you are trying
430 to squeeze out the maximum possible quality on a DVD, use
431 vbitrate=9800, but be warned that this could constrain you to
432 less than an hour of video on a single-layer DVD.
433
434 Here is a typical minimum set of lavcopts for encoding video for a VCD:
435
436 -lavcopts vcodec=mpeg1video:vrc_buf_size=327:vrc_minrate=1152:\
437 vrc_maxrate=1152:vbitrate=1152:keyint=15:acodec=mp2
438
439 SVCD:
440
441 -lavcopts vcodec=mpeg2video:vrc_buf_size=917:vrc_maxrate=2500:vbitrate=1800:\
442 keyint=15:acodec=mp2
443
444 DVD:
445
446 -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:\
447 vbitrate=5000:keyint=15:acodec=ac3
448
449 For higher quality encoding, you may also wish to add quality-enhancing
450 options to lavcopts, such as trell, mbd=2, and others. Note that qpel
451 and v4mv, while often useful with MPEG-4, are not usable in MPEG-1 or
452 MPEG-2. Also, if you are trying to make a very high quality DVD encode,
453 it may be useful to add dc=10 to lavcopts. Doing so may help reduce the
454 appearance of blocks in flat-colored areas. Putting it all together,
455 here is an example of a set of lavcopts for a higher quality DVD:
456
457 -lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:\
458 vbitrate=8000:keyint=15:trell:mbd=2:precmp=2:subcmp=2:cmp=2:dia=-10:\
459 predia=-10:cbp:mv0:vqmin=1:lmin=1:dc=10
460
461 If your movie has 2.35:1 aspect (most recent action movies), you will
462 have to add black borders or crop the movie down to 16:9 to make a DVD
463 or VCD. If you add black borders, try to align them at 16-pixel
464 boundaries in order to minimize the impact on encoding performance.
465 Thankfully DVD has sufficiently excessive bitrate that you do not have
466 to worry too much about encoding efficiency, but SVCD and VCD are
467 highly bitrate-starved and require effort to obtain acceptable
468 quality.
469
470
471
472
473
474 II.3. Limitations of the AVI container
475
476 Although it's the most widely-supported format after MPEG-1, AVI also
477 has some major drawbacks. Perhaps the most obvious is the overhead.
478 For each chunk of the AVI file, 24 bytes are wasted on headers and
479 index. This translates into a little over 5 MB per hour, or 1-2.5%
480 overhead for a 700 MB movie. This may not seem like much, but it could
481 mean the difference between being able to use 700 kbit/sec video or
482 714 kbit/sec, and every bit of quality counts.
483
484 In addition to gross inefficiency, AVI also has the following major
485 limitations:
486
487 1. Only fixed-fps content can be stored. This is particularly limiting
488 if the original material you want to encode is mixed content, for
489 example a mix of NTSC video and film material. Actually there are
490 hacks that can be used to store mixed-framerate content in AVI, but
491 they increase the (already huge) overhead fivefold or more so they
492 are not practical.
493
494 2. Audio in AVI files must be either constant-bitrate (CBR) or
495 constant-framesize (i.e. all frames decode to the same number of
496 samples). Unfortunately, the most efficient codec, Vorbis, does not
497 meet either of these requirements. Therefore, if you plan to store
498 your movie in AVI, you'll have to use a less efficient codec such
499 as MP3 or AC3.
500
501 With all of that said, MEncoder does not support variable-fps output
502 or Vorbis encoding. Therefore, you may not see these as limitations if
503 MEncoder is the only tool you will be using to produce your encodes.
504 However, it is possible to use MEncoder only for the video encoding,
505 and then use external tools to encode the audio and mux it into
506 another container format.
507
508
509
510
511
512
513
514 III. Basic MEncoder usage
515
516 III.1. Selecting codecs & format
517
518 Audio and video codecs for encoding are selected with the -oac and
519 -ovc options, respectively. The following choices are available,
520 although some may not have been enabled at compiletime:
521
522 Audio Codecs
523 mp3lame Encode VBR or CBR MP3 with LAME
524 lavc Use one of libavcodec's audio encoders
525 pcm Uncompressed PCM audio
526 copy Do not reencode, just copy compressed frames
527
528 Video codecs
529 lavc Use one of libavcodec's video encoders
530 xvid XviD
531 raw Uncompressed video frames
532 copy Do not reencode, just copy compressed frames
533 frameno Used for 3-pass encoding (not recommended)
534
535 Several other video codecs are available, but not recommended. The
536 lavc audio and video encoders have additional suboptions to select
537 which codec to use within lavc. The syntax is:
538
539 -lavcopts acodec=audio_codec_name
540 -lavcopts vcodec=video_codec_name
541
542 Your choices for lavc audio are mp2, ac3, and various adpcm formats
543 (low efficiency). For lavc video, you have many more choices:
544
545 mpeg1video MPEG-1 video
546 mpeg2video MPEG-2 video
547 mpeg4 MPEG-4 video, standards-compliant
548 msmpeg4 Pre-standard MPEG-4 used by MS (aka DivX3)
549 msmpeg4v2 Pre-standard MPEG-4 used by MS (low quality)
550 msmpeg4v1 Pre-standard MPEG-4 used by MS (low quality)
551 wmv1 Windows Media Video, V1 (aka WMV7)
552 wmv2 Windows Media Video, V2 (aka WMV8)
553 dvvideo DV video (used by DV cameras)
554 mjpeg Motion JPEG
555 ljpeg Lossless JPEG
556 ffv1 Lossless FFmpeg video codec #1 (slow)
557 huffyuv A standard lossless codec
558
559 ...and lots more that aren't worth mentioning for most people.
560
561
562
563 III.2. Selecting input file or device
564
565 MEncoder can encode from files or directly from a DVD or VCD disc.
566 Simply include the filename on the command line to encode from a file,
567 or dvd://titlenumber or vcd://tracknumber to encode from a DVD title
568 or VCD track. If you have already copied a DVD to your hard drive and
569 wish to encode from the copy, you should still use the dvd:// syntax,
570 along with -dvd-device followed by the path to the copied DVD root.
571 The -dvd-device and -cdrom-device options can also be used to override
572 the paths to the device nodes for reading directly from disc, if the
573 defaults of /dev/dvd and /dev/cdrom do not work on your system.
574
575 When encoding from DVD, it is often desirable to select a chapter or
576 range of chapters to encode. You can use the -chapter option for this
577 purpose. For example, -chapter 1-4 will only encode chapters 1 through
578 4 from the DVD. This is especially useful if you will be making a 1400
579 MB encode targetted for two CDs, since you can ensure the split occurs
580 exactly at a chapter boundary rather than in the middle of a scene.
581
582 If you have a supported TV capture card, you can also encode from the
583 TV-in device. Use tv://channelnumber as the filename, and -tv to
584 configure various capture settings. DVB input works similarly.
585
586
587 III.3. Loading video filters
588
589 Learning how to use MEncoder's video filters is essential to producing
590 good encodes. All video processing is performed through the filters --
591 cropping, scaling, color adjustment, noise removal, sharpening,
592 deinterlacing, telecine, inverse telecine, and deblocking, just to
593 name a few. Along with the vast number of supported input formats, the
594 variety of filters available in MEncoder is one of its main advantages
595 over other similar programs.
596
597 Filters are loaded in a chain using the -vf option:
598
599 -vf filter1=options,filter2=options,...
600
601 Most filters take several numeric options separated by colons, but the
602 syntax for options varies from filter to filter, so read the man page
603 for details on the filters you wish to use.
604
605 Filters operate on the video in the order they are loaded. For
606 example, the following chain:
607
608 -vf crop=688:464:12:4,scale=640:464
609
610 will first crop the 688x464 region of the picture with upper-left
611 corner at (12,4), and then scale the result down to 640x464.
612
613 Certain filters need to be loaded at or near the beginning of the
614 filter chain, in order to take advantage of information from the video
615 decoder that will be lost or invalidated by other filters. The
616 principal examples are pp (postprocessing, only when it is performing
617 deblock or dering operations), spp (another postprocessor to remove
618 MPEG artifacts), pullup (inverse telecine), and softpulldown (for
619 converting soft telecine to hard telecine).
620
621 Advanced topics in filter chains and usage information for specific
622 filters will follow in chapters IV and V, as they are needed for the
623 topics covered.
624
625
626
627 III.4. Notes on A/V sync
628
629 MEncoder's audio/video synchronization algorithms were designed with
630 the intention of recovering files with broken sync. However they seem
631 to cause unnecessary skipping and duplication of frames, and possibly
632 slight A/V desync, when used with proper input. It is therefore
633 recommended that you switch to basic A/V sync with the -mc 0 option,
634 or put this in your ~/.mplayer/mencoder config file, as long as you
635 are only working with good sources (DVD, TV capture, high quality
636 MPEG-4 rips, etc) and not broken ASF/RM/MOV files.
637
638 If you want to further guard against strange frame skips and
639 duplication, you can use both -mc 0 and -noskip. This will prevent ALL
640 A/V sync, and copy frames one-to-one, so you cannot use it if you will
641 be using any filters that unpredictably add or drop frames, or if your
642 input file has variable framerate! Therefore, using -noskip is not in
643 general recommended.
644
645 The so-called "three-pass" encoding which MEncoder supports has been
646 reported to cause A/V desync. This will definitely happen if it is
647 used in conjunction with certain filters, therefore, it is now
648 recommended NOT to use three-pass mode. This feature is only left for
649 compatibility purposes and for expert users who understand when it is
650 safe to use and when it is not. If you have never heard of three-pass
651 mode before, forget that we even mentioned it!
652
653 There have also been reports of A/V desync when encoding from stdin
654 with MEncoder. Do not do this! Always use a file or CD/DVD/etc device
655 as input.
656
657
658
659
660
661 IV.1. Encoding progressive video
662
663 As long as your input video is progressive (see section I.1),
664
665
666 Let's finally see a few examples:
667
668 Encoding from 2:2 pulldown PAL DVD, title 1
669 2.35:1 picture aspect
670 1200 kbit/sec MPEG-4 video
671 128 kbit/sec average-bitrate MP3 audio
672
673 mencoder dvd://1 -vf crop=712:432,scale=640:288 -mc 0 -oac mp3lame\
674 -lameopts abr:br=128 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=1200
675
676 The crop size was presumably obtained by using the cropdetect filter
677 in MPlayer, or experimenting first with crop rectangles in MPlayer.
678 The output framerate will be 25 fps, the same as the original DVD. It
679 would be preferable to adjust the playback speed to match the original
680 24 fps theatrical rate, but this is not yet possible with MEncoder.
681 The options we pass to libavcodec are the bare minimum, and will yield
682 relatively poor quality. We will refine then in subsequent sections.
683
684 Now, a second example:
685
686 Encoding from soft-telecined NTSC DVD, title 3
687 2.35:1 picture aspect
688 900 kbit/sec MPEG-4 video
689 Keeping the original AC3 audio
690
691 mencoder dvd://1 -vf crop=708:360,scale=640:288 -mc 0 -oac copy \
692 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=900 -ofps 23.976023976
693
694 This example is very similar to the first example, except for the
695 -ofps option to adjust the output framerate. Unless you tell it
696 otherwise, MEncoder takes its output framerate from the input
697 framerate. This is reported as 29.97 fps (actually 30000/1001), or
698 rather, 29.97 pairs of fields per second. But since the DVD is
699 soft-telecined, 1/5 of these fields are not actually present, but
700 intended to be added by the player when it telecines the movie in
701 realtime. There are actually only 23.976 (24000/1001) frames per
702 second. If you leave the framerate at the default, 29.97, it will
703 still work, but every 4th frame will get encoded in duplicate, making
704 the motion appear choppy.
705
706 Finally, a comment on the number 23.976023976. You'll often see
707 recommendations to use -ofps 23.976, but this is wrong. MEncoder will
708 reduce 23.976 to 2997/125, which is not the same as 24000/1001. So in
709 order to get the right framerate written in the output file's header,
710 always use plenty of precision.
711
712
713
714
715 IV.2. Two-pass encoding
716
717 The complexity (and thus the number of bits) required to compress the
718 frames of a movie can vary greatly from one scene to another. Modern
719 video encoders can adjust to these needs as they go and vary the
720 bitrate. However, they cannot exceed the requested average bitrate for
721 long stretches of time, because they do not know the bitrate needs of
722 future scenes.
723
724 Two-pass encoding solves this problem by encoding the movie twice.
725 During the first pass, statistics are generated regarding the number
726 of bits used by each frame and the quantization level (quality) at
727 which it was encoded. Then, when the second pass begins, the encoder
728 reads these statistics and redistributes the bits from frames where
729 they are in excess to frames that are suffering from low quality.
730
731 In order for the process to work properly, the encoder should be given
732 exactly the same sequence of frames during both passes. This means
733 that the same filters must be used, the same encoder parameters must
734 be used (with the possible exception of bitrate), and the same frame
735 drops and duplications (if any) must take place.
736
737 In theory it's possible to use -oac pcm or -oac copy during the first
738 pass to avoid spending time encoding the audio. However, this can
739 result in slight variations in which frames get dropped or duplicated,
740 so it may be preferable to encode the audio during the first pass as
741 well as the second. This also allows you to examine the final audio
742 bitrate and filesize, and to adjust the audio or video bitrate
743 slightly between passes if you don't meet your target size.
744
745 Here is an example:
746
747 Encoding from an existing AVI file
748 500 kbit/sec MPEG-4 video
749 96 kbit/sec average-bitrate MP3 audio
750
751 mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
752 abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=1
753
754 mencoder bar.avi -vf scale=448:336 -mc 0 -oac mp3lame -lameopts \
755 abr:br=96 -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=500:vpass=2
756
757 If you do not want to overwrite the output from the first pass when
758 you begin the second, you can use the -o option to choose a different
759 output filename. Note the addition of the vpass option in this
760 example. If vpass is not specified, single-pass encoding is performed.
761 If vpass=1, a log file is written with statistics from the first pass.
762 If vpass=2, the log file is read and the second pass is encoded based
763 on those statistics. If you are short on disk space or don't want the
764 extra disk wear from writing the file twice, you can use -o /dev/null
765 during the first pass. However, sometimes it is beneficial to watch
766 the first-pass file before beginning the second pass to make sure
767 nothing went wrong in the encoding.
768
769 Next, an example using XviD instead of libavcodec:
770
771 Encoding from an existing AVI file
772 500 kbit/sec MPEG-4 video
773 Copying the existing audio stream unmodified
774
775 mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
776 -xvidencopts bitrate=400:pass=1
777
778 mencoder foo.avi -vf scale=320:240 -mc 0 -oac copy -ovc xvid \
779 -xvidencopts bitrate=400:pass=2
780
781 The options used are slightly different, but the process is otherwise
782 the same.
783
784
785
786
787 IV.3. Encoding interlaced video
788
789 If the movie you want to encode is interlaced (NTSC video or PAL
790 video), you will need to choose whether you want to deinterlace or
791 not. While deinterlacing will make your movie usable on progressive
792 scan displays such a computer monitors and projectors, it comes at a
793 cost: the field rate of 50 or 59.94 fields per second is halved to 25
794 or 29.97 frames per second, and roughly half the information in your
795 movie will be lost during scenes with significant motion.
796
797 Therefore, if you are encoding for high quality archival purposes, it
798 is recommended not to deinterlace. You can always deinterlace the
799 movie at playback time when displaying it on progressive scan devices,
800 and future players will be able to deinterlace to full fieldrate,
801 interpolating 50 or 59.94 entire frames per second from the interlaced
802 video.
803
804 Special care must be taken when working with interlaced video:
805
806 1. Crop height and y-offset must be multiples of 4.
807
808 2. Any vertical scaling must be performed in interlaced mode.
809
810 3. Postprocessing and denoising filters may not work as expected
811 unless you take special care to operate them a field at a time, and
812 they may damage the video if used incorrectly.
813
814 With these things in mind, here is our first example:
815
816 mencoder capture.avi -mc 0 -oac lavc -ovc lavc -lavcopts \
817 vcodec=mpeg2video:vbitrate=6000:ilmv:ildct:acodec=mp2:abitrate=224
818
819 Note the ilmv and ildct options.