MEncoder: Difference between revisions

From LinuxTVWiki
Jump to navigation Jump to search
(Added examples of recording and transcoding, and tidying up in general)
(merged the relevant info from the "MEncoder-V4L" article; for user contributions see the archived "V4L:MEncoder-V4L" article)
Line 1: Line 1:
'''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''' 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:
MEncoder can, for example:
* [[Re-encode]] video streams and files
* [[Re-encode]] video streams and files
* [[Demux]] streams
* [[Demux]] streams
* [[Clean]] streams from errors that occured in the transmission
* [[Clean]] streams from errors that occured in the transmission


== Recording DVB streams ==
== Useful guides ==
* [http://mplayerhq.hu/DOCS/HTML/en/mencoder.html Encoding with MEncoder] chapter in the [http://mplayerhq.hu/DOCS/HTML/en/index.html MPlayer - The Movie Player] documentation (other languages are available).


== Working with Analogue TV ==
* Example scripts:
** [[V4L channel script]]
** [[V4L capture script]]

=== 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%

For more advice on using mencoder I recommend taking a look at the [http://mplayerhq.hu/DOCS/HTML/en/mencoder.html Encoding with MEncoder] chapter in the [http://mplayerhq.hu/DOCS/HTML/en/index.html MPlayer - The Movie Player] documentation (other languages are available).

=== Specifying an ALSA device other than default ===
To determine which sound devices you have on your system, use

cat /proc/asound/pcm

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

adevice=hw.1,0

=== 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 [http://debian.video.free.fr/ 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.
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:
To dump a DVB stream to disk, type:


Line 16: Line 92:
This will copy the video (-ovc) and audio (-oac) streams to the file ''stream.ts''.
This will copy the video (-ovc) and audio (-oac) streams to the file ''stream.ts''.


== Transcoding to XviD ==
=== 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.
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.


Line 25: Line 100:


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).
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).

==External Links==
* [http://www.ee.oulu.fi/~tuukkat/mplayer/tests/x264test4/readme.html x264 encoder threading benchmarks]



[[Category:Software]]
[[Category:Software]]

Revision as of 20:14, 28 November 2008

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

Useful guides

Working with Analogue TV

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%

For more advice on using mencoder I recommend taking a look at the Encoding with MEncoder chapter in the MPlayer - The Movie Player documentation (other languages are available).

Specifying an ALSA device other than default

To determine which sound devices you have on your system, use

 cat /proc/asound/pcm

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

  adevice=hw.1,0

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).

External Links