Mplayer notes

From Noah.org
Jump to: navigation, search


These are my notes on MPlayer and MEncoder and ffmpeg.

See also gstreamer.

Mac OS X playback

You might first need to install mplayer' (this also installs mencoder.=). I also often use imagemagick with mplayer, so I install both.

sudo brew install mplayer
sudo brew install imagemagick

The key trick to getting mplayer from the command-line to play video on Mac OS X is to specify the -vo core video option. For some reason this key piece of information was hard to find (didn't actually read the docs). Maybe I was having bad luck with Google that day.

mplayer "mf://*.png" -vo corevideo -mf type=png;fps=30
# or
mplayer "mf://*.jpg" -vo corevideo -mf type=jpg;fps=30

Merge two videos into side-by-side stereo 3D

See also to record from two cameras directly into a side-by-side stereo video: gstreamer#record_two_video_cameras_into_side-by-side_stereo_video.

This is untested. This is a work in progress.

ffmpeg -i left.mov frames-left-%09d.png
ffmpeg -i right.mov frames-right-%09d.png
number_frames=$(ls frames-left-*.png | wc -l)
cc=1
while [ $cc -lt $number_frames ]; do 
    index=$(printf "%09d" $cc)
    montage frames-left-${index}.png frames-right-${index}.png -geometry 640x480+0+0 frames-sbs-${index}.png 
    let a++
done
# ffmpeg -i %03d.png -i frames-sbs-*.png -an -vcodec copy stereo3d.mov
mencoder mf://frames-sbs-*.png -mf type=png:w=640:h=480:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:fast_pskip=0:threads=auto:crf=20 -o stereo3d.mov

Also, for ImageMagick see the composite command with the stereo option.

composite -stereo frames-left-0.png frames-right-0.png anaglyph.png

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: "Unsupported PixelFormat 31"

This happens when trying to play a group of 16-bit PNG images as a video. The fix is to convert the images to 8-bit PNG format.

mogrify -depth 8 *.png

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 -

fbdev -- Mplayer video output on framebuffer device (/dev/fbdev0 or /dev/fb0)

If you want to display video on a console not running X11 then you will need to talk directly to the frame buffer device. Be sure to use sudo or run command as root or add your user to the group that owns the fb device (group video on Ubuntu.

sudo mplayer -vo fbdev "mf://*.jpg" -mf fps=20 -loop 0

You may need to specify fbdev2 on some systems if you want color.

sudo mplayer -vo fbdev2 ct_scan_sample.mp4

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

If your original source video is in MJPEG format 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

Using mplay the following will split a video into individual frames. Tonvert 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

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:threads=auto:crf=18 -o output.mov

You can modify the width and height if necessary:

# 720p (1280 x 720)
mencoder mf://*.jpg -mf type=jpeg:w=1280:h=720:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:threads=auto:crf=18 -o output.mov
# 1080p (1920 × 1080)
mencoder mf://*.jpg -mf type=jpeg:w=1920:h=1080:fps=30 -ovc x264 -x264encopts preset=slow:tune=film:threads=auto:crf=18 -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: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 -- play directory of jpeg or png images as video

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.

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

Playing PNG images is a simple change.

mplayer "mf://*.png" -mf fps=30

You should not need to, but you may explicitly specify the still image format.

mplayer "mf://*" -mf type=jpg:fps=30

You may also want the playback to loop back to the begnning for endless playback.

mplayer "mf://*" -mf type=jpg:fps=30 -loop 0

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. It's fun to look at. I gives a bit of insight into how video motion compression works.

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