MPlayer

MPlayer is not just a media player. It is a whole video system. In addition to playing movies, it can apply filters, transcode, transmux, and download movies. It even includes a "slave mode" for if you want to control it from another program.

Playing movies

Of course the most basic thing that you would want to do with a media player is to play movies. MPlayer supports many different formats including: avi, mov, mp3, mp4, ogg, ogm, rm, mkv.

To play a movie:
mplayer movie.avi

If a movie has different video, audio, subtitle tracks, you can specify them by number:

mplayer -vid 1 -aid 2 -sid 3 movie.mkv

-vid specifies the video track, -aid specifies the audio track, and -sid specifies the subtitle track.

To see what tracks are available in a movie, use verbose mode:
mplayer -v movie.mkv

Naturally, you usually won't want to have to specify language by the track number. It is much more convenient to specify which language you want. For formats that support it, you can use the language identifier.

To play a movie in a language:
mplayer -alang jpn -slang eng movie.mkv

This specifies that you want the audio language (alang) to be Japanese (jpn) and the subtitle language (slang) to be English (eng). Note that you can also flip through the subtitles by pressing 'j'.

Specifying each of this options each time would become tedious. You can save all of your settings in ~/.mplayer/config. These are the same as the command line parameters, but use an equal sign.

This is my mplayer config file:
fs=1           #Fullscreen
sid=0          #Subtitles enabled
alang=jpn,eng  #Use Japanese or English audio
slang=eng      #Use English subtitles

This says that I like movies to play full screen (fs), I like subtitles (sid), I prefer Japanese (jpn) audio over English (eng) audio (alang), if both are available, and I want English (eng) subtitles (slang).

MPlayer can play more than just stand-alone files. For example, if you have libdvdcss installed, you can play DVDs with CSS. (Without it, you can only play non-CSS DVDs.)

To play track 1 of a DVD:
mplayer dvd://1

Some computers have multiple DVD drives. In that case, you might have to specify which drive.

To play track 1 of a DVD from /dev/cdroms/cdrom2
mplayer -dvd-device /dev/cdroms/cdrom2 dvd://1

You can also stream from URLs if you have network support enabled.

Play from a URL:
mplayer http://host/path/file.ext

Sometimes streaming is not convenient, for example because your connection is not quite fast enough. You can tell it to buffer / download the entire file first, then play it.

Buffer and then play a URL:
mplayer -dumpstream -dumpfile a.ext http://host/path/file.ext
mplayer a.ext

MPlayer can also play some less conventional formats. A useful trick is that it can treat a directory full of JPEGs as a video. Using this, you can make a slide show or a regular movie.

Playing a directory of JPEGs:
mplayer -fps 30 mf://dir/\*.jpg

The backslash (\) before the star is to escape it. Depending on how you are using it, this might not be necessary. Also, for a slide show, you might want to use -fps 1 or so.

There is also a loop option to tell mplayer to play something again after it finishes playing it. So, you can use this to make a continuous slide show (like a screen saver).

Looping slide show:
mplayer -loop 0 -fps 1 mf://dir/\*.jpg

-loop 0 means to loop forever. You can put a number to make it loop only that many times.

Terminology

To have a better idea what is going on, it helps to know the terminology. A container format is what you usually think of as a video or audio file, such as avi, mp4, ogg, and mkv. The container has streams, such as video, audio, and subtitles. The container keeps these different streams synchronized with each other and has times in the streams close to each other in the file, so the media player will not have to jump around the file just to play sequential frames. Putting the streams together in this fashion is called multiplexing, which is sometimes abbreviated to muxing. Taking it apart is demultiplexing.

Video and audio are very large. So, to be able to handle them efficiently, they need to be represented in a more succinct way. This is called compression. Compressing a stream is called encoding. Similarly, decompressing is called decoding. The means of encoding and decoding is called a codec (code, decode). Again, since having raw video or audio around is large and undesireable, one typically goes from 1 codec to another, which is called transcoding.

Example diagram of a container and streams:

ContainerStreamLanguageCodec/Representation
Matroska (mkv)
VideoH.264
AudioEnglishOgg Vorbis
AudioJapaneseOgg Vorbis
SubtitleEnglishSRT

Making movies

In addition to playing movies, mplayer is also capable of encoding movies. To do this, mplayer includes a tool called mencoder. It allows you to recompress (transcode) a movie using a different codec and also to put it into a different container. For this section, I will also assume that you have the tools MP4Box (from GPAC), oggenc (from vorbis-tools), and mkvmerge (from mkvtoolnix). These tools help take care of the container formats. If you do not have them, you should install them using whatever package manager your Linux distribution has, or from source. For example, Gentoo has portage, Debian (including Ubuntu) has apt-get, which has a GUI front end Synaptic. RedHat (including FedoraCore) has RPM.

The main benefit of using mencoder as opposed to many other tools out there is that mencoder can transcode anything that mplayer can play. This makes it able to handle a much wider range of input formats than most other programs.

Before encoding a movie, you must decide what the destination format is. What codecs and container formats do you want to use? A format that has been popular lately is H.264 in a MP4 container. This is what Apple is currently using. I will not address the audio portion here since I haven't worked with it as much. To encode to H.264, you must have mplayer compiled with x264 support.

Making a QuickTime compatible MP4 with H.264 from a.avi:
mencoder a.avi -o a.h264 -of rawvideo -nosound \
  -ovc x264 -x264encopts bitrate=800    #Convert to H.264
MP4Box -fps 30 -new -add a.h264 a.mp4   #Put in mp4 container 

The backslash (\) at the end of the line means that the line continues. Note that if you just let mplayer make an .avi with H.264 in it, it will not work with most media players. Not all features of H.264 are supported by .avi, so using .avi for H.264 is not recommended. Also, many container programs, such as MP4Box and mkvmerge will complain a lot if given an H.264 file in an .avi container. You should specify the fps for MP4Box, otherwise it will try to set it to a default value, even if the correct fps value is contained in the .h264 file. Take care in calibrating the bitrate value so it is appropriate for what you are trying to transcode.

If you want something even more modern, you can use H.264 and Ogg Vorbis in a Matroska container.

Making a Matroska file:
mencoder a.avi -o a.h264 -of rawvideo -nosound \
  -ovc x264 -x264encopts bitrate=800           #Convert video to H.264
mplayer a.avi -vo null -ao pcm:fast:file=a.wav #Extract audio
oggenc a.wav                                   #Convert audio to Vorbis
MP4Box -fps 30 -new -add a.h264 a.mp4          #Put video in mp4 container
mkvmerge -o a.mkv a.mp4 a.ogg                  #Multiplex into Matroska 

The MP4Box step is necessary because mkvmerge cannot take a .h264 file as input, but can take a .mp4 file. Note that because the audio and video are being handled separately, it is possible that they desync, however this does not usually happen. You can also add subtitle tracks in different languages and so forth, but that is out of the scope of this guide.

What the script is doing is demultiplexing the file, transcoding the parts separately, and then multiplexing them back together.

A safer approach, to avoid audio and video getting out of sync, is to use FFMPEG. It is essentially the core of mplayer. It has better support for different containers than mencoder. You can get an approximation of the above script with this command:

ffmpeg -y -i in.avi -acodec libvorbis -vcodec libx264 out.mkv

This will convert in.avi into out.mkv with the video in H.264 and the audio in Ogg Vorbis. Note that support for these features must be compiled into FFMPEG. Also, make sure that you are using a recent version.

-y allows it to overwrite the output file if necessary. -i specifies the input file. -acodec says which audio codec to encode to, and similarly, -vcodec says which video codec to encode to.