MEncoder is a very powerful tool which is part of the MPlayer software package. It is made from the same code base as MPlayer, and so can read every file format supported by MPlayer.
MEncoder can, for example:
- Re-encode video streams and files
- Demux streams
- Clean streams from errors that occured in the transmission
- Encoding with MEncoder chapter in the MPlayer - The Movie Player documentation (other languages are available).
Working with Analogue TV
Capturing VHS video
"Hi, I'm John. I have a BT878/SAA7134 or similar based analog capture card and would like to digitize my old VHS/camcorder tapes using MEncoder."
I'm going to save you some time here: forget about it! Explanation: that may sound harsh, but it's the truth. When using unstable sources (like analog terrestrial television, tapes, etc), TV-cards tend to drop frames when the signal is unstable.
Ignore people who tell you to try -vf harddop, -fps, -ofps, -noskip, -mc or killing MEncoder before it finishes. It won't work. I've tried many combinations.
This means MEncoder is simply NOT SUITABLE for capturing such sources. Capturing from a videogame console, DVD-player or DVB-box may work (as in theory they should never have an unstable signal), but unstable sources cannot be captured (with A/V sync) using MEncoder.
A card with hardware MPEG2-encoder like a Hauppauge PVR-150/250-350 can work around this problem as the trouble of synchronizing is moved from software to the MPEG encoder/muxer on the hardware of the card.
In the end, I found a line that keeps sync, a very basic line really:
mencoder -tv driver=v4l2:norm=pal:input=1:width=352:height=576:buffersize=512:audiorate=48000:amode=0:alsa tv:// -oac copy -ovc copy -ofps 25 -o capturefile
But this stutters, it keeps skipping frames. Cutting the resolution in half does not help. Adding -noskip fixes this and produces a perfectly fluent video (so the performance appears suffient), but there goes your audio sync, out of the window. If you are thinking about capturing unstable sources using Linux, please, please get a PVR-card. I've wasted a lot of time on this so you don't have to.
Mencoder usage on a slow x86
This is the command line I use for recording PAL (UK), using a simple capture card, and an old PC (PIII 700mhz):
sudo nice --10 mencoder tv:// -v -tv \ driver=v4l2:width=576:height=576:input=1:device=/dev/video0:immediatemode=0:forceaudio:outfmt=yv12 \ -o outfile.avi -ovc lavc -lavcopts vcodec=mjpeg:aspect=4/3 -aspect 4:3 -noautoexpand -oac pcm \ -endpos 00:30:00
I use sudo to ensure that the recording process gets priority over most other processes (via the nice --10). The width of 576 is the maximum that my box can process without dropping frames. The width isn't as important as the height, as the capture card should be able to convert it from 768 -> 576 without losing quality. All the options used are tuned for speed, without dropping too much quality.
If this still takes more cpu speed than you've got, but you've got lots of disk space, you can capture the raw video and uncompressed audio. Substitute these options:
-ovc raw -vf format=yv12 -oac pcm
Once captured I re-encode, as mjpeg with the above options will use around 4gig of disk space per hour of recording. Here's the re-encode line I use, but you'll probably want to play around with it to get what you want. This part of the process can take as long as necessary, and typically takes my box about 16 hours to process a 2 hour plus capture (with a double pass):
nice -+19 mencoder infile.avi -ffourcc DIVX -ovc lavc \ -lavcopts vcodec=mpeg4:vbitrate=1000:mbd=2:v4mv:dia=4:vpass=1:turbo:autoaspect \ -vf crop=560:432:12:72 -oac mp3lame -lameopts cbr:br=128:mode=0 -o /dev/null
nice -+19 mencoder infile.avi -ffourcc DIVX -ovc lavc \ -lavcopts vcodec=mpeg4:vbitrate=1000:mbd=2:v4mv:dia=4:vpass=2:autoaspect \ -vf crop=560:432:12:72 -oac mp3lame -lameopts cbr:br=128:mode=0 -o outfile.new.avi
Using 2 passes improves quality and ensures the target bitrate is achieved within very close tolerances. It's possible to do more passes by using vpass=3 (check the man page on this), but this only helps with very short encodes of a few seconds or less. Don't bother unless you have a very short clip and a very strict bitrate target. In order to improve quality and efficiency, you should also crop the black bars at the top/bottom and sides of your captured image by adding an appropriate "-vf crop" option. Your ideal crop parameters may be different from the one above. A good way of finding out what crop= options to use is:
mplayer -ss 60 -vf cropdetect
This starts viewing at the 60 second mark, and outputs to the terminal the crop= parameters required:
crop area: X: 7..575 Y: 72..503 (-vf crop=560:432:12:72) ??,?% 0 0 86%
Specifying an ALSA device other than default
To determine which sound devices you have on your system, use
The devices you can use for capture will have the word "capture" in their description. Note that you will have to "mangle" the device name in order for mencoder to accept it: since the tv:// options in mencoder are separated by colons, you can't call a PCM device hw:1.0 - the correct form is
Correcting NTSC framerate
In some recent versions of mencoder, the default framerate for ntsc with the following command comes out as 29 instead of 29.97:
mencoder -tv driver=v4l2:width=720:height=480:norm=ntsc -ovc lavc -lavcopts \ vcodec=mpeg4:mbd=2 -oac mp3lame -lameopts cbr:br=128 -endpos 30 -o outfile.avi \ tv:// > /dev/null
This is using Christian Marillat's unofficial package of mencoder for Debian amd64, mencoder_1.0-pre7-0.0_amd64.deb and may or may not apply to other versions.
The fact that the framerate is slightly off is not noticeable in playback, and has been the case for at least three months, from January 2005. What is new is that the latest mencoder version usefully reports "Skipping frame!" once a second during recording. The workaround is to set the framerate explicitly with -fps 30000/1001.
In addition, in the most recent version of mencoder, the default codec for mpeg is FMP4, while it used to be DIVX. To reset it to DIVX, use the -ffourcc DIVX option.
The full command gives great results as before, overriding the poor defaults:
mencoder -tv driver=v4l2:width=720:height=480:norm=ntsc -ffourcc DIVX \ -fps 30000/1001 -ovc lavc -lavcopts vcodec=mpeg4:mbd=2 -oac mp3lame -lameopts \ cbr:br=128 -endpos 30 -o outfile.avi tv:// > /dev/null
Note that using "mbd=2" (high quality macroblock decision algorithm) is slower than "mbd=1" -- the latter is the same as "vhq" (deprecated).
For unofficial packages of mencoder for Debian, see Christian Marillat's Debian Repositories.
I don't have a TV card on a x86 machine or other platforms and can't report on whether the same tweaks are required there.
Working with Digital TV
While it is possible to encode on-the-fly DVB streams, this requires a very fast computer and is processor intensive. It is generally better to dump the DVB stream to disk in MPEG-TS format, and then clean up the stream afterwards.
Dumping a DVB stream to disk
To dump a DVB stream to disk, type:
$ mencoder -ovc copy -oac copy -o stream.ts dvb://"STREAM NAME"
This will copy the video (-ovc) and audio (-oac) streams to the file stream.ts.
Transcoding the file to XviD
An MPEG-TS dump of a DVB stream is extremely large (~1GB for 30 minutes). MEncoder will also allow you to transcode the stream dump into something more useful, like XviD.
Example: To transcode a dump to XviD 800kbps video and MP3 128kbps audio, type:
$ mencoder -forceidx -vf scale=640:-2 -ovc xvid -xvidencopts bitrate=800 -oac mp3lame -lameopts cbr:br=128 stream.ts -o output.avi
This command also forces an index to be created (-forceidx) and scales (-vf scale=640:-2) the image to a width of 640 (the height is calculated using the pre-scaled aspect ratio).