Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Video Playback and Encoding with MPlayer and MEncode

by KIVILCIM Hindistan
04/10/2003

MPlayer to rule them all,
MPlayer to show them,
MPlayer to bring them together,
and in the darkness encode them.

You have Linux on desktop, you have broadband. You have cutting edge p2p file sharing programs, but cannot get all the fun. Why? Because you lack a very important component, a decent movie player.

Search no more. MPlayer is here for all your needs. Be it mpg, avi, mov, Real Media, or even the latest version of Windows Media Player files, MPlayer can display them seamlessly with all support, meaning subtitles, AC3 sound, and more. Even more, you can watch TV from your TV card, capture streams from the tuner card or the Internet, or even recode them with your favorite codec, with on screen display and the key commands that you can only find in professional VTRs. MPlayer is more than just a media player, it is an entire multimedia solution.

O'Reilly Emerging Technology Conference.

As for performance, I can say that MPlayer far surpasses any Windows player, under the same configuration, by a wide margin. This goes for both performance and quality. A stock Celeron 300 machine running Linux and MPlayer will outperform a Celeron 466 most likely. Even if it does not, you can configure almost every aspect to see that it does.

MPlayer History and Codecs

The MPlayer project, which at the time of writing is #1 at Freshmeat (with a score of 58777, beating her majesty the Linux kernel at 41,468), started almost two years ago. A Hungarian programmer, Árpád Gereöffy, could not find a player to his taste. He spent half an hour hacking on libmpg3. After a month or so, there were two programs -- mpg12play for playing mpegs and avip for avi files. In the first days of 2001 these two files united in a single binary called MPlayer 0.10.

Bit by bit, piece by piece, after many hours of development and Coke consumption, the player came to the almost-done version of0.90 rc5.

After two years of active development, MPlayer is probably the most capable player around, supporting a wide range of video codecs:

It also supports a wide range of audio codecs:

Let's see what the most popular project on Freshmeat says for itself.

Configuring and Compiling MPlayer

First you must visit the MPlayer homepage. There you will find the source for MPlayer V0.90 rc5, the most stable and full-featured version at the time of writing. To enable GUI support, you must download additional skins and font files from the same address. This is all you need. Compiling is simple. Extract the .bz2 file, run configure, make, and make install. (To use the GUI functions, use configure --enable-gui --enable-png.)

That's it. Now extract your desired font and skin files to the ~/.mplayer directory in order to enable on-screen display and skin support. Now you have an all round media player, which is capable of doing lots of things.

By default, MPlayer comes with most of the codecs that you will need, including the cutting edge DivX 5 codec. If you need some legacy Windows codec that does not have support in the MPlayer tree, you can install the Win32 codecs package and should have no problem. I advise you to use the MPlayer/libavi codecs first, because they are much faster and more stable.

Using and Tweaking MPlayer

You can start MPlayer as a command line program with mplayer. Invoke the GUI with as gmplayer. The GUI is mostly self explanatory, but the command line options have some magic to explain.

The main MPlayer config file is ~/.mplayer/config. This file has comments for every function. The defaults are usually fine, but you can tweak a few things. MPlayer stores key bindings in ~/.mplayer/input.conf. You can edit them extensively, but remember: first backup, then play it safe. The syntax is very simple and needs no explanation.

MPlayer also has several special command-line options to deal with weird media file behavior. Suppose that you have a Divx in Japanese but the subtitle track is not synchronized with the video; one has 25 fps and the other 23, meaning the film follows the subtitle. This command should solve your problem:

mplayer <filename> -subfile <filenmame.sub> -subfps 25

A worse case is where the movie is badly encoded and the audio can never catch up with the video. This boring case could ruin your precious Sunday afternoon if it were not for MPlayer. You can solve this problem by delaying the video slightly:

mplayer <filename> -delay <secs>

Both delays may be negative.

Suppose you have a file encoded with codec X, but MPlayer wants to play it with codec Y. You can force MPlayer to use a certain codec with:

mplayer <filename> -vc <codec>

Suppose you have just started to download a conference presentation and want to know its quality before committing to 700 MB--or you just have a broken divx. This command rebuilds the divx index from scratch, so that you can jump forward and backward in the broken avi:

mplayer <filename> -idx

If the avi still does not display, try these options with different configurations:

-nobps -ni -mc0 -forceidx -nocache

MEncoder

A decent media player is one of the prerequisites of a good desktop. Sometimes you may want more than that. For example, you may have backed up the hard-to-watch Robotech series to the .rm format, but now you would like to watch it on your vcd player. MPlayer also has a solution for this: it can crosscode nearly all media files. If you have compiled the MPlayer package, MEncoder is also present.

The syntax is very simple. This command line encodes the basket.rm file with the libav codec (the best divx codec for both performance and quality) and the soundtrack with mp3lame.

mencoder -ovc lavc basket.rm -oac mp3lame -o basket.avi

Remember the avi file with the broken index? Instead of always using the workaround, you can permanently fix it with MEncoder. The following command rebuilds the index and copies the whole audio and video stream as they are to the output file.

mencoder -idx input.avi -ovc copy -oac copy -o output.avi

Perhaps you'd like to catenate multiple avi files into one single file. Provided they use the same codec and have the same bitrate, this is also easy. It's back to Unix roots:

cat 1.avi 2.avi | mencoder -noidx -ovc copy -oac copy -o output.avi -

I'll not go into much detail here, because MPlayer and MEncoder have more options than I can describe in this article. Enjoy these rich programs and experiment with the settings.

Conclusion

As with many Linux applications, there is also a MPlayer only distribution called Movix. It takes just 8Mb on the CD. Your Divx cd's will be bootable, needing no harddisk, no operating system, nothing but the CD to play and the monitor to watch. The movix distribution is fully muscled. You will likely reach the best fps rate you have ever seen.

Don't forget, the best popcorn is salty with butter ;)

References

KIVILCIM Hindistan works as a full time computer security consultant with a CISSP, using Linux and Free Software as weapons of choice.


Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.