handleMedia.sh

Being partially incapacitated has given me the opportunity to make some tweaks to my personal scripts which deal with all the incoming media formats I pull in using SABnzbd on my Linux box. For a long time I have been using separate scripts to deal with different files types, and I finally decided to make one script that automatically chooses which actions to take based on the file type found in the Downloads folder that SABnzbd dumps its content into. Without further ado, here is the new master script!

#!/bin/bash

#/usr/local/bin/handleMedia.sh
#
# Jay Holler 09-10-2009
# Find files in the Downloads directory and move them to
# the appropriate folder on the NFS share or put them in the Dropbox directory for syncing to iTunes.

## Move to the Downloads directory that SABnzbd uses

cd /home/jayholler/Downloads

###############################################
## Start the avi matching portion of the script
###############################################

if [ -e *.avi ] || [ */*.avi ] || [ -e Movies/*.avi ] || [ -e Movies/*/*.avi ]; then

storageDir=/storage/TV

mv */*.avi /home/jayholler/Downloads
mv Movies/*.avi /home/jayholler/Downloads
mv Movies/*/*.avi /home/jayholler/Downloads
mv TV/*/*.avi /home/jayholler/Downloads
mv TV/*.avi /home/jayholler/Downloads

for i in *.avi
do
myFile=”$i”

shopt -s nocasematch
if [[ “$myFile” =~ ([A-Za-z0-9\.]*)\.(S..E..).*.avi ]] ; then
File=${BASH_REMATCH[2]}.avi
PreShowName=${BASH_REMATCH[1]}
ShowName=$(echo $PreShowName | sed ‘s/\./ /g’)
echo “[*] A new episode of $ShowName is ready for your enjoyment: $File” > /tmp/emailmessage.txt && sleep 2
[ -d “$storageDir/$ShowName” ] || mkdir -v “$storageDir/$ShowName”
mv -v $myFile “$storageDir/$ShowName/$File”
ln -s “$storageDir/$ShowName/$File” “/storage/Unwatched/$myFile”
wget http://localhost:XXXXX/web/ushare.cgi?action=refresh -o /dev/null -P /dev/null
mumbles-send -g qu0x “`basename $0`” “`cat /tmp/emailmessage.txt`”
/usr/local/bin/prowl.pl -application=”`basename $0`” -event=”New Show!” -notification=”`cat /tmp/emailmessage.txt`” -priority=1 -apikey=NOTTODAY
/usr/local/bin/email.sh
else
echo “[+] $myFile does not match our TV shows, pushing it to /storage/Movies on lunchbox” > /tmp/emailmessage.txt
mv -v “$myFile” “/storage/Movies/$myFile”
/usr/local/bin/prowl.pl -application=”`basename $0`” -event=”New Movie!” -notification=”`cat /tmp/emailmessage.txt`” -priority=1 -apikey=NOTTODAY
/usr/local/bin/email.sh
fi
shopt -u nocasematch
done

fi

#########################################
## End avi matching portion of the script
#########################################

###########################################
## Begin mp3 matching portion of the script
###########################################

if [ -e *.mp3 ] || [ -e Music/*.mp3 ] || [ -e Music/*/*.mp3 ] ; then
mv */*.mp3 /home/jayholler/Dropbox
mv Music/*.mp3 /home/jayholler/Dropbox
mv Music/*/*.mp3 /home/jayholler/Dropbox
echo “Moved a new album of mp3s to your Dropbox folder!” > /tmp/emailmessage.txt
mumbles-send -g qu0x “`basename $0`” “`cat /tmp/emailmessage.txt`”
/usr/local/bin/prowl.pl -application=”`basename $0`” -event=”New mp3s!” -notification=”`cat /tmp/emailmessage.txt`” -priority=1 -apikey=NOTTODAY
f7f099
fi

#########################################
## End mp3 matching portion of the script
#########################################

###########################################
## Start mkv matching portion of the script
###########################################
if [ -e *.mkv ] ; then
### Transcode 720p H264 mkv files for the Apple TV

if [ -z “$1” ]; then
echo “Usage: `basename $0` filename”
exit 0
fi

# Wait while any other ffmpeg processes are running
while [ -n “$(ps -ef | egrep “ffmpeg|HandBrakeCLI” | grep -v grep)” ];
do
echo -e “\n[$(date +%b\ %d\ %Y:\ %H:%M:%S)]\nFound another instance of HandBrake or ffmpeg running, pausing 5 minutes…”
sleep 300
done

# Get the beginning time from the date cmd.
START=$(date +%D\ %T)

# Email us that the next process has begun
echo -e “About to start transcoding $1 at $START” > /tmp/emailmessage.txt
/usr/local/bin/email.sh

#HandBrakeCLI cmd
HandBrakeCLI -Z “QuickTime” -i “$1” -o “$1.m4v”

# Get the ending time of the transcode process from the date cmd.
END=$(date +%D\ %T)

# Inform us with an email that transcoding is completed
echo “Transcoding of $1 was started on $START and completed on $END.” > /tmp/emailmessage.txt
/usr/local/bin/prowl.pl -application=”`basename $0`” -event=”Transcoding Complete” -notification=”`cat /tmp/emailmessage.txt`” -priority=1 -apikey=NOTTODAY

# Move the original mkv file to the backup at /mkvs
mv -v “$1” /mkvs

# Chop off the .mkv from the file name
transcodedFile=`echo “$1.m4v” | sed -e ‘s/\.mkv//’`
mv “$1.m4v” “$transcodedFile”

# Determine the appropriate place to move the file and do so
/usr/local/bin/moveMp4s.sh “$transcodedFile”
fi

#########################################
## End mkv matching portion of the script
#########################################

exit 0

Advertisements

Author: jayholler

A technology lover living in California with my wife and two children.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s