?

Log in

 
 
26 December 2008 @ 02:58 pm
flv2avi: Transcode Flash Video files into AVI files (or any other type mplayer understands)  
Gonna put the SSH over SSL epic on hold for a while.  Thought I'd do some simple scripts, but ones that follow a theme.  The theme is video processing, and most will use my favorite video playing & manipulating software, mplayer/mencoder.  You've already seen bbc7stream and aviid, both of which used mplayer.  This one converts Flash video into AVI files.  It's a very simple shell script.

BEGIN USAGE
jdimpson@artoo:~/bin$ flv2avi
Usage: flv2avi <Flash Video file>.flv
jdimpson@artoo:~/bin$
END USAGE

BEGIN CODE
#!/bin/sh

FLV=$1;
if [ -z "$FLV" ]; then
        ME=`basename $0`;
        echo "Usage: $ME <Flash Video file>.flv"
        exit 1;
fi
AVI=`basename "$FLV" .flv`.avi;

OFPS=`mplayer -identify -vo null -ao null -frames 1 "$FLV" 2>&1 | grep ID_VIDEO_FPS | sed -e 's/.*=\(.*\)\..*/\1/'`;

if [ -z $OFPS ];
then
        echo couldnt determine FPS, using 15;
        OFPS=15;
fi

ABITRATE=`mplayer -identify -vo null -ao null -frames 1 "$FLV" 2>&1 | grep ID_AUDIO_BITRATE | tail -1 | sed -e 's/.*=\(.*\).../\1/'`;

if [ -z $ABITRATE ];
then
        echo couldnt determine audio bitrate, using 32;
        ABITRATE=32;
fi

mencoder "$FLV" -ofps "$OFPS"  -oac lavc -ovc lavc -lavcopts vcodec=msmpeg4v2:acodec=mp3:abitrate=$ABITRATE -o "$AVI";
RET=$?;

if [ $RET -eq 0 ]; then
        echo sucess;
else
        echo failed "$FLV";
        rm -f "$AVI";
fi

exit $RET;
 
END CODE

This is really just a wrapper around mencoder, which is part of the mplayer suite.  mencoder already knows how to read FLV files.  Unfortunately, it doesn't seem to know how to automatically preserve the quality of the video file when converting into another format.  Thus before starting the transcode, there are two calls to mplayer, in identify mode, to figure out the audio bitrate and the video frames per second.  If you're you/'re a regular reader of my blog, you'll realize that mplayer is being used just like it was in my aviid script.

I test the return codes and such because when mencoder encounters an error midway through transcoding, it doesn't delete the partial output.  So I do that in the script.

By the way, you can convert to any video format, not just avi.  But you'll need to figure out they right syntax to mencoder to make that happen yourself.