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
- 1 Useful guides
- 2 Working with Digital TV
- 3 Working with Analogue TV
- 4 External Links
- Encoding with MEncoder chapter in the MPlayer - The Movie Player documentation (other languages are available).
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).
Working with Analogue TV
Do not use mplayer to record VHS/camcorder tapes
Analogue recordings often have strange issues like frame rates that vary from second to second. Workarounds like
-mc etc. can't overcome this fundamental obstacle, so
mplayer will always produce desynchronised audio and video. To get high quality video from tapes, see V4L capturing.
These problems are uncommon for digital video sources (e.g. games consoles and DVD players), and are handled automatically by expensive cards with MPEG2 encoders (such as a Hauppauge PVR-150/250-350). If you're in that situation, the following might work acceptably well for you.
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%
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.
Using a PVR-350 to output TV signal from the vivid driver
Sometimes, it is interesting to generate test signals for debugging signal input on other boards. One way of doing that is to use both ivtv and vivid driver together.
Assuming that ivtv driver was loaded first, and then vivid, we have two devices:
- /dev/video1, for vivid capture
- /dev/video16, for composite/s-video mpeg output
With such config, we can use mencoder to generate a PAL signal at the PVR-350 TV output using:
mencoder tv:// -v -tv driver=v4l2:width=640:height=480:input=1:device=/dev/video1:immediatemode=0:forceaudio:outfmt=rgb24 \ -nosound -noaspect -noautoexpand -nosub -of mpeg -ofps 25 -srate 48000 -ovc lavc -oac lavc -vf scale=720:576,harddup \ -af lavcresample=48000,channels=2 -mpegopts format=dvd:tsaf:vaspect=4/3 -lavcopts \ acodec=ac3:abitrate=192:vcodec=mpeg2video:vbitrate=8000:mbd=2:trell:vrc_maxrate=9000:vrc_buf_size=1800:keyint=15:vstrict=0:aspect=4/3:vpass=1:turbo \ -o /dev/video16