Thursday, December 22, 2011

MPlayer

I found this in Google's web cache after http://mydebian.blogdns.org disappeared, the first page is the one I originally saved but the other two look like they might also be helpful




July 21, 2007 | 20:13
This guide has been written using Mplayer version 1.0rc1 with a headless machine (without a monitor and/or video card); things could be different with other versions or configurations.
Mplayer is not only a versatile movie’s player but also a very powerful encoder. Webcam can be captured with it and the output is completely customisable: from a jpeg for every frame, to a divx video, or even an event driven capturer…the limit is just your imagination!
First, the source code for the latest version of mplayer/mencoder can be downloaded at mplayer headquarters’ site. In order to use it, an essential set of codecs must be obtained from the same page. To install them just type
# mkdir /usr/local/lib/codecs/
# cd /usr/local/lib/codecs
# tar xvfj /full/path/essential*.tar.bz2
and there you are. To compile and install mplayer you must do a little more:
$ ./configure
$ make
# make install
(“#” means you need to be root to run the command). The odds are you will get mplayer compiled without any problem.
Remember that if you plan to run mplayer in slave mode so that you can grab any screenshot, libpng has to be installed (before configure is executed) or it won’t work.
Well, mplayer is now installed in your system I suppose. If a webcam is correctly installed and v4l is working then you should be able to do many things:
capture 15 screenshots per second in jpeg format:
$ mplayer -tv driver=v4l:fps=15 -vo jpeg tv://
capture 30 screenshots per second with a custom width & height (the default one is probably too small) :
$ mplayer -tv driver=v4l:fps=15:height=288:width=352 -vo jpeg tv://
(if you set an illegal width and/or height you should probably obatin pink or green garbage)
Capture and encode in a resized mpeg4 video named movie.avi:
$ mencoder -tv driver=v4l:fps=15:height=288:width=352 -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=640:480 -o movie.avi tv://
Capture a frame in a png picture only when you want it:
$ mkfifo /tmp/test.fifo
$ mplayer -slave -quiet -input file=/tmp/test.fifo -vf screenshot -tv driver=v4l tv:// &
Then when you want to capture just type:
$ echo "screenshot 0" >/tmp/test.fifo
Alternatively you can also start a continuos capture by typing:
$ echo "screenshot 1" >/tmp/test.fifo
(type it again to stop capture)

A practical example: an html page containing a refreshing captured picture

I am assuming you already have Apache2 running and that it’s pointing to your filesystem directory /var/www (that is if you put text.html in /var/www/ everyone can see it in http://yoursite.example/text.html).
Write this code in webcam.sh
#!/bin/bash

[ -p /tmp/my.fifo ] && rm /tmp/my.fifo
mkfifo my.fifo

mplayer -slave -quiet -input file=/tmp/my.fifo -vf screenshot,scale=640:480 -tv driver=v4l:width=352:height=288 tv:// &

while true
do
echo "screenshot 0" >/tmp/my.fifo
sleep 10
name=`ls -tr shot*.png | tail -n1`
mv $name /var/www/lastshot.png
sleep 50
done
The delay of 60 seconds is splitted in two parts because we don’t know when the image will be ready to be moved. This solution is not much elegant but it should work for every configuration. Alternatively, with ImageMagick installed you could use something like:
$ identify shot*.png >/dev/null 2>&1; while [ $? -ne 0 ]; do identify shot*.png >/dev/null 2>&1; done
The above command waits until a valid png picture is present in the current directory (man identify to get more info).
Now you need only to create the following page in /var/www/:
<html>
   <head>
      <meta http-equiv="refresh" content="60" />
      <title>Webcam shot every minute!</title>
   </head>

   <body bgcolor="black" text="white">
      <img src="lastshot.png" />
   </body>
</html >
Finished! To make it working just type:
$ nohup sh webcam.sh &
and visit http://yoursite.example/thepageyouhavejustcreated.html
Hope this helps!

Why mplayer doesn’t work in some scripts

May 15, 2008 | 8:59
Sometimes you need to get mplayer/mencoder inside a script. Let’s say you want to automatically grab a picture from your webcam every 10 seconds:
capture_w=here goes the capture width of your webcam
capture_h=and here the height
mplayer -slave -quiet -input file=/tmp/tmp.fifo \
        -vf screenshot,eq2,scale=500:375 \
        -tv driver=v4l:quality=90:height=$capture_h:width=$capture_w:input=0:norm=pal:\
 noaudio:contrast=10:brightness=-25:saturation=100 tv:// \
        -vo null &>/dev/null&
while :; do
 echo "screenshot 0" >/tmp/tmp.fifo
 sleep 10;
done
(parameters of the tv option may vary for your webcam)
or encode the last grabbed frames of the webcam in a video:
video_codec=mpeg4;
mencoder "mf://*.jpg" -mf fps=10 -o where/to/put/the/movie/temp.avi \
                                -ovc lavc -lavcopts vcodec=$video_codec
or download a realaudio media streaming in wav format :
mplayer -bandwidth 10000000 -vc null -vo null \
-ao pcm:fast:file=output.wav http://example.com/file
Please note the bandwidth option: it makes mplayer reading the stream many times faster than the normal speed.
For all the above cases you may have at least one problem: if mplayer goes inside a “while read” or more generally at the right end of a pipe, you could get some nasty error messages like these:
while read foo; do
mplayer ......
done < <(command arg)
[..]
Cache fill:  0.00% (0 bytes)   No bind found for key ':'.
Cache fill:  0.00% (0 bytes)   No bind found for key 'c'.
Cache fill:  0.00% (0 bytes)   No bind found for key 'l'. 
The cause of that strange output is that mplayer reads from the input (int our case the next output line of foocommand) and try to interpret it as a list of commands; this is not what we generally want. Fortunately, making mplayer ignore "its" standard input is very simple: just append a </dev/null to its command line or use -noconsolecontrols which is there for it.


Howto to remove “Couldn’t resolve name for AF_INET6″ error message…

August 14, 2007 | 15:04
… when using mplayer with Internet video streaming.
You need only to edit ~/.mplayer/config by adding this line:
prefer-ipv4 = yes
Done! Now when you run mplayer you won’t be bother anymore by that utterly pointless message

Again I did not write the three pages above but since they are no longer hosted to my knowledge I am mirroring them here.

No comments:

Post a Comment