MEncoder: Difference between revisions

From LinuxTVWiki
Jump to navigation Jump to search
 
(8 intermediate revisions by 4 users not shown)
Line 9: Line 9:
* [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).
* [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 ==
== 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.
* Example scripts:
** [[V4L channel script]]
** [[V4L capture script]]


=== Capturing VHS video ===
=== Dumping a DVB stream to disk ===
To dump a DVB stream to disk, type:
"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."


$ mencoder -ovc copy -oac copy -o stream.ts dvb://"STREAM NAME"
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.


This will copy the video (-ovc) and audio (-oac) streams to the file ''stream.ts''.
Ignore people who tell you to try -vf harddup, -fps, -ofps, -noskip, -mc or killing MEncoder before it finishes. It won't work. I've tried '''many''' combinations.


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


$ mencoder -forceidx -vf scale=640:-2 -ovc xvid -xvidencopts bitrate=800 -oac mp3lame -lameopts cbr:br=128 stream.ts -o output.avi
In the end, I found a line that keeps sync, a very basic line really:


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

== Working with Analogue TV ==


=== Do not use mplayer to record VHS/camcorder tapes ===
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.

Analogue recordings often have strange issues like frame rates that vary from second to second. Workarounds like <code>-vf harddup</code>, <code>-fps</code>, <code>-ofps</code>, <code>-noskip</code>, <code>-mc</code> etc. can't overcome this fundamental obstacle, so <code>mplayer</code> will ''always'' produce desynchronised audio and video. To get high quality video from tapes, see [[V4L_capturing|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.

=== Example scripts ===
* [[V4L channel script]]
* [[V4L capture script]]


=== Mencoder usage on a slow x86 ===
=== Mencoder usage on a slow x86 ===
Line 65: Line 73:


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).
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 ===
=== Correcting NTSC framerate ===
Line 98: Line 97:
For unofficial packages of mencoder for Debian, see [http://debian.video.free.fr/ Christian Marillat's Debian Repositories].
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.
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 ===
== 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.


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.
=== Dumping a DVB stream to disk ===
To dump a DVB stream to disk, type:


Assuming that ivtv driver was loaded first, and then vivid, we have two devices:
$ mencoder -ovc copy -oac copy -o stream.ts dvb://"STREAM NAME"


* /dev/video1, for vivid capture
This will copy the video (-ovc) and audio (-oac) streams to the file ''stream.ts''.
* /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:
=== 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.


mencoder tv:// -v -tv driver=v4l2:width=720:height=576:norm=PAL:input=1:device=/dev/video1:immediatemode=0:forceaudio:outfmt=rgb24 \
Example: To transcode a dump to XviD 800kbps video and MP3 128kbps audio, type:
-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 \
$ mencoder -forceidx -vf scale=640:-2 -ovc xvid -xvidencopts bitrate=800 -oac mp3lame -lameopts cbr:br=128 stream.ts -o output.avi
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
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==
==External Links==
* [http://www.ee.oulu.fi/~tuukkat/mplayer/tests/x264test4/readme.html x264 encoder threading benchmarks]
* [http://www.ee.oulu.fi/~tuukkat/mplayer/tests/x264test4/readme.html x264 encoder threading benchmarks]
* [http://linuxreviews.org/man/mencoder/ MEncoder man page] (''Linux Reviews'')





Latest revision as of 10:08, 17 February 2016

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 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 -vf harddup, -fps, -ofps, -noskip, -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.

Example scripts

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

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=720:height=576:norm=PAL: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

External Links