Saving video streams with mencoder on Linux

The following script uses the mencoder command to read a live video feed and save it to a file for playback. It could probably be modified or used as it is for audio, but I did not try it. It was custom-made to read a stream in the asf format and encoded in WMV3 (Windows Media Video 9), but I assume that it could be parametrized to take and output other formats. The benefit of this script is that it does not require any transformation or recoding of the stream, as it just copies (or dumps) the contents of the stream to a file, and packages it in its native format, that's supported by FFmpeg's libavformat library.

# Reads a live stream and copies its contents to a file

NOW=`date +%Y%m%d%H%M`;

if [ $# -ne 3 ]
    echo "Usage: [stream url] [duration] [identifier]"
    exit 1
    STREAM_URL=${1}     DURATION=${2}
    IDENTIFIER=${3}     mencoder -forceidx -oac copy -ovc copy -endpos ${DURATION} -vf harddup ${STREAM_URL} \     -of lavf -lavfopts format=${FORMAT} -o ${DLDIR}/${IDENTIFIER}${NOW}.${EXTENSION} fi exit 0

Download it:

Whatever you do with this script is your business. Please be careful about legal questions with respect to copyright when you save stream segments.

You need to have MPlayer/mencoder installed. It works best when you have the latest codecs installed, and this script should only work when you have FFmpeg installed too (although I think you could modify it to do without). You might also need the w32codecs binary codecs package.To get the latest versions of these packages, on Debian, try looking at Debian Multimedia, and for Ubuntu, check out Medibuntu. I did not test it thoroughly, so beware when you use the script, and be sure that you know what you are doing! The script works very well as a cron job.

For the stream that I've been using this on, I noticed that the video was out of sync for the first 15-30 seconds, but that it corrected itself afterwards. I am not sure if this is a server-side issue, as I did not try it on other streams.

The -forceidx option is required for WMV to be re-indexed. I struggled a bit with previous incarnations of the script, using FFmpeg and MPlayer, because they would not reindex. The wmv files produced by mencoder in the script can be used with the Windows Media File Editor GUI (part of Windows Media Encoder - comes free with Windows, but needs to be installed separately).


, ,

Leave a comment

Type the characters you see in the picture above.

About this Entry

This page contains a single entry by Cedric published on November 4, 2007 5:57 PM.

Restaurant Congee (豐衣粥食) in Brossard was the previous entry in this blog.

Stars in Montreal on Nov 30th / Dec 1st is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.