Linux video conversion shell script
Posted: Sun Jun 20, 2010 1:34 pm
For your viewing pleasure!
First: a brief description:
What this script will do:
Convert your video files (anything that mencoder can read) into an two=pass mpeg4/mp3 avi file.
Accept inputs for desired video bitrate, cropping, audio file, framerate, minimum title length.
Read the data from a dvd, skipping titles under a certain length.
Accept inputs in the form of command line options, or prompt you for the info that it needs.
What it won't do:
Give you any choice about what codec to use.
Be completely dummyproof.
Be necessarily bug-free.
Give you any choice about what specific dvd titles to read.
Work in windows (though, I'm sure it can be translated... any volunteers?)
Give you any choice in terms of the video filters that it uses, beyond cropping.
How to get started:
invoke the script with --help.
read through it (it's not really all that long).
ridicule my silly engineering-ish coding.
I made it because I was tired of having to look up the command line switches to use with mencoder each time I wanted to compress something, at at the same time I didn't really want to move over to a GUI-based programs, so I made myself this script. You can invoke it without any command line options, and it will ask you for everything that it needs.... so you don't have to remember anything other than where you put the script.
Prerequisites:
And, the actual script code: (copy-paste it into a file, and flag as executable)
enjoy!
EDIT: updated the code per a bug a found... now it can handle files with spaces in the name as long as you pass it with quotation marks.
Also eliminated the search for a -ch switch that I was thinking about implementing at one point, but then didn't.
First: a brief description:
What this script will do:
Convert your video files (anything that mencoder can read) into an two=pass mpeg4/mp3 avi file.
Accept inputs for desired video bitrate, cropping, audio file, framerate, minimum title length.
Read the data from a dvd, skipping titles under a certain length.
Accept inputs in the form of command line options, or prompt you for the info that it needs.
What it won't do:
Give you any choice about what codec to use.
Be completely dummyproof.
Be necessarily bug-free.
Give you any choice about what specific dvd titles to read.
Work in windows (though, I'm sure it can be translated... any volunteers?)
Give you any choice in terms of the video filters that it uses, beyond cropping.
How to get started:
invoke the script with --help.
read through it (it's not really all that long).
ridicule my silly engineering-ish coding.
I made it because I was tired of having to look up the command line switches to use with mencoder each time I wanted to compress something, at at the same time I didn't really want to move over to a GUI-based programs, so I made myself this script. You can invoke it without any command line options, and it will ask you for everything that it needs.... so you don't have to remember anything other than where you put the script.
Prerequisites:
Code: Select all
sudo apt-get install mencoder lsdvd
Code: Select all
#!/bin/sh
audiooption=\"\"
outfile=\"\"
bitrate=\"\"
audio=\"\"
framerate=\"\"
crop=\"\"
length=\"2\"
input=\"\"
echo \"This script will compress/convert videos for you into xvid video & mp3 audio\"
echo \"Use --help for info on parameters this scripts can receive\"
# command line options to look for:
if [ \"$1\" = \"--help\" ]; then
echo \"Usage: videocompress.sh [-o outfile -b bitrate -a audiofile -f framerate -c crop -l mm:ss] [infile]\"
echo \"-o Output file name, .avi will be appended\"
echo \"\"
echo \"-b Video bitrate\"
echo \" 350-400 kBit is about right for 320x240 video\"
echo \" 900-1100 kBit is about right for 640x480 video - myth does 1000\"
echo \" 1400-2000 kBit is about right for HD video - myth does 1500\"
echo \"\"
echo \"-a Optional audio to use instead of audio from video file; use \\\"0\\\" to specify none\"
echo \"\"
echo \"-f Desired output framerate. Expects a number, greater than 10. \\\"ntsc\\\" can be used, too\"
echo \"\"
echo \"-c Desired cropping. In width:height:x:y; where values may be omitted, height and width\"
echo \" must be multiples of 16. Use \\\"n\\\" for no cropping, use \\\"y\\\" for default 720:400\"
echo \"\"
echo \"-l Minimum track length (dvd only) in minutes. Causes the scripts to skip over any tracks\"
echo \" under the specified length. Defaults to 2 minutes. Will not be prompted for.\"
echo \"\"
echo \"infile Video file to be used. Also accepts \\\"dvd://\\\".\"
echo \"\"
echo \"Any parameter not passed, with the exception of -l and -ch, will result in a prompt for the needed value.\"
exit 0
fi
# parsing through the parameters passed
until [ \"$1\" = \"\" ]
do
if [ \"$1\" = \"-o\" ]; then
shift
outfile=$1
shift
elif [ \"$1\" = \"-b\" ]; then
shift
bitrate=$1
shift
elif [ \"$1\" = \"-a\" ]; then
shift
audio=$1
shift
elif [ \"$1\" = \"-f\" ]; then
shift
framerate=$1
shift
elif [ \"$1\" = \"-c\" ]; then
shift
crop=$1
shift
elif [ \"$1\" = \"-l\" ]; then
shift
length=$1
shift
elif [ -f \"$1\" ] || [ \"$1\" = \"dvd://\" ]; then
input=\"$1\"
shift
else
echo \"$1 not recognized or not a valid input file\"
shift
fi
done
if [ \"$input\" = \"dvd://\" ]; then
echo \"Using dvd as input\"
elif [ -n \"$input\" ]; then
echo \"Using $input as source file\"
else
echo \"Valid input not found\"
read -p \"Source file: (or dvd://)\" input
if [ -f \"$input\" ]; then
echo \"$input found!\"
elif [ \"$input\" = \"dvd://\" ]; then
echo \"Using dvd\"
else
echo \"Valid input not found\"
exit 0
fi
fi
if [ -n \"$outfile\" ]; then
echo \"Using $outfile as output file name\"
else
read -p \"Output file name? (.avi will be appended): \" outfile
fi
if [ -n \"$bitrate\" ]; then
echo \"Using a bitrate of $bitrate\"
else
echo \"Select a video bitrate:\"
echo \"350-400 kBit is about right for 320x240 video\"
echo \"900-1100 kBit is about right for 640x480 video - myth does 1000\"
echo \"1400-2000 kBit is about right for HD video - myth does 1500\"
read -p \"Target video bitrate (kBit): \" bitrate
fi
if [ \"$framerate\" = \"ntsc\" ]; then
echo \"Using NTSC framerate (29.97fps)\"
framerate=30000/1001
elif [ -z \"$framerate\" ]; then
read -p \"Select a framerate, greater than 10 (leave blank for NTSC): \" framerate
fi
if [ -z \"$framerate\" ]; then
echo \"Using NTSC framerate (29.97fps)\"
framerate=30000/1001
else
echo \"Using $framerate as framerate\"
fi
if [ \"$crop\" = \"n\" ]; then
echo \"No crop being applied\"
videofilter=\"-vf pullup,softskip,harddup\"
elif [ \"$crop\" = \"y\" ]; then
echo \"Cropping to 720:400\"
crop=\"720:400\"
videofilter=\"-vf crop=$crop,pullup,softskip,harddup\"
elif [ -n \"$crop\" ]; then
echo \"cropping to $crop\"
videofilter=\"-vf crop=$crop,pullup,softskip,harddup\"
else
read -p \"do you want to crop the video? (y)\" crop
if [ \"$crop\" = \"y\" ]; then
echo \"both width and height must be a multiple of 16\"
read -p \"Enter the crop size, in width:height:x:y - 720:400 for tv-recorded video:\" crop
videofilter=\"-vf crop=$crop,pullup,softskip,harddup\"
else
videofilter=\"-vf pullup,softskip,harddup\"
fi
fi
if [ \"$audio\" = \"0\" ]; then
echo \"Using video source file audio\"
elif [ -z \"$audio\" ]; then
echo \"If you want to use audio other than that of the video file, specify it\"
read -p \"Audio file (leave blank to use audio from source file): \" audio
elif [ -f $audio ]; then
echo \"Using $audio for audio file\"
audiooption=\"-audiofile \"$audio\"\"
else
echo \"Audio file not found, using video files's audio\"
fi
if [ \"$input\" = \"dvd://\" ]; then
titles=`lsdvd | grep Title: | wc -l`
echo \"`expr $titles - 1` Titles\"
count=1
until [ $count -eq $titles ]
do
echo \"Title $count\"
line=`expr $count + 1`
time=`lsdvd | grep Title: | sed -n \"$line p\" | awk {'print $4'} | tr [=:=] \" \"`
hr=`echo \"$time\" | awk {'print $1'}`
min=`echo \"$time\" | awk {'print $2'}`
hr=`expr \"$hr\" '*' 60`
time=`expr $hr + $min`
if [ \"$time\" -ge \"$length\" ]; then
mencoder dvd://$count -sws 9 $videofilter -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$bitrate:vpass=1:threads=3 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -quiet -o /dev/null
mencoder dvd://$count -sws 9 $videofilter -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$bitrate:vpass=2:threads=3 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -quiet -o \"$outfile-$count.avi\"
else
echo \"Title not long enough ($time minutes), skipping\"
fi
count=`expr $count + 1`
done
else
mencoder -sws 9 $videofilter -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$bitrate:vpass=1:threads=3 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -quiet -o /dev/null \"$input\"
mencoder -sws 9 $videofilter -oac mp3lame -ovc lavc -lavcopts vcodec=mpeg4:vbitrate=$bitrate:vpass=2:threads=3 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -quiet -o \"$outfile.avi\" \"$input\"
# alternate, codec, xvid.
# mencoder -sws 9 $videofilter -oac mp3lame -ovc xvid -xvidencopts bitrate=$bitrate:pass=1 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -o /dev/null \"$input\"
# mencoder -sws 9 $videofilter -oac mp3lame -ovc xvid -xvidencopts bitrate=$bitrate:pass=2 -ofps $framerate -af channels=2 -lameopts cbr:br=128 $audiooption -o \"$outfile.avi\" \"$input\"
fi
rm divx2pass.log
EDIT: updated the code per a bug a found... now it can handle files with spaces in the name as long as you pass it with quotation marks.
Also eliminated the search for a -ch switch that I was thinking about implementing at one point, but then didn't.