How to make an MPEG2 video file with the highest quality possible using FFMPEG?

83,044

The problem is that the default bitrate for the MPEG-2 is rather low (as with most other video encoders in ffmpeg, the H.264 one being an exception). MPEG-2 is also not the best choice as a codec these days.

Better quality for MPEG-2

You have a few options if you want to stick with MPEG-2:

  • Increase the bitrate. You're now using -b:v 2500k. If it's HD video, you will not get far with only 2.5 MBit/s. You need at least double that or even more to make the result look good. For example, use -b:v 6000k -target pal-dvd.

    For 720p, I think that you should still use a higher bitrate. Remember that DVDs use MPEG-2 and come in about 4.7 GB for 2hrs of movie, so you end up with around 5–8 MBit/s. MPEG-2 is really not very compression-efficient and works better at higher bitrates.

  • Use a specific quality setting. Change -b:v … to -qscale:v 2. The number here ranges from 1 to 31 and higher means lower quality. There's no point going beyond 4 or 5. If you don't care for the bitrate start with 2 and see if that works for you.

Messing with the number of B-frames, motion estimation method or GOP size may tweak the quality a little but won't result in big changes.

Silent audio

Use -f lavfi -i aevalsrc=0 to generate a silent audio stream. For example:

ffmpeg -i "in.wmv" -f lavfi -i aevalsrc=0 -shortest -c:v mpeg2video -qscale:v 2 -c:a libmp3lame "out.mpg"

You may need to add -target pal-dvd to the above command to force a certain buffer size.

I chose MP3 as codec. MPEG files cannot contain audio other than MPEG Layer I and II audio as well as PCM streams, so using a silent Ogg Vorbis file will not work unless you convert the audio stream as well (which is not what you're doing when you use -c:a copy).

Use a different video codec

I'm surprised that a TV that plays video files will read MPEG-2 but not anything else. At least MPEG-4 Part II video should be supported (that's what you know as "DivX" – an MPEG-4 Part II encoder). So you could try:

ffmpeg -i "in.wmv" -f lavfi -i aevalsrc=0 -shortest -c:v libxvid -qscale:v 2 -c:a libmp3lame "out.mp4"

Your TV might actually also support H.264, but only a certain profile. Try using the baseline profile, for example:

ffmpeg -i "in.wmv" -f lavfi -i aevalsrc=0 -shortest -c:v libx264 -profile:v baseline -crf 23 -c:a aac -strict experimental "out.mp4"

In the above example I've used the CRF option to set the quality instead of qscale. See the H.264 encoding guide for more.

Share:
83,044

Related videos on Youtube

Ivan
Author by

Ivan

Updated on September 18, 2022

Comments

  • Ivan
    Ivan almost 2 years

    I have got a WMV (v9 (WMV3), 960x720, 30.000030 fps, planar 4:2:0 YUV, produced by PowerPoint 2010) file and need to convert it to MPEG2 - the only format my TVset can read from an USB flash drive (I have also tried MP4/h.264, AVI/XVID - nothing but MPEG2 works).

    I have managed to to the job with simple

    ffmpeg -i "in.wmv" -c:v mpeg2video "out.mpg"
    

    but the quality of the result is dreadful (clearly visible visual distortions are introduced) and the playback is not smooth (too slow at some moments).

    I have also tried

    ffmpeg -i "in.wmv" -c:v mpeg2video -pix_fmt yuv420p -me_method epzs -threads 4 -r 30.000030 -g 45 -bf 2 -trellis 2 -cmp 2 -subcmp 2 -s 960x720 -b 2500k -bt 300k -async 1 -y "out.mpg"
    

    (I have found this somewhere in the Internet and modified a little bit - changed the resoultion, the refresh rate and the output format (from VOB to bare MPG)) succesfully but the quality is still too bad.

    What parameters do I have to use to save as much quality as possible? Compression ratio doesn't matter at all, even increase in file size is acceptable.

    Anther thing I actually need (I have chosen not to include it in the question title to avoid making it too specific but I'd appreciate it being considered in the answers) is adding pure silence as a sound track - there is no sound in the original but the TV set complains about it and I'd like to get rid of this complain. I have generated a same-length (second-precise) silence OGG Vorbis file using Audacity but I can't manage to merge it with the video:

    ffmpeg -i in.mpg -i silence.ogg -c:v copy -c:a libmp3lame out.mpg
    

    and even bare

    ffmpeg -i in.mpg -c:v copy -out.mpg
    

    gives "buffer underflow" and "packet too large" errors. (in.mpg are the files produced by the same FFMPEG binary and the same source WMV file using the first two commands in the question).

    I am using a Zeranoe FFMPEG build on Windows 7.

  • slhck
    slhck over 9 years
    Setting the duration is not necessary, as ffmpeg will just use whatever is necessary for the video.
  • dstob
    dstob over 9 years
    I kept things to the two step process as Ivan had done. If you leave out the duration in my command then I do not know how long ffmpeg would run (likely longer than you would want).
  • Ivan
    Ivan over 9 years
    "Messing with the number of B-frames, motion estimation method or GOP size may tweak the quality a little but won't result in big changes." - may I just make every frame a B-frame and use no motion estimation with MPEG2? The video has a lot of VERY dynamic parts.
  • Ivan
    Ivan over 9 years
    I have tried the first command line you suggest (ffmpeg -i "in.wmv" -f lavfi -i aevalsrc=0 -shortest -c:v libxvid -qscale:v 2 -c:a libmp3lame "out.mp4") - it encodes about 100 frames and then starts flooding the screen with errors like [mpeg @ 02f69ba0] buffer underflow i=0 bufi=235538 size=239761 and [mpeg @ 02f69ba0] packet too large, ignoring buffer limits to mux it.
  • slhck
    slhck over 9 years
    @Ivan You change the bitrate with -b:v. Added it to my answer. Regarding the encoder settings: You can't make every frame a B-frame. Also, you need motion estimation – the more exhaustive the method, the better the compression. But if it's a dynamic video, the first thing you will want is increase the bitrate significantly. The encoding settings really do not have much of an impact. As for your error, are you sure this is the error to the correct command? An [mpeg] error can only happen if your output file is .mpg, not when it's .mp4.
  • Ivan
    Ivan over 9 years
    I have copy=pasted a wrong part, @slhck, I am using ffmpeg -i "in.wmv" -f lavfi -i aevalsrc=0 -shortest -c:v mpeg2video -qscale:v 2 -c:a libmp3lame "out.mpg" of course.
  • slhck
    slhck over 9 years
    @Ivan I couldn't reproduce that, but try setting -target pal-dvd to force a buffer size, or use -bufsize 5M -maxrate 5M (or whatever bitrate you're using).
  • Ivan
    Ivan over 9 years
    -target pal-dvd solves the problem, -bufsize dosn't work (I have tried -b:v 7M -bufsize 7M -maxrate 7M).
  • slhck
    slhck over 9 years
    @Ivan Glad it worked. Thanks for reporting back.
  • Ivan
    Ivan over 9 years
    I can consider the problem solved although playback is visibly slow at the most dynamic moments when using the TV and somewhat better when using an HDMI-attached DVD&USB player (and perfect on the PC). I am going to try decreasing the resolution to gain speed. Thank you for a great detailed answer.
  • Ivan
    Ivan over 9 years
    By the way I have also managed to get it playing h.264 - just a bit of patirnce was needed, it was slower to load. I have an identical picture in WMV (PC only), h.264 and MPEG2 now.
  • Royi
    Royi over 7 years
    Anyway to create lossless video in MPEG2?
  • slhck
    slhck over 7 years
    @Royi Briefly checking it, it does not seem to be possible with ffmpeg. You can only set qscale to 1 which is still lossy. Any reason why this is your use case instead of using, e.g. lossless HuffYUV or other, more common codecs?
  • Royi
    Royi over 7 years
    I actually just having troubles with FFMPEG on Windows. I have qtrle video in MOV Container (Creates on OS X). I tried ffmpeg -i DL126H.mov -c copy VideoClip0002.mp4 I get Could not find tag for codec qtrle in stream #0, codec not currently supported in container.
  • slhck
    slhck over 7 years
    @Royi Please ask a new question and include the full, uncut command line output. Ideally, add a sample video as well.
  • Royi
    Royi over 7 years
    @slhck, I did - superuser.com/questions/1188546/…. Thank You.