Flylib.com

Books Software

 
 
 

Hack82.Rip Streaming Video


Hack 82. Rip Streaming Video

Use MPlayer to rip streaming video feeds directly to a file for later viewing .

With broadband connections becoming more and more the norm, many web sites are providing not just streaming audio but even streaming video content. Like with streaming audio, sometimes you would like to save a streaming video feed to a file for later (possibly offline) viewing. [Hack #81] discusses how to use streamripper to rip audio feeds, which it does well, but to rip video feeds you need to use a tool such as Mplayer.

MPlayer is an incredibly flexible video and audio player (for more information on MPlayer check out [Hack #48] ). MPlayer supports a wide variety of audio and video formats including streaming audio and video and can also dump the raw video and audio streams directly to a file.

The first step to rip a stream is to get the URL for the streaming video. In some cases this is as easy as right-clicking a link on the web page and selecting Copy Link. Some video streams are embedded in a web page, so you might have to view the page's source code to find the direct link to the stream (many Quicktime feeds are like this). After you have found the URL, the next step is to play a bit of the stream to confirm that MPlayer can, in fact, access it:


$ mplayer


http://movies.sample.com/example.mov


Replace the URL with the path to the video stream you want to play. After MPlayer does some initial caching of the streaming content, you will see the video playback in a window. If the video doesn't play, check the error output in the console for some reasons why. MPlayer might not have all of the codecs it needs to play the video, so check out [Hack #53] to make sure you have all of the codecs you need.

After MPlayer has successfully played part of the video, hit Ctrl-C to stop playback, then add two extra options to rip the stream:


$ mplayer


URL


-dumpstream -dumpfile


filename


This command sets MPlayer in a special mode to dump the streaming content directly to the file you specify with the -dumpfile argument. Replace filename with the name of the output file you wish to use. Keep in mind that this is a raw output file directly from the stream, so to play it back in other video players you may need to convert it to a more universal format. For more information on converting video files between formats check out [Hack #63] .



Hack 83. Command-Line Streaming MP3 Player

Use basic command-line tools to create your own streaming MP3 player .

When setting up Obsequeium (http://obsbox.sf.net) or Jinzora (http:// jinzora .org) web jukeboxes [Hack #84] , I've found it helpful to set up a dedicated streaming player so I can listen to my net jukebox constantly while testing. Preferably the MP3 stream should never stop and, should something happen to the playback, it should pause for a moment and try the stream again. It may seem the mpg123/mpg321 command-line players are perfect for this task, but they have a habit of locking up and not exiting or retrying when something goes wrong with the stream. This makes them unsuitable for use as dedicated streaming players.

You can build a robust command-line streaming player with one command if you have madplay and wget installed. wget and madplay are both popular programs and should be prepackaged by your Linux distribution. Use your distribution's software installation tool to install these programs. If for some reason you don't have these tools prepackaged, download the tarballs from http://www.underbit.com/products/mad/ and http://www.gnu.org/software/wget/wget.html, compile, and install them according to the included installation instructions.

With wget and madplay installed, the following command will play the stream from http://example.com/mystream :


$ wgetqO


http://example.com/mystream


madplay -Q --no-tty-control-


wget reads the MP3 stream and quietly writes it to stdout which gets piped to madplay' sstdin . From there, madplay decodes the stream and writes it to the default sound device. The Q and no-tty-control options tell madplay to play the music without any text output and places the process in the background so it won't take over the terminal.

There is one more gotcha to overcome before our robust streaming MP3 player can run for days on end. MP3 streams that run for days have this nasty habit of dying right as you're grooving to your favorite track. To prevent this from happening, call the streaming player in a simple shell script loop:

#!/bin/sh
	while [ 1 ]
	do
		wget -q -O -http://example.com/mystream  madplay -Q --no-tty-control-
	sleep 5
	done

Should something happen with the stream and the player exits, it will pause for five seconds and then restart the stream again. The five-second pause prevents the player from pummeling your streaming server with connection requests in case something happens with the server.

Finally, to ensure the robustness of the command-line player, I recommend killing the player once a day at a slack time to restart the stream and make sure that it doesn't die in the middle of your favorite track. Use cron to accomplish this by adding this line to your crontab (edit your crontab with crontab e):

0 4 * * * killall9 madplay

This causes cron to kill madplay each day at 4 A.M. Pick a time when you expect the least number of people to listen, because listeners will need to sit through five seconds of silence.

That completes our hassle-free robust streaming MP3 player. This player configuration has played music for me for nearly two years without requiring human attention.

Robert Kaye