jmap82
September 17th, 2008, 08:24 AM
I don't consider myself a programmer, but I do find it fascinating. I'm tinkering around with bash scripting just to see what I can do.
I wrote a simple script to focus my learning experience. I got it to work pretty much the way I want it, and now is the stage of my learning process where I ask experienced people to tell me how to do it better.
Synopsis
HandBrake (http://handbrake.fr/) is a powerful H.264 video encoder that processes DVD files for archiving. HandBrakeCLI is officially supported by Handbrake on Linux systems, and it is easy enough to learn. However, when archiving personal video collections there are two processes involved: Ripping and Encoding. The Ripping process requires the user to be at the console changing out DVDs and saving the files to an appropriate directory. The Encoding can be batch processed while you sleep. There is nothing that can be done about having to manually change DVDs but I thought I could save myself and others some time by writing a script that will create a batch file automatically, considering that the same settings are typically used for each video.
The Script
#!/bin/sh
#
#My humble attempt to write a script that will generate
#a batch file for HandBrakeCLI.
#
#
clear #Clear the screen for the following message.
cat << EOF
************************************************** ********
* Copy the HandBrakeCLI binary into the *
* /home/USER/ direcory if you want to run the *
* the batchfile upon completion. Feel free to alter *
* this script to suit your needs. *
* Message me at forums.ubuntu.com U/N jmap82. Thanks. *
* *
* Press Enter to continue. *
* *
************************************************** ********
EOF
read zzzz #Pause for user feedback.
cd /media/Love/RippedDVDs #A default start directory.
rm HBBatch #Removes prevous batch file to so a new one will be created and ensures this file is not included in ls or dir output.
echo "You are now in `pwd`" #Feedback for user.
echo "Files in this directory are:"; dir #Feedback for user.
echo "Is this where you want to be? ('y' or 'n')" #Opportunity for user to change directory without re-writing the script.
read ans #Listener for confirmation.
while [ $ans != y ] #Begin While loop to change directories.
do
echo "Where are your source DVD files? (address from root)"
read address #Listener for new source directory.
cd $address #Go to new source directory.
echo "You are now in `pwd`" #Feedback for user.
echo "Files in this directory are:"; dir #Feedback for user.
echo "Is this correct? ('y' or 'n')" #Request for confirmation.
read ans #Listener for Confirmation.
done #End While loop.
ls > /tmp/FileList
num=`cat /tmp/FileList|wc -l` #Counts the number of lines in the file (which is equal to the number of files in the folder) to provide an upper limit for the While loops.
i=1 #Sets counter to 1 for While loop.
while [ $i -le $num ] #While loop to plug file names into a string for the batch file.
do
name=`awk "FNR == $i" /tmp/FileList` #"FNR == $i" MUST be in DOUBLE QUOTES otherwise the variable will not be interpreted.
echo "$HOME/HandBrakeCLI -i `pwd`/$name -o $HOME/Videos/$name.mp4;">> `pwd`/HBBatch #Plugs all previously assigned variables into a command string for HandBrakeCLI. Other options can be added of course.
i=`expr $i + 1` #Increases value of $i by 1
done
rm /tmp/FileList #Clean up Temporary files.
chmod +x HBBatch #Prepare batch file for execution.
echo "Process Complete. Batch file as been created in `pwd`" #Feedback for user.
echo ""
echo "To begin encoding with HandBrakeCLI enter 'y'" #Request to process batch file.
read go #Listener for confirmation.
if [ $go = y ] #If loop to check the confirmation variable.
then
./HBBatch
else
exit 0
fi
I'm sure its pretty messy, but I'm still learning. I hope you guys can help me make it better :).
Thanks in advance.
jmap82.
I wrote a simple script to focus my learning experience. I got it to work pretty much the way I want it, and now is the stage of my learning process where I ask experienced people to tell me how to do it better.
Synopsis
HandBrake (http://handbrake.fr/) is a powerful H.264 video encoder that processes DVD files for archiving. HandBrakeCLI is officially supported by Handbrake on Linux systems, and it is easy enough to learn. However, when archiving personal video collections there are two processes involved: Ripping and Encoding. The Ripping process requires the user to be at the console changing out DVDs and saving the files to an appropriate directory. The Encoding can be batch processed while you sleep. There is nothing that can be done about having to manually change DVDs but I thought I could save myself and others some time by writing a script that will create a batch file automatically, considering that the same settings are typically used for each video.
The Script
#!/bin/sh
#
#My humble attempt to write a script that will generate
#a batch file for HandBrakeCLI.
#
#
clear #Clear the screen for the following message.
cat << EOF
************************************************** ********
* Copy the HandBrakeCLI binary into the *
* /home/USER/ direcory if you want to run the *
* the batchfile upon completion. Feel free to alter *
* this script to suit your needs. *
* Message me at forums.ubuntu.com U/N jmap82. Thanks. *
* *
* Press Enter to continue. *
* *
************************************************** ********
EOF
read zzzz #Pause for user feedback.
cd /media/Love/RippedDVDs #A default start directory.
rm HBBatch #Removes prevous batch file to so a new one will be created and ensures this file is not included in ls or dir output.
echo "You are now in `pwd`" #Feedback for user.
echo "Files in this directory are:"; dir #Feedback for user.
echo "Is this where you want to be? ('y' or 'n')" #Opportunity for user to change directory without re-writing the script.
read ans #Listener for confirmation.
while [ $ans != y ] #Begin While loop to change directories.
do
echo "Where are your source DVD files? (address from root)"
read address #Listener for new source directory.
cd $address #Go to new source directory.
echo "You are now in `pwd`" #Feedback for user.
echo "Files in this directory are:"; dir #Feedback for user.
echo "Is this correct? ('y' or 'n')" #Request for confirmation.
read ans #Listener for Confirmation.
done #End While loop.
ls > /tmp/FileList
num=`cat /tmp/FileList|wc -l` #Counts the number of lines in the file (which is equal to the number of files in the folder) to provide an upper limit for the While loops.
i=1 #Sets counter to 1 for While loop.
while [ $i -le $num ] #While loop to plug file names into a string for the batch file.
do
name=`awk "FNR == $i" /tmp/FileList` #"FNR == $i" MUST be in DOUBLE QUOTES otherwise the variable will not be interpreted.
echo "$HOME/HandBrakeCLI -i `pwd`/$name -o $HOME/Videos/$name.mp4;">> `pwd`/HBBatch #Plugs all previously assigned variables into a command string for HandBrakeCLI. Other options can be added of course.
i=`expr $i + 1` #Increases value of $i by 1
done
rm /tmp/FileList #Clean up Temporary files.
chmod +x HBBatch #Prepare batch file for execution.
echo "Process Complete. Batch file as been created in `pwd`" #Feedback for user.
echo ""
echo "To begin encoding with HandBrakeCLI enter 'y'" #Request to process batch file.
read go #Listener for confirmation.
if [ $go = y ] #If loop to check the confirmation variable.
then
./HBBatch
else
exit 0
fi
I'm sure its pretty messy, but I'm still learning. I hope you guys can help me make it better :).
Thanks in advance.
jmap82.