Hack 48. Use MPlayer
MPlayer is a great video player that can play just about any format. This introduction shows the basics you need to get up and running.
Every once in a while a tool comes along in Linux that impresses you in almost every respect with its flexibility. MPlayer is one of those tools. When it comes to video and audio playback, think of MPlayer as your universal translator. It can play basically any audio or video format you throw at it (provided it has the libraries available) in just about any container you throw at it. For instance, it can play DVDs from the disc, an image of the disc, or even just the VOBs from the DVD). Of course, depending on your taste, there is one downsideby default MPlayer is a command-line program. There is a graphical frontend for MPlayer for those interested, called gmplayer, or you might want to check out some of the other video players mentioned in this book such as xine [Hack #55] or VLC [Hack #56]. This hack discusses the basics of how to play multimedia files with MPlayer from the command line.
The first step to using MPlayer is to install it. Because of all of the different libraries mplayer supports and potential dependencies they cause, I recommend using a precompiled MPlayer package whenever possible. Most major distributions either ship with MPlayer packages available, or have third-party packagers that provide MPlayer (for instance, Debian users can add the following line to their /etc/apt/sources.list file to create an excellent third-party repository:
deb ftp://ftp.nerim.net/debian-marillat/ sid main
The next step is to sync with your repository and install mplayer:
$ apt-get upgrade and apt-get install mplayer-686
Replace 686 with 386, 586, k6, or k7 depending on your processor.
If you can't find a precompiled binary for your distribution, or if your precompiled binary did not include some of the extra MPlayer options you need, download the source code from the official site at http://mplayerhq.hu and follow the directions in the documentation at http://mplayerhq.hu/DOCS/HTML/en/install.html closely.
With mplayer installed, basic file playback is as simple as:
$ mplayer file.avi
The console will immediately fill with a lot of different output. This can be useful because MPlayer is telling you what information it can figure out about the file you passed to it, along with information about how it will try to play it. MPlayer should also display the video in a new window and - immediately start playback. Back in the console you will see output scroll by as MPlayer updates you on which frame is playing and how far along MPlayer is in the video.
MPlayer provides an extensive list of key bindings so that you can control playback. The manpage lists all of these options and Table 3-1 lists some of the more commonly used ones.
Most of these key bindings are pretty self-explanatory, but the + and options to adjust the audio delay are worth mentioning further. Sometimes when you create your own videos or convert videos between formats, the audio and video fall out of sync. This can be very frustrating when you are watching a movie, but with MPlayer you can tweak the audio with the + and -keys. Just hit one of the keys a few times and see whether you have improved or worsened the sync problems, and then adjust until the video and audio is completely in sync.
MPlayer is truly a universal multimedia playback tool, and in the next sections, I'll list some examples for playing back specific video types. For most video files, it is sufficient to simply pass the filename as an argument to mplayer, but for special videos like DVDs, VCDs, and filestreams, things are done slightly differently.
3.3.1. DVD Playback
MPlayer has good support for DVD playback, however one thing it does not have support for is DVD menus. When you play a DVD with MPlayer, it skips the menu system and everything else upfront and goes right to the movie, which can actually be a feature if you don't want to sit through the numerous ads and FBI warnings some DVDs have. Most DVDs have a main featurethe movie you purchased plus several lesser features, such as behind-the-scenes footage or scenes that were cut. In the case of episodic disks like TV shows, each episode is a different feature. Each of these features is a title, and you can select which title to play when you run mplayer. To start playback of the first title on a DVD, type:
$ mplayer dvd://1
Replace 1 with the number of the title you want to play. If you want to play a range of titles, you can specify the range on the command line. For instance, to play titles three through six, type:
$ mplayer dvd://3-6
You can also specify individual chapters (scenes) or a range of chapters with the -chapter argument. To play chapter four through eight on title one, type
$ mplayer dvd://1 -chapter 4-8
MPlayer will attempt to play from /dev/dvd, but if that device doesn't exist, or you want to point it to a different device, you may use the -dvd-device argument. The following command will play back from /dev/hdc:
$ mplayer dvd://1 -dvd-device /dev/hdc
You can even use the -dvd-device argument to play back directly from a DVD image somewhere on your filesystem:
$ mplayer dvd://1 -dvd-device /path/to/dvd.iso
It is even possible to use a directory full of VOB files:
$ mplayer dvd://1 -dvd-device /path/to/directory/
You may also specify language and subtitle options directly from the command line. The -alang option controls the audio language option and can accept multiple languages separated by commas. In that case, MPlayer will try the first language and fall back on the next language if the first isn't available. For instance, to play a movie in Japanese and fall back to English if Japanese isn't available, type:
$ mplayer dvd://1 -alang ja,en
The -slang option controls which language's subtitles are shown. To show the English subtitles on the above example, type:
$ mplayer dvd://1 -alang ja,en -slang en
3.3.2. (S)VCD Playback
(S)VCD playback in MPlayer is much like DVD playback. Just use vcd:// instead of dvd:// in the command line with the track to play as an argument. So, to play track one of a VCD, type:
$ mplayer vcd://1
MPlayer can even play the .bin files from (S)VCDs. You don't even need to pass any special optionsjust point mplayer to the .bin file to start playback.
3.3.3. Streaming Playback
MPlayer supports playback from a number of different audio and video streams. Just pass the URL on the command line:
$ mplayer http://example.com/stream.avi $ mplayer rtsp://example.com/stream
In fact, MPlayer's support for streams is such that a browser plug-in has been developed to leverage it [Hack #91].
There are a number of reasons MPlayer may not output your video correctly. If MPlayer has trouble identifying your video, all the video codecs mplayer requires may not be installed in your system. [Hack #53] explains how to find and install the various video and audio codecs you need under Linux.
If MPlayer plays the video but the video output either looks strange, you can't see it at all, or playback is very jerky, it's possible that MPlayer is configured to use the wrong video output option for your system. The different video output options MPlayer supports are numerous enough to a hack of their own, so check out "Master Video Output Options" [Hack #47] for more information.
Another reason for jerky video is simply that a system is too slow to play the video well. In this case, MPlayer will warn you in its output that your system is too slow to play the video and will recommend that you add the -framedrop option. This option tells MPlayer to drop video frames if the video can't keep up with the audio on the system.
3.3.5. More MPlayer Uses
MPlayer has a number of advanced and just plain fun uses. This hack only scratches the surface of options you can use with MPlayer, but other MPlayer uses can be found in [Hacks #49, #51, #52, and #53].