Mplayer notes

From Noah.org
Jump to navigationJump to search

These are my notes on MPlayer and MEncoder. These are my favorite video tools.

Play side-by-side stereo 3D video as an anaglyph

Anaglyphic images and video kind of suck, but it's a handy technique to use when you don't have a better method to display 3D stereo.

The trick is to use the scale video filter with the stereo3d filter.

mplayer -vf stereo3d,scale sbs_stereo_video.mov
# or
mplayer -vf stereo3d,scale -vo xv sbs_stereo_video.mov

rotate video

Use the -vf option to add a rotate filter. The filter takes an integer between

mplayer "mf:///var/spool/motion/*.jpg" -vf rotate=2

For the integer:

0 = 90 degrees clockwise
1 = flip
2 = 90 degrees counter-clockwise *
3 = flip

Error: "Source image dimensions are too high"

This error happens when playing a set of images as a video stream.

mplayer "mf://*.jpg" -loop 0
VO: [xv] 2304x1296 => 2304x1296 Planar YV12  [zoom]
Source image dimensions are too high: 2304x1296 (maximum is 2048x2048)
FATAL: Cannot initialize video driver.

This can often be worked around by using a different video output driver:

mplayer "mf://*.jpg" -vo x11 -loop 0
mplayer "mf://*.jpg" -vo gl -loop 0

configure mplayer

These options are good to put in ~/.mplayer/config:

stop-xscreensaver = "yes"  
framedrop = yes

The following video output option is more user friendly because you can resize the video or make it play fullscreen. The downside is that you can only play one video at a time because 'xv' is a limited system resource. What is worse is that other mplayers can actually start and will run, but they won't display anything. You will see other mplayer processes in your `ps` list even though you see no video.

vo=xv

The following option allows multiple videos to play at once and it is also more CPU efficient. This is handy when editing lots of little video clips. The downside is that it does not stretch video to fit a window and it cannot play fullscreen.

vo=x11

Play videos in a loop

The number 0 is the count, but this means to loop forever. IMPORTANT: you must put the '-loop' option after everything else on the command-line. For some stupid reason if you put the '-loop' before anything else then mplayer will still loop the video, but it will not reuse the same window; it will close the window and open a new window after each loop. This causes an annoying flicker at the end of the video. Also the window size and location will be reset to the default.

mplayer "mf://ct-scan-frames*.png" -mf fps=8 -loop 0

Play videos in a loop in Nautilus

IMPORTANT: you must put the '-loop' option after the '%f' when configuring Nautilus. The following option in Gnome Nautilus will play videos in a loop.

mplayer %f -loop 0

Play HD video on low performance system

This allows me to play HD video content (1080P) on my MacMini (running Linux) which has just barely the power to handle HD. This lowers quality a little bit, but it's hardly noticeable.

mplayer -lavdopts lowres=2:fast:skiploopfilter=all:threads=8 high_def_video.h264.ts

Play a DVD ISO

This will play the first chapter off a DVD ISO disk image.

mplayer dvd://1 -dvd-device disk_image.iso

This works with mencoder for transcoding DVD. Note that you are selecting a certain audio track that you have to put it before the dvd://1 option. I'm not sure why.

mencoder -aid 129 dvd://1 -dvd-device 'disk_image.iso' -o 'video.avi' -ovc lavc -oac copy -lavcopts vcodec=mpeg4:vbitrate=1500:mbd=2:aspect=4/3:vpass=1

Play a DVD VIDEO_TS directory structure

This will play the first chapter off a DVD VIDEO_TS directory structure where "DVD_Directory" should be the directory above the VIDEO_TS directory. This is similar to playing a DVD ISO.

mplayer dvd://1 -dvd-device /home/user/DVD_Directory

Play a video from an unfinished RAR download

You can play a video in a rar file before it has even finished downloading. Use the single dash option with `mplayer` to play video from a pipe. Use 'p' option on unrar to send the file data from a rar file to stdout.

unrar p "movie_download.part01.rar" | mplayer -

Mplayer video output on framebuffer device (/dev/fbdev0)

Be sure to use sudo or run command as root. Be sure to specify fbdev2 if you want color.

sudo mplayer -vo fbdev2 ct_scan_sample.flv

Webcam preview -- play raw video stream from video device

mplayer tv:// -tv device=/dev/video0

Record video directly from video device

Record video with ffmpeg (-an sets no audio):

ffmpeg -an -f video4linux2 -s 800x600 -r 25 -b 2000k -i /dev/video0 -vcodec mpeg4 -vtag DIVX -y video.avi

Record video with mencoder:

mencoder -nosound tv:// -tv device=/dev/video0:driver=v4l2::width=800:height=600:fps=30 -ovc lavc -o video.avi
mencoder -frames 30 -nosound tv:// -tv device=/dev/video0:driver=v4l2:fps=30 -ovc lavc -o video.avi

Convert video file to a sequence of images

This will convert a video file to a sequence of JPEG images:

mplayer -nosound -vo jpeg input.mov

You might prefer PNG as an intermediate format for better quality:

mplayer -nosound -vo png:z=9 input.mov

Note that the :z=9 option is slow. The following is much faster; however, it uses far more space as the PNG images are store uncompressed.

mplayer -nosound -vo png input.mov

Note that if your source video is MJPEG then it's best to use ffmpeg to decode the JPEG frames without recompression. The JPEG frames in the MPEG are missing headers, so a header must be added to each image to make them valid. See http://ffmpeg.org/ffmpeg-doc.html#SEC57 for more info.

The following will dump the individual JPEG frames from an MJPEG video.

ffmpeg -i input.mov -vcodec copy -vbsf mjpeg2jpeg frame_%d.jpg

Convert a sequence of images to video file

Same idea as sequence playback, but use mencoder to make a video file:

h264

mencoder mf://*.jpg -mf type=jpeg:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:fast_pskip=0:threads=auto:crf=20 -o output.mov

You can modify the width and height if necessary:

mencoder mf://*.jpg -mf type=jpeg:w=1280:h=720:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:fast_pskip=0:threads=auto:crf=20 -o output.mov
1920 × 1080
mencoder mf://*.jpg -mf type=jpeg:w=1920:h=1080:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:fast_pskip=0:threads=auto:crf=20 -o output.mov

For even higher quality you can use these options:

mencoder mf://*.jpg -mf type=jpeg:fps=25 -ovc x264 -x264encopts preset=veryslow:frameref=15:tune=film:fast_pskip=0:threads=auto:crf=15 -o output.mov

mpeg4

mencoder "mf://*.jpg" -mf fps=25 -o output.avi -ovc lavc -lavcopts vcodec=mpeg4

Note that Mplayer and Mencoder do not handle TIFF images, so you may need to use [ImageMagick]] to convert the images to PNG or JPEG first. For example:

mogrify -format jpg -quality 85 *.tif

Play a sequence of images

This will play all jpeg images in the directory as a video. No need to first convert them to a video file. This will work with most image types. Playing a JPEG sequence is shown here:

mplayer "mf://*.jpg" -mf fps=24

Convert FLV to MPG (Flash video to MPEG video conversion)

The quality is about equal to the original; but the video almost doubles in size. This is for low-quality Flash videos similar to what you see on YouTube standard definition.

mencoder example.flv -oac copy -ovc lavc -of mpeg -lavcopts vcodec=mpeg1video -o example.mpg

Create a VCD, SVCD, or DVD

Install vcdimager and cdrdao. Both vcdimager and cdrdao are also available through apt on Ubuntu. For DVD you will also need dvdauthor. For, DVDs these instructions create an ISO disk image without a menu. For information on creating a DVD menu read the man page for dvdauthor. These instructions also assume NTSC. Read the mencoder help files for information on creating PAL disks.

VCD

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xvcd -vf \
scale=352:240,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
vcodec=mpeg1video:keyint=18:vrc_buf_size=327:vrc_minrate=1152:\
vbitrate=1152:vrc_maxrate=1152:acodec=mp2:abitrate=224 -ofps 30000/1001 \
-o <filename>.mpg <filename>.avi
vcdimager -t vcd2 -l "Movie Title" -c <filename>.cue -b <filename>.bin <filename>.mpg
cdrdao write --device /dev/cdrom <filename>.cue

SVCD

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=xsvcd  -vf \
scale=480:480,harddup -srate 44100 -af lavcresample=44100 -lavcopts \
vcodec=mpeg2video:mbd=2:keyint=18:vrc_buf_size=917:vrc_minrate=600:\
vbitrate=2500:vrc_maxrate=2500:acodec=mp2:abitrate=224 -ofps 30000/1001 \
-o <filename>.mpg <filename>.avi
vcdimager -t svcd -l "Movie Title" -c <filename>.cue -b <filename>.bin <filename>.mpg
cdrdao write --device /dev/cdrom <filename>.cue

DVD NTSC WIDESCREEN 16/9

I have a simple script which does this. This will take any video file and burn it to a widescreen DVD-Video format. Click here to download: avi2dvd

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
-vf scale=720:480,harddup -srate 48000 -af lavcresample=48000 \
-lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:\
keyint=18:vstrict=0:acodec=ac3:abitrate=192:aspect=16/9 -ofps 30000/1001 \
-o <filename>.mpg <filename>.avi
dvdauthor -o dvd/ -t <filename>.mpg
dvdauthor -o dvd/ -T
mkisofs -dvd-video -o disk_image.iso dvd/
growisofs -dvd-compat -Z /dev/dvd=disk_image.iso
# You can skip mkisofs step if you want to burn just one DVD.
# Use this growisofs command instead:
growisofs -dvd-compat -dvd-video -V "<volumelabel>" -Z /dev/dvd dvd/

DVD NTSC FULL 4/3

mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd:tsaf \
-vf scale=720:480,harddup -srate 48000 -af lavcresample=48000 \
-lavcopts vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:\
keyint=18:vstrict=0:acodec=ac3:abitrate=192:aspect=4/3 -ofps 30000/1001 \
-o <filename>.mpg <filename>.avi
dvdauthor -o dvd/ -t <filename>.mpg
dvdauthor -o dvd/ -T
mkisofs -dvd-video -o disk_image.iso dvd/
growisofs -dvd-compat -Z /dev/dvd=disk_image.iso
# You can skip mkisofs step if you want to burn just one DVD.
# Use this growisofs command instead:
growisofs -dvd-compat -dvd-video -V "<volumelabel>" -Z /dev/dvd dvd/

Note that /dev/cdrom should be a sym link to the real CDR burning device. This is probably already setup for you. On my system, /dev/cdrom is a sym link to /dev/scd0.

Display motion vectors

This might come in use for debugging:

mplayer -lavdopts vismv=1 input.avi

Configure mplayer under Gnome

I'm not a Gnome fan, but I've learned to live with it out of laziness. A few things make it more tolerable.

gconftool-2 --type string --set /desktop/gnome/volume_manager/autoplay_dvd_command "mplayer dvd://"
cp /usr/share/applications/defaults.list /usr/share/applications/defaults.list_backup
sed -e 's/totem.desktop/mplayer.desktop/g' /usr/share/applications/defaults.list_backup > /tmp/defaults.list
/tmp/defaults.list /usr/share/applications/defaults.list

Build mplayer on Ubuntu Overview

This is a very brief overview. See the #Build MPlayer script for everything that really needs to be done.

aptitude -q -y install libpng-dev libsvga1-dev zlib1g-dev
aptitude -q -y install libgtk1.2-common libglib1.2 libgtk1.2 libgtk-dev
aptitude -q -y install x-window-system-dev libx11-dev libxv-dev
aptitude -q -y install lame liblame-dev libtwolame-dev libavcodec-dev
./configure --disable-ass --enable-largefiles --enable-x11 --enable-xv --enable-svga
make
make install

Build MPlayer script

This will build MPlayer with svga, xv, and x11 output support. This is intended for an Ubuntu system, but might work on others. This should work on the majority of systems with a fresh Ubuntu installation with UNIVERSE support. Remove libsvga support for use on a totally stock Ubuntu with no UNIVERSE support.

This script pretty much automates everything. It will download the source code, codecs, and fonts from mplayerhq.hu and install everything. Normally I use Ubuntu packages, but in this instance I find that building from source works better. It's easier and I get every single codec that is available from mplayerhq.hu.

'x11' output gives a little better quality. 'xv' output quality isn't quite as nice, but it is faster (less CPU) and it can be dynamically resized or made fullscreen. The 'x11' video output has a fixed size even if you press 'f' for fullscreen (it just gets centered in the screen). 'xv' sometimes has a problem where only one 'xv' resource can be used at a time and some other process might lock 'xv' so you can only play one video at a time.

Download mplayer_build.sh <include svncat src="file:///home/svn/src/shell/mplayer_build.sh" highlight="sh" />

Error! overflow in spectral RLE, ignoring

This error usually happens on Ubuntu (8.10 Intrepid) if you install mplayer and do not also install the ffmpeg package. Simply install ffmpeg and the message will usually go away. By default mplayer will try to use some sort of FFMPEG compatibility layer if the real ffmpeg package is not available.

Sometimes installing ffmpeg will not fix the "overflow in spectral RLE" problem. In this case, I found that re-encoding the video using ffmpeg fixes the problem (adjust 300k to be near your original video bitrate):

ffmpeg -i bad_video.mpg -b 300k good_video.avi