1001 tips for encoding videos with mplayer/mencoder


General Tips

  • Using "harddup" at the end of the filter chain will help A/V sync. It will copy frames when in the original there is nothing to stay in sync with the original. It results in a slightly bigger file, but improves demuxing and remuxing.
  • NOT using -ofps (the option that sets the output frame rate) prevents the output result to have artifacts during the first seconds of the video (to be exact it will have artifacts (grey pixels) until it reaches the first keyframe)
  • Keeping an audio track during the video pass fixes pretty much all sync issues except for wmv, h264 and any other codec that allows variable frame rate. In those cases using "-mc 0" fixes the sound sync. '(adding "-mc 0" for non variable frame rate codecs screws up some contents !)'

A set of good parameters for mencoder

bFrame = 3 
me = umh
search range -> default (for SD)
ref =~ 5
trelis = 1
bPiramid -> ok
CRF (quality detection):
- 18 (you won't see the difference)
- 24 (you start to see the difference)
Deinterlace with yadif or mcdint
De-telecine with pullup
And you can pretty much bump up all the other parametters you want to use...

Downsampling the audio to 2 channels

To go from Surround sound (or anything that has more than 2 channels) to 2 channels you can use mplayer with the "-af pan" option. AAC or DTS needs a matrix for downsampling to 2 channels.

Frame Rate

It is recommended to use fraction instead of numbers (24000/1001 rather than 23.976). wmv videos have a variable frame rate. The frame rate reported in the header of the file is 1000fps (cause it doesn't know) so be carefull if you're detecting it via midentify...

Interlaced Videos

An interlaced video is a video that has half of the displayed image on each frame. So you need two frames to recreate the image. You can choose to deal with this on the playback side (the most conservative way) or you can choose to De-Interlace that video. It's recommended to deinterlace at playback for maximum quality, but be carefull, if you choose to go with deinterlacing because it loses half of the movie info. For example on a video with a frame rates of 50 (or 6000/1001) it would be lowered down to 25 (or 3000/1001).

In order to be able to deinterlace a video you have to comply to the following rules:

  • If you want to crop, the crop height and y-offset (vertical position) must be multiples of 4
  • If you want to scale the video, you must scale it before deinterlacing !
  • Postprocessing and denoising filters may not work (mess up your video) or degrade the quality

Extracting the audio track

You can extract the audio track from a video into a wav file with mplayer.

Just set the video output to null and the audio driver to PCM (the wave format)

mplayer -vo null -ao pcm:file=my_audio_track.wav 

Don't use "-vc dummy". Some forums decommend it but it makes the audio drop (stop, no more sound) before the end sometimes.

A/V sync

(DO NOT encode from STDIN !)

mencoder is made to fix A/V sync issues of the input file, but if that doesn't work (mostly when the original has a variable frame rate) you can use "-mc 0" (it sets the 'M'aximum 'C'orrection to 0). If you want to further guard against strange frame duplications and skips you can use "-mc 0 -noskip". This prevents all A/V sync and copies frames one-by-one so you can NOT use any filter that drops or duplicate frames or if the input has variable bitrate.

When Encoding Audio & Video Separately

Do NOT use "-nosound" when encoding the video pass:

  • it helps A/V sync (thanks to the audio track in your video, the frames aren't drop where they shouldn't to keep sound sync.)
  • avoids getting "too many audio packets..." error.
  • helps mencoder to know when to duplicate/drop frames

Click to Rate This Article