Hello, I finally decided to sit down and learn how to write scripts in bash so I wrote one to encode any video files in a directory to DIVx using mencoder. I wanted it to only touch valid video files by checking the suffix at the end of the file. I used FILESUF=${file#*.} to remove everything but the suffix. I ran into a problem with files that have multiple '.'s in the name. Is there a way to start at the end of the sting going backwards and remove everything after (and including) the first '.' it finds? THanks for the help!
Here is the full code for anyone that's interested:
Code:
#!/bin/bash
# Converts all video in a directory to DIVx5.0
# Uses the "mencoder" binary from the "mplayer" package,
SUFFIX=avi # New filename suffix.
VALIDSUF=(mpg mpeg mjpeg mv rm rmvb asf mov) #removed wmv as it does not convert well
element_count=${#VALIDSUF[@]}
#process arguments
case "$1" in
--help | -h)
echo "Use encodedivx to encode all video media in a directory to the DIVx5.0 codec format"
echo
echo "Usage: encodedivx <directory>"
echo "will assume current directory if none specified"
exit 1
;;
*/) #directory argument?
DIR=$1
;;
*) #default
if [ -n "$1" ] #argument is given but invalid
then
echo "Usage: encodedivx <directory>"
exit 1
else #use current DIR if non specified
DIR=$PWD
echo DIR=$PWD
fi
esac
echo "Encode all video files in $DIR?: {Y|N}"
read VERIFY
while true #loop until valid input for $VERIFY
do
case "$VERIFY" in
"Y" | "y")
for file in $DIR/* #check files in $DIR
do
FILENAME=${file%.*} #break up filename
FILESUF=${file#*.}
i=0
while [ "$i" -lt "$element_count" ] #check if suffix is valid
do
if [ "${VALIDSUF[$i]}" == "$FILESUF" ] #if valid, encode
then
echo
mencoder $file -ovc lavc -oac lavc -ffourcc DX50 -o $FILENAME.$SUFFIX
fi
let "i=$i+1" #check next suffix
done
done
break
;;
"N" | "n")
echo "Quiting..."
exit 1
;;
*) #invalid input for $VERIFY
echo "Encode all video files in $DIR?: {Y|N}"
read VERIFY
esac
done
exit 0
I would be more then happy to hear any suggestions on how to improve this. This was my first attempt at a shell script and it seems kind of clunky to me.
thanks!
...drkstr