https://wiki.archlinux.org/api.php?action=feedcontributions&user=Numbr6&feedformat=atomArchWiki - User contributions [en]2024-03-29T07:20:39ZUser contributionsMediaWiki 1.41.0https://wiki.archlinux.org/index.php?title=MEncoder&diff=204407MEncoder2012-06-11T00:01:17Z<p>Numbr6: Fix missing preme= argument in last pass 3 mencoder call.</p>
<hr />
<div>[[Category:Audio/Video]]<br />
{{i18n|MEncoder}}<br />
{{Article summary start}}<br />
{{Article summary text|An overview of the video encoding/decoding tool provided by [[MPlayer]].}}<br />
{{Article summary heading|Related}}<br />
{{Article summary wiki|DVD Ripping}}<br />
{{Article summary wiki|Dvd2Avi}}<br />
{{Article summary wiki|MPlayer}}<br />
{{Article summary wiki|Video2dvdiso}}<br />
{{Article summary end}}<br />
<br />
MEncoder is part of the {{Pkg|mencoder}} package.<br />
<br />
== Basics ==<br />
<br />
The basic syntax for a conversion is<br />
{{bc|mencoder original_video.mpg -o new_video.avi -ovc output_video_codec -oac output_audio_codec}}<br />
<br />
So to convert movie.mpg to movie.avi with MPEG-4 Part 2 video and MP2 audio, the command is<br />
{{bc|mencoder movie.mpg -o movie.avi -ovc lavc -oac lavc}}<br />
<br />
The default output formats without any options are MPEG-4 for video and MP2 for audio.<br />
<br />
This is basically how one converts a video. However, there are MANY more options available.<br />
<br />
For input formats, MEncoder can use any format that MPlayer can play, so to verify whether it will work with your video, just try playing it in MPlayer.<br />
<br />
To list <u>o</u>utput <u>v</u>ideo <u>c</u>odecs, run<br />
$ mencoder -ovc help<br />
<br />
Similarly, to list <u>o</u>utput <u>a</u>udio <u>c</u>odecs, run<br />
$ mencoder -oac help<br />
<br />
This information can also be found [http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-selecting-codec.html here] where it better explained, although non-specific.<br />
<br />
There are many more options available (e.g. you can define bit rates and expected sizes). {{Gentoo|Mencoder}}<br />
<br />
==Example==<br />
<br />
This approach allows one to make a .mkv file with an [[Wikipedia:H.264/MPEG-4 AVC|H.264]]-encoded video and any number of Vorbis-encoded audio tracks.<br />
<br />
We will use '''mencoder''' (part of '''mplayer''' package) for ripping and encoding and '''mkvmerge''' (part of '''mkvtoolnix''' package) for making the .mkv file itself.<br />
<br />
=== Ripping and encoding the video ===<br />
<br />
The H.264 encoder is usually used in two passes: the first reads informations about the movie, the second uses that information to encode. We will not extract any audio for now.<br />
<br />
Commands follow; remember to replace the variables with the proper values:<br />
<br />
# First pass: we are just collecting information, so the normal output is thrown away.<br />
mencoder -dvd-device "$ISO" dvd://"$TITLE" -chapter "$CHAPTER" -o /dev/null -nosound -ovc x264 \<br />
-x264encopts direct=auto:pass=1:turbo:bitrate=900:bframes=1:\<br />
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \<br />
-vf scale=-1:-10,harddup<br />
<br />
# Second pass: here we compress the video track using the information from the first step.<br />
mencoder -dvd-device "$ISO" dvd://"$TITLE" -chapter "$CHAPTER" -nosound -ovc x264 \<br />
-x264encopts direct=auto:pass=2:bitrate=900:frameref=5:bframes=1:\<br />
me=umh:partitions=all:trellis=1:qp_step=4:qcomp=0.7:direct_pred=auto:keyint=300 \<br />
-vf scale=-1:-10,harddup -o video.avi<br />
<br />
This will create a '''video.avi''' file containing the video. You can play with the '''-x264encopts''' options and the '''-vf''' filters to improve the quality or reduce the file size. For example, a movie with a black border should be cropped with '''-vf crop=$X:$Y,scale=-1:-10,harddup''' with the proper values instead of '''$X''' and '''$Y''' (see '''cropdetect''' in the MEncoder manual). You may want to scale down the movie with '''-vf scale=$WIDTH:-10,harddup''' the width of the movie will become '''$WIDTH''' (keep '''$WIDTH''' a multiple of 16: 640, 480, or 320 are usually fine), the height will be correctly calculated in order to keep the aspect ratio.<br />
<br />
You can also use any other of the filters MEncoder has to offer, like '''pullup,softskip''' or you can change the frame rate using '''-ofps'''. (If you do so, remember to use the same frame rate everywhere including in the commands to rip audio.)<br />
<br />
It is important that you use '''harddup''' as the last filter: it will force MEncoder to write every frame (even duplicate ones) in the output. Also, it is necessary to use '''scale=$WIDTH,-10''' with '''$WIDTH''' as '''-1''' to keep the original width or a new, usually smaller, width: it is necessary since the H.264 codec uses square pixels and DVDs instead use rectangular pixels.<br />
<br />
=== Ripping and encoding the audio ===<br />
<br />
You can extract audio tracks as needed. Here we compress with the Vorbis algorithm, but you may want to check the MEncoder manual<br />
in order to see alternatives.<br />
<br />
The command follows (replace the variables with desired values):<br />
<br />
# Here we rip and compress the audio.<br />
mencoder -dvd-device "$ISO" dvd://"$TITLE" -alang "$AUDIOLANG" -chapter "$CHAPTER" -ovc frameno \<br />
-oac lavc -lavcopts acodec=vorbis:abitrate=224 -channels 2 -srate 48000 -o "$AUDIOLANG".avi<br />
<br />
You should repeat the command for every audio track you want, so we will have .avi files with the audio track.<br />
<br />
You may also want to use '''-channels 6''' to exact all the channels of a 5.1 DVD or changing the bit rate. As with the video, you can use audio filters via '''-af''' but it is not necessary.<br />
<br />
=== Making the final .mkv file ===<br />
<br />
Putting it all together in a single file is simple. Add other audio tracks if needed:<br />
<br />
mkvmerge -D audio.avi -A video.avi -o mymovie.mkv<br />
<br />
The '''.mkv''' file will contain everything, so you can store your movie keeping all the audio track you want. Even if you are not interested in keeping multiple sound tracks, the H.264/Vorbis format pair should ensure great quality.<br />
<br />
Of course to make the work easier, you should write every command in a single file and execute it.<br />
<br />
See [[Dvd2Avi]] for a sample script.<br />
<br />
=== Encoding video mp4 for Nokia 5800 XM and Nokia N97 ===<br />
In '''2 pass'''es with small bitrates (640kbps video vbitrate and 96kbps audio abitrate) yields pretty watchable video mp4 for nokia 5800 xm and Nokia N97 phones' default video player. Let's assume that we have a video.avi file (replace video.avi with your video file)<br />
<br />
'''mkv to mp4 (nokia 97, 5800 compatible)'''<br />
<br />
'''# step 1:''' convert the mkv to mpg ; many mkv files do not directly get converted to mp4<br />
mencoder "$1" -ovc lavc -lavcopts vcodec=mpeg1video -aid 0 -oac pcm -o delete_me.mpg<br />
'''# step 2:''' convert the mpg file to mp4<br />
mencoder -of lavf -lavfopts format=mp4 -oac lavc -ovc lavc -lavcopts \<br />
aglobal=1:vglobal=1:acodec=libfaac:vcodec=mpeg4:abitrate=128:vbitrate=640:keyint=250:mbd=1:vqmax=10:lmax=10:turbo -af lavcresample=44100 \<br />
-vf harddup,scale=640:-3 "delete_me.mpg" -o "$1.mp4"<br />
'''# step 3:''' delete the temporary huge sized mpg file <br />
rm "delete_me.mpg"<br />
<br />
replace "$1" with the .mkv video filename. e.g., sample.mkv; here -aid 0 = first audio track in mkv; <br />
<br />
'''Converting an avi to mp4 (nokia 97, 5800 compatible) using multipass (2 passes)'''<br />
<br />
# First pass:<br />
mencoder -of lavf -lavfopts '''format=mp4''' -oac lavc -ovc lavc -lavcopts \<br />
aglobal=1:vglobal=1:'''acodec=libfaac:vcodec=mpeg4:abitrate=96:vbitrate=640''':keyint=250:mbd=1:vqmax=10:lmax=10:vpass=1:turbo -af lavcresample=44100 \<br />
-vf harddup,'''scale=640:-3''' "video.avi" -o "video.mp4"<br />
<br />
# Second pass:<br />
mencoder -of lavf -lavfopts '''format=mp4''' -oac lavc -ovc lavc -lavcopts \<br />
aglobal=1:vglobal=1:'''acodec=libfaac:vcodec=mpeg4:abitrate=96:vbitrate=640''':keyint=250:mbd=1:vqmax=10:lmax=10:vpass=2 -af lavcresample=44100 \<br />
-vf harddup,scale=640:-3 "video.avi" -o "video.mp4"<br />
<br />
After the 2nd pass is done, copy the video.mp4 to nokia 5800 xm or n97's memory card and open the file using file manager of nokia after removing the device from the computer.<br />
<br />
we need to run the above two commands in the folder where video.avi is present. play around with abitrate, vbitrate, and scale values to get video quality and size of your liking. alternate '''formatfactory''' is a mencoder gpl application for windows which '''runs in wine''', can also '''convert all-to-mp4'''.<br />
<br />
'''scale=640:-3''' will try to keep the video width to 640 and resize the video height accordingly. Do use the "original" aspect in nokia's mp4 player "option->aspect" for 16:9 and 4:3 aspect ratio videos.<br />
<br />
=== Encoding a multi audio / multi language MKV video to an MP4 with different audio streams ===<br />
to encode multi-audio file to mp4 we need to use the "-aid <audio stream number>" like -map 0:1 in ffmpeg) <br />
<br />
1. To extract video+audio stream1 (usually english) of mkv file:<br />
mencoder -oac copy -ovc copy '''-aid 0''' sample.mkv -o sample.mp4 <br />
<br />
2. To extract video+audio stream2 (usually non-english, like Hindi, French, etc.) of mkv file:<br />
mencoder -oac copy -ovc copy '''-aid 1''' sample.mkv -o sample.mp4<br />
<br />
=== Adding SubRip subtitles to a file ===<br />
<br />
The following output video codec (-ovc) options are suggested as very high-quality settings and should suffice for most transcodings, including the additon of subtitles to a stream. <br />
<br />
====Two-pass [http://www.videolan.org/developers/x264.html x264] ([http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-x264.html very high-quality]) ====<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts pass=1:preset=veryslow:fast_pskip=0:tune=film:frameref=15:bitrate=3000:threads=auto -sub original_video.srt -subfont-text-scale 3 -o /dev/null<br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts pass=2:preset=veryslow:fast_pskip=0:tune=film:frameref=15:bitrate=3000:threads=auto -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
</pre><br />
* {{Ic|fast_pskip}}={{Ic|0}} is a maximum quality {{Ic|placebo}} preset option.<br />
* {{Ic|frameref}} is the only other major option undefined by {{Ic|preset}} settings.<br />
* {{Ic|bitrate}} values can be modified to suit desired file size and quality needs. <br />
* {{Ic|tune}} should be set to match the type and content of the of media being encoded.<br />
<br />
====Single-pass x264 (very high-quality) ====<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts preset=veryslow:tune=film:crf=15:frameref=15:fast_pskip=0:threads=auto -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
</pre><br />
* The following example uses the option {{Ic|-of lavf}} to mux the output into a [[Wikipedia:Matroska|Matroska]] container which is autodetected from the output file extension {{Ic|.mkv}}<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -of lavf -ovc x264 -x264encopts preset=veryslow:tune=film:crf=15:frameref=15:fast_pskip=0:global_header:threads=auto -sub original_video.srt -subfont-text-scale 3 -o output_video.mkv<br />
</pre><br />
* {{Ic|global_header}} writes global video headers to extradata, or in front of keyframes and is typically required for .mp4 and .mkv containers.<br />
<br />
====Two-pass [http://www.xvid.org/ xvid] ([http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-xvid.html very high-quality]) ====<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc xvid -xvidencopts pass=1:chroma_opt:vhq=4:max_bframes=1:quant_type=mpeg:threads=6 -sub original_video.srt -subfont-text-scale 3 -o /dev/null<br />
mencoder original_video.avi -oac copy -ovc xvid -xvidencopts pass=2:chroma_opt:vhq=4:max_bframes=1:quant_type=mpeg:bitrate=3000:threads=6 -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
</pre><br />
* {{Ic|threads}}={{Ic|n}} where n = physical, or CPU cores. <br />
* Recent versions of mencoder enable {{Ic|bvhq}}={{Ic|1}} as a default setting. <br />
* Xvid does not accept {{Ic|bitrate}} settings on the first of multiple-pass encodings.<br />
* {{Ic|subfont-text-scale 2-3}} helps with proper sizing with 16:9 format screens.<br />
* {{Ic|max_bframes}}={{Ic|0}} can be set so long as the bitrate is high enough.<br />
<br />
==== Three-pass [[Wikipedia:Libavcodec|lavc]] ([http://www.mplayerhq.hu/DOCS/HTML/en/menc-feat-enc-libavcodec.html very high-quality mpeg4]) ====<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ffourcc DX50 -ovc lavc -lavcopts vpass=1:mbd=2:mv0:trell:v4mv:cbp:predia=6:dia=6:precmp=6:cmp=6:subcmp=6:preme=2:qns=2:vbitrate=3000 -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
mencoder original_video.avi -oac copy -ffourcc DX50 -ovc lavc -lavcopts vpass=3:mbd=2:mv0:trell:v4mv:cbp:predia=6:dia=6:precmp=6:cmp=6:subcmp=6:preme=2:qns=2:vbitrate=3000 -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
mencoder original_video.avi -oac copy -ffourcc DX50 -ovc lavc -lavcopts vpass=3:mbd=2:mv0:trell:v4mv:cbp:predia=6:dia=6:precmp=6:cmp=6:subcmp=6:preme=2:qns=2:vbitrate=3000 -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
</pre><br />
* Introducing {{Ic|threads}}={{Ic|n}}>{{Ic|1}} for {{Ic|-vcodec mpeg4}} may skew the effects of [[Wikipedia:Motion_estimation|motion estimation]] and lead to [http://ffmpeg.org/faq.html#SEC16 reduced video quality] and compression efficiency.<br />
* {{Ic|predia}}={{Ic|6}}:{{Ic|dia}}={{Ic|6}}:{{Ic|precmp}}={{Ic|6}}:{{Ic|cmp}}={{Ic|6}}:{{Ic|subcmp}}={{Ic|6}} to {{Ic|predia}}={{Ic|3}}:{{Ic|dia}}={{Ic|3}}:{{Ic|precmp}}={{Ic|3}}:{{Ic|cmp}}={{Ic|3}}:{{Ic|subcmp}}={{Ic|3}} can reduce encoding times without incurring much loss in quality.<br />
* {{Ic|vmax_b_frames}} not included as referenced in the official mencoder documentation as the current default setting is to not to use [[Wikipedia:Video_compression_picture_types|B-frames]] at all.<br />
* {{Ic|vb_strategy}} not included as referenced in the official mencoder documentation for the same reason as above. Else {{Ic|vb_strategy}}={{Ic|2}}.<br />
<br />
=== Adding VOBsub subtitles to a file ===<br />
====Two-pass x264 (very high-quality) ====<br />
<br />
* Direct {{Ic|-vobsub}} to the {{ic|subtitle_file}} using the full pathname of the file without extensions ({{ic|.idx}}/{{ic|.sub}}).<br />
* Select the second subtitle ID language ({{Ic|-vobsubid 2}}) contained within the VOBsub files ({{ic|.idx}}/{{ic|.sub}}). <br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts pass=1:preset=veryslow:fast_pskip=0:tune=film:frameref=15:bitrate=3000:threads=auto -vobsub subtitle_file -vobsubid 2 -o /dev/null<br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts pass=2:preset=veryslow:fast_pskip=0:tune=film:frameref=15:bitrate=3000:threads=auto -vobsub subtitle_file -vobsubid 2 -o output_video.avi<br />
</pre><br />
<br />
==== Testing subtitle muxing results ====<br />
Avoid passing resource intensive encoding options in order to verify desired results sooner rather than later.<br />
===== Single-pass x264 (low quality) =====<br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts preset=ultrafast:threads=auto -sub original_video.srt -subfont-text-scale 3 -o output_video.avi<br />
</pre><br />
<pre style='overflow:auto'><br />
mencoder original_video.avi -oac copy -ovc x264 -x264encopts pass=2:preset=ultrafast:threads=auto -vobsub subtitle_file -vobsubid 2 -o output_video.avi<br />
</pre><br />
<br />
=== mp2 vs. mp3lame vs. aac ===<br />
<br />
* {{Pkg|toolame}} is recommended over [http://www.ffmpeg.org/ FFmpeg] lavc (libavcodec) for mp2 encoding.<br />
* [http://lame.sourceforge.net/ mp3lame] is recommended over [http://www.audiocoding.com/faac.html FAAC] (not fully developed) encoding at all bitrates.<br />
<br />
== GUI frontends ==<br />
<br />
* {{Pkg|ogmrip}}<br />
*: OGMRip is an application and a set of libraries for ripping and encoding DVD into AVI, OGM, MP4, or Matroska files using a wide variety of codecs. It relies on mplayer, mencoder, ogmtools, mkvtoolnix, mp4box, oggenc, lame, and faac to perform its tasks.<br />
* {{AUR|hybrid-encoder}}<br />
*: Hybrid is a multi platform (Linux/Mac OS X/Windows) Qt based frontend for a bunch of other tools which can convert nearly every input to x264/Xvid/VP8 + ac3/ogg/mp3/aac/flac inside an avi/mp4/m2ts/mkv/webm container, a BluRay or an AVCHD structure.<br />
* {{AUR|hypervc-qt4}}<br />
*: Hyper Video Converter is a frontend for various cli videoencoder tools I have made because I wanted something, that let's me quickly convert videos from konqueror without typing 3-line-commands in the console.<br />
* {{AUR|iriverter}}<br />
*: iriverter is a cross-platform frontend to mencoder designed to facilitate the conversion of almost any video format to one that is playable on various multimedia players. <br />
* {{AUR|kmenc15}}<br />
*: Kmenc15 is an advanced Qt/KDE MEncoder frontend, generally designed to be a VirtualDub replacement for Linux. It is most useful for editing and encoding large high quality AVIs capped from TV. It allows cutting and merging at exact frames, applying any MPlayer/MEncoder filter, with preview.<br />
* {{AUR|kvideoencoder}}<br />
*: KVideoEncoder is a GUI for the mencoder and transcode.<br />
* {{AUR|qvideoconverter}}<br />
*: QVideoConverter is an GUI for mencoder and provides an simple way to convert files/dvds to DivX videos.<br />
* {{AUR|jmencode}}<br />
*: This program is a simple java front-end for the free and very useful MPlayer software, for the purpose of encoding video. Initially the focus is on converting DVD into MPEG-4.</div>Numbr6