MPEG-2 encoding with ffmpeg

I’ve done some significant experimentation with different options for encoding MPEG-2 video from a live video source with ffmpeg.  The objective was to create video that was approximately 3 Mbps at a “decent” resolution; this video would be manually edited and then converted to Web quality.  By “decent”, I mean a high enough resolution so that the video can survive the transcode to Web resolutions without too many re-encoding artifacts.   When encoding a live source, performance is critical; if ffmpeg can’t keep up with the live frames being captured, you may as well not even try to encode the video; your live capture buffers will quickly fill up and bring down your encoding system.  Obviously, you can’t use any sort of two-pass encoding, either.

In my tests of real-time encoding, I encountered one particular transition that was very difficult for ffmpeg to encode without artifacts.  I use this as a worst-case scenario for testing various settings of the MPEG-2 encoder.

Here’s the command line I started with:

ffmpeg -i test.mp4 \     -vcodec mpeg2video -pix_fmt yuv420p -me_method epzs -threads 4 \     -r 29.97 -g 15 -s 704x396 -b 2500k -bt 300k \     -acodec mp2 -ac 2 -ab 192k -ar 44100 \     -async 1 \     -y \     -f vob output.mpg

First, we’ll go through these options and then we’ll talk about what I changed to get better results.

Video options

Let’s go through the various options to control the video encoding.

-vcodec mpeg2video

This instructs ffmpeg to use MPEG-2 video encoding

-pix_fmt yuv420p

For convenience, I wanted to be able to play the video in QuickTime (with the MPEG-2 Playback Component). I found that QuickTime could not play yuv422p MPEG-2 (YUV 4:2:2 Planar), but it can handle yuv420p (YUV 4:2:0 Planar)

-me_method epzs

ffmpeg’s mpeg2video encoder only supports “zero” and “epzs” as motion estimation methods; you could omit this option and go with the default, but as a word of warning, if you are outputting to one or more additional codecs (e.g. MPEG-4), you might be using another motion estimation method, and without an explicit motion estimation method specified for the mpeg2video encoder, ffmpeg will try to use the same motion estimation method used by the other codec, and it will fail.  Better to be safe here and explicitly specify the me_method.

-threads 4

ffmpeg’s mpeg2video encoder does not do automatic thread detection, so I set this to 4 because I’m working on a 4-core machine; you could omit this option, but if you’re outputting to one or more codecs, and you’re using -threads 0 with another codec, ffmpeg will try to use the automatic thread detection on the MPEG-2 output, and it will fail.

-r 29.97

MPEG-2 must be either 25 or 29.97 frames per second; no other frame rates will work.

-g 15

use a GOP size of 15, a very standard GOP size

-s 704x396

Output a frame size of 704×396

-b 2500k -bt 300k

2.5 Mbps with tolerance of 300k; the mpeg2video codec requires that the tolerance be “large enough” for the bitrate (so you can’t set a 10k tolerance on a 3Mbps bitrate, for example); a larger tolerance had some effect on video quality, but nothing near the -bf 2 effect.

Audio options

Now a look at the audio options:

-acodec mp2

use MPEG-1 layer II audio (you could use ac3, mp3, or libfaac instead, but that might cause some compatibility issues — I don’t believe that DVD VOBs can contain AAC audio, for example)

-ac 2 -ab 192k -ar 44100

Output 2-channnel audio at 192Kbps and a 44100 Hz sampling rate.

-async 1

Use audio/video sync method 1 (I always use this audio sync method).

Output options

Finally, have a look at the output options:

-f vob 'test-vob-b3000-bt500-704x396-yuv420p-g45-bf2-trellis2-cmp2.mpg'

Use the VOB output format; don’t be tempted to use the mpeg2video format — you won’t get any audio track.

Improving the quality

Now lets look at how we can improve the quality, especially during that horrible transition.  The command-line options here are the result of a lot of experimentation.

B-frames

Adding B-frames seemed to give the most bang for the buck.

-bf 2

this inserts two B-frames between each P frame; ffmpeg’s default is to use no B-frames at all; Using B-frames made very noticeable differences in video quality while also reducing file size (many artifacts were removed).

Rate distortion optimal quantization -trellis 2

rate-distortion optimal quantization; this cleans up the bulk of the artifacting that remained after enabling B-frames.

Pel ME Compare Function -cmp 2 -subcmp 2

Explicitly specify the full pel me compare function, sub pel me compare function; this seems to pick up any tiny bits of artifacting after -trellis 2; important to note that on its own, this does not make the video quality improvement that -trellis 2 does.

GOP Size -g 45

use a GOP size of 45; this made a noticeable improvement in the quality.  The impact is comparable to the impact I saw from adding -trellis 2 to the command-line options.

I’ve seen recommendations about going up to -g 100, but I’ve also read that some decoders might have trouble with very large GOP sizes, which could make it hard/impossible to use the resulting video with some editors.

Final command-line

Here is the final command I used:

ffmpeg -i test.mp4 \     -vcodec mpeg2video -pix_fmt yuv420p -me_method epzs -threads 4 \     -r 29.97 -g 45 -bf 2 -trellis 2 -cmp 2 -subcmp 2 -s 704x396 -b 2500k -bt 300k \     -acodec mp2 -ac 2 -ab 192k -ar 44100 \     -async 1 \     -y \     -f vob output.mpg

Sursa
2011-03-31 21:46:04



Comenteaza





Ultimele 25 posturi adăugate

19:40:10Fără Titlu —» Путепроводные Заметки
17:34:29Fără Titlu —» Путепроводные Заметки
13:50:00Gardă nocturnă —» Andrei LANGA. Blogul personal
04:10:25Fără Titlu —» Путепроводные Заметки
03:34:00POEME PRIN ANI —» Leo Butnaru
20:35:49Fără Titlu —» Путепроводные Заметки
18:21:16Fără Titlu —» Путепроводные Заметки
16:48:26Fără Titlu —» Путепроводные Заметки
15:06:56Fără Titlu —» Путепроводные Заметки
09:40:48Cinci zile de știință și inovație, la Congresul Mondial al Vinului —» Fine Wine
04:47:00DIN REVISTA TINERILOR —» Leo Butnaru
17:26:00ADIO, MAESTRE EUGEN DOGA (1.III.1937 – 3.VI.2025)! —» Leo Butnaru
14:09:18Fără Titlu —» Путепроводные Заметки
09:04:36Nou: Chateau Cristi Chardonnay 2024 și Sauvignon Blanc 2024 —» Fine Wine
05:26:00POEME PRIN ANI —» Leo Butnaru
16:39:17Fără Titlu —» Путепроводные Заметки
12:42:27Fără Titlu —» Путепроводные Заметки
12:34:07Fără Titlu —» Путепроводные Заметки
11:17:0031 mai – Ziua Mondiala fără tutun —» Biblioteca Publică or.Rîşcani
05:18:00DIALOG LA „HYPERION” —» Leo Butnaru
18:05:00Umbre —» Andrei LANGA. Blogul personal
13:05:05Fără Titlu —» Путепроводные Заметки
06:34:52Hai la Caravana Poveștilor —» Biblioteca de Arte 'Tudor Arghezi'
06:15:00DIN YES-EURI —» Leo Butnaru
03:52:38Fără Titlu —» Путепроводные Заметки