I wrote this script for OS X that uses HandBrakeCLI to convert tv shows in AVI format to iPhone format and add them to iTunes. I thought I would share for others and/or feedback.
#!/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 "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:]]+).* - (.*)$"
# perform regex
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=$((episode+0))
episode=`echo $episode | sed 's/0*//'`
fi
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi; do
# if there is already an .mp4 file continue
if [[ -e "${i%.*}"" - iPhone.mp4" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
HandBrakeCLI -i "$i" -o "${i%.*}"" - iPhone.mp4" --preset="iPhone & iPod Touch"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
# add converted video file to itunes
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/${i%.*} - iPhone.mp4"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv"
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 if "$CATEGORY" = "movies"
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0
#!/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 "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:]]+).* - (.*)$"
# perform regex
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=$((episode+0))
episode=`echo $episode | sed 's/0*//'`
fi
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi *.mkv; do
# if there is already an .mp4 file continue
if [[ -e "${i%.*}"" - iPhone.mp4" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
HandBrakeCLI -i "$i" -o "${i%.*}"" - iPhone_processing.mp4" --preset="iPhone & iPod Touch"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
mv "${i%.*}"" - iPhone_processing.mp4" "${i%.*}"" - iPhone.mp4"
# add converted video file to itunes
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/${i%.*} - iPhone.mp4"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv"
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 if "$CATEGORY" = "movies"
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0
kcossabo wrote:
Hi, working from your script, and not getting good results.
1) I needed to add a space before and after the <<
2) Script works when run from Terminal, but if I put it in SABnzbd, I get an errror;
"321:328: execution error: The variable video is not defined. (-2753)"
any ideas?
Why did you need to add a space before and after the <<? The code works fine for me just the way it is. What version of OS X are you running? And what version of iTunes are you running?
I just cut and pasted your newest version and ran it.... worked perfect. Not sure how I screwed up the first one.
Now I am going to 'play' to see if I can change the Hand Break profile to AppleTV, and uncomment the Episode and Season additions. Any reason why they are commented out?
kcossabo wrote:
I just cut and pasted your newest version and ran it.... worked perfect. Not sure how I screwed up the first one.
Now I am going to 'play' to see if I can change the Hand Break profile to AppleTV, and uncomment the Episode and Season additions. Any reason why they are commented out?
Thanks again for the sharing.
Well I didn't have a perfect way to strip the season and episode numbers out. Sometimes SabNZBD will rename the files fine, sometimes it wont. I didn't want to base it of the NZB title because sometimes the title will be "Show Name - 1x01 - Blah" but there are two files (which is why I loop over all files). So I needed a way to strip out information from file names. And not all TV shows get converted correctly.
If you get it working please share. I would like to see. Personally I only watch the show and then delete it so I didn't care about stripping the season and episode information out.
#!/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 "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:]]+).* - (.*)$"
# perform regex
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=$((episode+0))
episode=`echo $episode | sed 's/0*//'`
fi
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi *.mkv; do
# if there is already an .mp4 file continue
if [[ -e "${i%.*}"" - AppleTV.mp4" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
/Users/kevincossaboon/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV_processing.mp4" --preset="AppleTV"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
mv "${i%.*}"" - AppleTV_processing.mp4" "${i%.*}"" - AppleTV.mp4"
# add converted video file to itunes
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/${i%.*} - AppleTV.mp4"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv"
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 if "$CATEGORY" = "movies"
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0
Last edited by kcossabo on January 24th, 2010, 7:20 pm, edited 1 time in total.
Strange. I cannot figure out why you would be getting that error. Try taking the episode and season in the AppleScript out. Does the HandBrakeCLI part finish at least? I mean does the .mp4 file get created?
kcossabo wrote:
crash and burn on changing for apple tv. Back to getting
272:279: execution error: The variable video is not defined. (-2753)
the code edited was the below. I am going back to your iPhone version, and just changing the HandBreakCLI profile and see what happens.
#!/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 "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:]]+).* - (.*)$"
# perform regex
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=$((episode+0))
episode=`echo $episode | sed 's/0*//'`
fi
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi *.mkv; do
# if there is already an .mp4 file continue
if [[ -e "${i%.*}"" - AppleTV.mp4" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
/Users/kevincossaboon/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV_processing.mp4" --preset="AppleTV"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
mv "${i%.*}"" - AppleTV_processing.mp4" "${i%.*}"" - AppleTV.mp4"
# add converted video file to itunes
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/${i%.*} - AppleTV.mp4"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv"
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 if "$CATEGORY" = "movies"
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0
I think I got it, A ROSE BY ANY OTHER NAME DOES NOT ADD TO iTunes.....
the file must have m4v extension, not mp4 for AppleTV, changed the extension and I can drag and drop them to iTunes. I changed the script and running it now.
will let you know if it solves the issue.
Last edited by kcossabo on January 25th, 2010, 8:52 am, edited 1 time in total.
#!/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 "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:]]+).* - (.*)$"
# perform regex
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=$((episode+0))
episode=`echo $episode | sed 's/0*//'`
fi
# navigate to directory of video file
cd "$DIR"
# iterate through all avi files
for i in *.avi *.mkv; do
# if there is already an .m4v file continue
if [[ -e "${i%.*}"" - AppleTV.m4v" ]]; then
echo "Skipping" $i
continue
fi
# convert avi file to mp4 file using HandBrake
/Users/user/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV_processing.m4v" --preset="AppleTV"
# if HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
mv "${i%.*}"" - AppleTV_processing.m4v" "${i%.*}"" - AppleTV.m4v"
# add converted video file to itunes
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$DIR/${i%.*} - AppleTV.m4v"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
if "$CATEGORY" = "tv"
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 if "$CATEGORY" = "movies"
set name of video to "$NAME"
end if
end tell
APPLESCRIPT
done
exit 0