Various mplayer Tips & Tricks

mplayer is the program I use to play all audio & video files I have (which is quite a bit). It's even the program used by my vitunes application to play all multimedia files. It's relatively easy to use in its basic form, but below is a listing of applications of mplayer that I can never remember the exact details of.

Skipping through Audio/Video Files with a Broken Index

How to skip forward/backwards in files with a broken index

Frequently, audio/video files I want to watch either have a mising or broken index, which is what allows you to skip-through the playback of the file. You can force mplayer to build and use its own index, however, using the -idx switch, or the -forceidx if the index within the file is causing problems.

   mplayer -idx file.whatever
   mplayer -forceidx file.whatever

Playing a DVD

How to play a DVD

The OpenBSD FAQ pretty much explains everything you need, yet I always forget that to play a DVD using mplayer, you can do one of the following...

  • Run mplayer dvd:/// as root, or
  • Mount the DVD as root (mount -t cd9660 -r /dev/cd0c /mnt) with appropriate permissions on the mount location, and then run mplayer dvd:/// as any user.

Taking a Screenshot during a Video

How to take screenshots (screen captures) of a playing video

After a little digging through the mplayer man page, it's quite easy. The important thing to remember is to start mplayer with the -vf-add screenshot option.

   mplayer -vf-add screenshot  file.whatever

After that, you can do one of the following:

  • As the video is playing, you can take a single screenshot by simply pressing the s key.
  • Or, to start taking a screenshot of every frame, press S (capital-s). The files will be stored in the current directory named shotXXXX.png.

Extracting Clips of Audio, Video, or Both

How to extract audio/video segments from one file and save to another

This takes a little work, but it can be quite easy once you get the hang of it. Note that it's possible to extract only an audio segment, or only a video segment. Below, I extract both, but it should be fairly easy from the below examples (and the mplayer man page) how to extract only one.

  1. First, play the video/dvd in mplayer and record the start and end time of the clip you want to extract.
    mplayer video.mpg
  2. Play the video in mplayer using these start/stop times to make sure you have the time-span just right, using
    mplayer video.mpg -ss 828.2 -endpos 37.5

    This tells mplayer to start playing the video 828.2 seconds from the begining of the file, and to play only the next 37.5 seconds.
  3. Once you have the time-span correct, use mencoder to extract the clip as follows. Note that in this example, I'm extracting the video from a DVD (specifically title 7).
    mencoder dvd://7 -ss 828.2 -endpos 37.5 \
             -o foo.avi -oac copy -ovc lavc -lavcopts vcodec=mpeg4

    The -o foo.avi tells mencoder to save the clip to the file foo.avi in the current directory. The -oac, -ovc, and -lavcopts specify what audio and video codecs to use for the extracted clip (for the audio, I just said "copy" whatever codec was used in the original, and for the video I direct it to use the mpeg4 codec).
  4. That's it. Once you're done, play the resulting clip in mplayer to make sure you got it right.