#!/bin/bash # # Real Media Ripper (C) 2006 Matthew Gates # # This program is distribued under the terms for the GNU GPL License, # Version 2. For more details, see http://www.gnu.org/copyleft/gpl.html # # Run with -h option for documentation. # verbose="> /dev/null 2>&1" audio_format=ogg video_format=mpeg dirty=0 main () { # Process command line options while getopts ":hvVda:f:" option; do case $option in h) usage 0 ;; v) echo "Real Media Ripper v0.1" echo "(C) 2006, Matthew Gates " exit 0 ;; V) verbose="" ;; d) dirty=1 ;; a) audio_format="$OPTARG" ;; f) video_format="$OPTARG" ;; *) echo "ERROR: unknown option: $option" 2>&1 ;; esac done shift $(($OPTIND - 1)) # check selected options which use cleartext parameters are valid shopt -s nocasematch case "$audio_format" in ogg|mp3|wav) echo "nop" > /dev/null ;; *) echo "ERROR: unknown audio format: $audio_format" 1>&2 echo "Valid formats: ogg mp3 wav" 1>&2 exit 1 ;; esac case "$video_format" in mpeg) echo "nop" > /dev/null ;; *) echo "ERROR: unknown video format: $audio_format." 1>&2 echo "Valid formats: mpeg" 1>&2 exit 1 ;; esac # OK, process the command line parameters. input="" output="" if [ $# -eq 2 ]; then output="$2" input="$1" elif [ $# -eq 1 ]; then input="$1" else echo "ERROR: should have 1 or 2 parameters" 1>&2 exit 1 fi check_programs ram="" rtsp="" case "$input" in http*ram) eval "echo 'RAM/URL mode' $verbose" ram="$input" rtsp="$(GET "$input" |sed 's/ *$//')" if [ "$output" = "" ]; then output="${input##*/}" output="${output%\.ram}" output="${output%\.RAM}" fi ;; rtsp*) eval "echo 'RTSP mode' $verbose" rtsp="$input" if [ "$output" = "" ]; then output="${input##*/}" output="${output%\.rm}" output="${output%\.RM}" output="${output%\.ra}" output="${output%\.RA}" fi ;; *ram) eval "echo 'RAM/FILE mode' $verbose" rtsp="$(cat "$input")" if [ "$output" = "" ]; then output="${input%\.ram}" output="${output%\.RAM}" fi ;; *) echo "ERROR: don't know how to handle target: $input" 1>&2 exit 1 ;; esac eval "echo 'RAM = $ram' $verbose" eval "echo 'RTSP = $rtsp' $verbose" eval "echo 'OUT = $output' $verbose" get_media "$rtsp" "$output" } get_media () { eval "echo 'Downloading stream... Make sure you have a LOT of free space!' $verbose" eval "mplayer -vo 'yuv4mpeg:file=$2.yuv' -ao 'pcm:file=$2.wav' '$1' $verbose" if ! [ -a "$2.yuv" ]; then encode_audio_only "$2.wav" fi eval "echo 'Transcoding temporary files to $video_format...' $verbose" case "$video_format" in mpeg) out_final="$2.mpg" if [ -a "$2.wav" ]; then eval "echo 'encoding video with audio' $verbose" eval "ffmpeg -i '$2.wav' -i '$2.yuv' '$out_final' $verbose" else eval "echo 'encoding video only' $verbose" eval "ffmpeg -i '$2.yuv' '$out_final' $verbose" fi lev=$? ;; *) echo "ERROR: (get_video) unknown video format $video_format" 1>&2 ;; esac if [ $dirty -eq 0 ]; then eval "echo 'Cleaning up...' $verbose" rm -f "$2.wav" "$2.yuv" fi eval "echo 'Here is you file:' $verbose" eval "ls -l '$out_final' $verbose" exit $lev } encode_audio_only () { case "$audio_format" in wav) output="$1" eval "echo 'wav format requested - no encoding to do' $verbose" lev=0 ;; ogg) output="${1%.wav}.ogg" eval "echo 'Encoding OGG/Vorbis audio' $verbose" eval "oggenc '$1' $verbose" lev=$? if [ $dirty -eq 0 ]; then eval "echo 'Cleaning up...' $verbose" rm -f "$1" fi ;; mp3) output="${1%.wav}.mp3" eval "echo 'Encoding MP3 audio' $verbose" eval "lame '$1' '$output' $verbose" lev=$? if [ $dirty -eq 0 ]; then eval "echo 'Cleaning up...' $verbose" rm -f "$1" fi ;; *) echo "ERROR: (encode_audio_only) unknown audio format $audio_format" 1>&2 ;; esac eval "echo 'Here is you file:' $verbose" eval "ls -l '$output' $verbose" exit $lev } echoerr () { lev="$1" shift echo "$@" 1>&2 exit $lev } check_programs () { for prog in mplayer ffmpeg GET oggenc lame; do which $prog > /dev/null || echoerr 2 "ERROR: Can't find $prog, please install it and make sure it's in the PATH" done } usage () { cat < [basename] Options: -h : this cruft -v : print name and version -V : verbose mode -d : dirty mode (don't delete intermediate files) -a : audio format (ogg [default], mp3, wav) -f : video format (mpeg [default]) EXAMPLES: \$ ${0##*/} -f mpeg http://foo.com/video.ram wiggle Downloads the URL, putting the result in "wiggle.mpg" (mpeg format) \$ ${0##*/} http://foo.com/audio.ram Downloads the URL, putting the result in audio.ogg (ogg/vorbis format) \$ ${0##*/} -a mp3 http://foo.com/audio.ram Downloads the URL, putting the result in audio.mp3 (mp3 format) \$ ${0##*/} -a wav http://foo.com/audio.ram Downloads the URL, putting the result in audio.wav (wav format) EOF exit ${1:-0} } main "$@"