Terminal MEncoder
John Knight tries to join the cool kids by ripping videos from the terminal with MEncoder – the bigger, sharper-dressed brother of Mplayer.
John Knight tries to join the cool kids by ripping videos from the terminal with the big-brother of Mplayer.
To carry on from last month’s MPlayer feature, we have MPlayer’s brother in arms, MEncoder. And like with MPlayer you may well ask, “MEncoder has a GUI, so why would I use the terminal?” Once again, we’d say that the Command Line gives you power in ways a GUI just can’t match. Each MEncoder command has five components: the program itself, the original video for conversion, the output video filename, the choice of audio codec, and the choice of video codec. The basic command syntax looks like this: $ mencoder original-video -o output-video -oac audio-codec -ovc video-codec So to start with a random video, here’s an example of the minimum amount of syntax you can get away with: $ mencoder alien-bovril.flv -o test.avi -oac x264 -ovc mp3lame
It’s probably worth breaking that down still more. Those audio and video codec switches are unfortunately mandatory – not specifying them will return an error. The example above uses the x264 library for encoding H.264 video streams, and the LAME library for encoding MP3 audio streams, all held within an .avi container.
To explore what codecs, codec packages or containers are available to use on your system, for audio, enter: $ mencoder -oac help And for video: $ mencoder -ovc help
In order to save you from a long boring list, we’ve included a screenshot listing the output of both commands ( below
right). This is on a Linux Mint system with nothing installed too fancy or exotic.
You’ll notice ‘copy’ in both the audio and video sections, which grabs what’s already in the source. The mp3lame option is particularly useful, and lavc (short for libavcodec) is also great for covering the most common audio formats.
On the video front, raw does uncompressed video and can be an excellent tool for certain tricky video projects, as long as you have the disk space! The majority of users will be interested primarily in the extensive lavc codec pack, plus xvid (Divx compatible) and x264 (H.264 compatible).
To explore the logic and syntax of MEncoder, we’ll take a basic command and then progressively change and add to it in a way we hope is easily followed. MEncoder’s syntax is convoluted, with different structures between codecs, and unfortunately we only have space to cover one codec each.
We’ve gone with x264 for the video codec, and mp3lame for the audio, but see the box ( below) for more info on other codecs. The command below will re-encode our test video into something that uses H.264 video and MP3 audio: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264
However, that basic command will only use default values, and you’ll likely want to tweak areas such as file size, quality and aspect ratio.
Let’s start with audio bitrate. To modify the mp3lame settings you must use the -lameopts switch, followed by the necessary arguments. In this case we will use a constant bitrate of 192kbps. First, insert the cbr argument (Constant Bit Rate), followed by a : separator, and finally bitrate argument (br=xxx), which looks like so: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -lameopts cbr:br=192 -ovc x264
Perhaps you aren’t happy with the default setting’s video quality and want to either turn it up for a better image, or down to save file size. The average bitrate for both audio and video will be displayed after mencoder terminates, and you use that number as a reference.
To set the bitrate manually, first use the -x264encopts switch, then use the argument bitrate=xxx. In this case, we’ll turn the bitrate up to 2000, and MEncoder will encode at that approximate bitrate, like so: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264 -x264encopts bitrate=2000 If things are getting squashed into the wrong aspect ratio, or you’d like to correct the aspect ratio of an existing video, you can do that. For a 16x9 image, simply add the argument -aspect 16:9, as in the following: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264 -aspect 16:9 And to return a stretched 4x3 image to its original proportions, just change the aspect argument to 4:3. $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264 -aspect 4:3
Do you have a clip that was shot on video and has that horrible “liney” appearance? This needs deinterlacing. There are multiple filters you can use, but we had the best results with yadif (Yet Another De-Interlacing Filter). To run it, use the video filter switch -vf with the argument yadif: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264 -vf yadif So to combine the last three examples into one command would look like so: $ mencoder alien-bovril.flv -o test.avi -oac mp3lame -ovc x264 -x264encopts bitrate=2000 -aspect 16:9 -vf yadif
DVD ripping
Now in order to avoid massive legal problems for the magazine, we’re going to assume that if you’re ripping from DVD, you either have legal rights over that DVD, or live in a country that legally permits personal backups. We don’t accept legal responsibility for your personal usage, nor do we promote illegal file sharing. ( we’veseenthe LXF torrents–Ed)
See the box ( above) for more information, but to do a simple rip of the DVD’s first title looks like so: $ mencoder dvd://1 -o test.avi -oac mp3lame -ovc x264
So far this is all stuff that can be done with a GUI. So why use the terminal? Because the functionality of the Linux shell enables you to do amazing things, and to the programs used within it, it adds extensibility and augmented functionality.
For starters, you can write shell scripts that will organise encoding jobs for you in advance. For instance, when needing to back up a video series for easy access, we were able to set up a script that would separately rip every DVD title in one go.
We’ll only give you two lines of the script we used to give you a general idea, but once you’ve worked out the settings for one title, you can usually apply the same to the rest on the disc. Then you need only change a couple of characters for each line (perhaps DVD title and episode numbers) and you can plough through the entire disc. Perfect for backing up DVD box sets! $ mencoder dvd://1 -o video-1-15.avi -oac mp3lame -lameopts cbr:br=192 -ovc x264 -aspect 16:9 -nosub -aid 128 $ mencoder dvd://2 -o video-1-16.avi -oac mp3lame -lameopts cbr:br=192 -ovc x264 -aspect 16:9 -nosub -aid 128
We’ll finish up here merging videos. If you input multiple videos of the same format, you can simply use copy for the codec choice and merge all videos into one, like so: $ mencoder video1.avi video2.avi video3.avi -o all-threevideos.avi -oac copy -ovc copy
Or if you have videos of varying formats and don’t mind some loss in quality (you might want to use a high bitrate to compensate), you can stitch them all together, and choose your own order, like so: $ mencoder video2.mov video1.avi video3.flv video4.mkv -o everything-squished-together.avi -oac mp3lame -ovc x264
Obviously if you’re just doing one or two videos, the GUI is probably the way to go. But if you want to do large batches of videos, or perhaps you just like getting your hands dirty, then you want the terminal. A bit of terminal know-how makes a powerful tool like MEncoder all the more so when combined with some old-fashioned inventiveness.