Code: Select all
#!/bin/bash
# 1 The final directory of the job (full path)
# 2 The name of the NZB file
# 3 Clean version of the job name (no path info and ".nzb" removed)
# 4 Newzbin report number (may be empty
# 5 Newzbin or user-defined category
# 6 Group that the NZB was posted in e.g. alt.binaries.x
DIR=$1
NZB_FILE=$2
NAME=$3
NZB_ID=$4
CATEGORY=$5
GROUP=$6
echo ""
echo "Directory: "$DIR
echo "NZB Filename: "$NZB_FILE
echo "Job Name: "$NAME
echo "NZB ID: "$NZB_ID
echo "Category: "$CATEGORY
echo "Usenet Group: "$GROUP
echo ""
echo ""
# wont print error in for loop if there are no avi files in the directory
shopt -s nullglob
# regex to match job name to strip out show name, season number, episode number and episode name
# in multi part episodes (eg: Show - 1x01-1x02 - Episode), the first episode (1 in this case) is used
regex="^(.*) - ([[:digit:]])+x([[:digit:]]+).* - (.*)$"
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi *.mkv; do
NAME=${i%.*}
# if there is already an .mp4 file continue
if [[ -e "$NAME - iPhone.mp4" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
#HandBrakeCLI -C 1 -i "$i" -o "$NAME - iPhone_processing.mp4" --preset="iPhone & iPod Touch"
HandBrakeCLI -i "$i" -o "$NAME - iPhone_processing.mp4" --preset="iPhone & iPod Touch"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
mv "$NAME - iPhone_processing.mp4" "$NAME - iPhone.mp4"
if [[ $CATEGORY -eq "tv" && $NAME =~ $regex ]] ; then
show_name=${BASH_REMATCH[1]}
season=${BASH_REMATCH[2]}
episode=${BASH_REMATCH[3]}
episode_name=${BASH_REMATCH[4]}
episode=`echo $episode | sed 's/0*//'`
fi
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/$NAME - iPhone.mp4"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv" and "$show_name" ≠ "" then
set video kind of video to TV Show
set show of video to "$show_name"
set season number of video to "$season"
set episode number of video to "$episode"
set episode ID of video to "$episode_name"
else
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0