I am working on a script (BASH) that will run after a video is downloaded. It calls applescript to add the video to iTunes.
The script works when called from terminal, but, when it is called automatically from the download application SABnzbd, it fails.
the Script;
#!/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
StartDir="/Users/user1/HandBreak-SABnzbd"
HandBreakDir="$StartDir/HandBreakCLI"
AtomicParsleyDir="$StartDir/AtomicParsley"
iTunesAdd="/Music/iTunes/iTunes Media/Automatically Add to iTunes"
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 *.*; 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
$HandBreakDir/HandBrakeCLI -i "$i" -o "${i%.*}"" - AppleTV.mp4" --preset="AppleTV"
# if HandBrake did not exit gracefully, continue with next iteration (This only occurs on a MAJOR error)
if [[ $? -ne 0 ]]; then
continue
fi
# Check to see if the file was created if so, add it to iTunes
if [[ -e "${i%.*}"" - AppleTV.mp4" ]]; then
# add converted video file to itunes
osascript << EOT
tell application "iTunes"
using terms from 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" 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 if "$CATEGORY" = "movies" then
set name of video to "$NAME"
end if
end using terms from
end tell
EOT
echo "${i%.*}"" - AppleTV.mp4 ---> Created and added to iTunes"
else
echo "No Video Created ------------------------"
fi
done
exit 0
When run from terminal;
works fine. when run at the end of the download from the applications I get;
I am working off other’s code and could use some help with the above issue and;
how / why does this add the video to iTunes? by referencing the file in iTunes, and changing the ATOMs it adds it?
In the Configuration tab for Folders, there is a Post-Processing Script Folder - set it to the place of the script
Then for each BIN download, there are pul downs for Category - Set to “TV” and Script - Set to the script.
If you are asking how SABnzbd actually calls the script, I have no idea. That is above my head.
I can now confirm that the issue is the file extension.
iTunes will not accept the AppleTV encoded Video as a mp4 it needs to be a m4v
This is true for this script, dragging and dropping and the
~\Music\iTunes\iTunes-Media\Automatically Add to iTunes
if present.
The error seems to be caused from the variable being unable to keep the assignment of the POSIX path. My assumption is that it is added, then iTunes rejects it, and erases it, then the next reference sees it as ‘unassigned’
#!/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 - Replace /Users/user/HandBreak-SABnzbd/HandBreakCLI with you rpath to HandBreakCLI
/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
I should add that I was using you original script with the handbrake code and mp4.
In terminal i had no problems getting a .mp4 file into iTunes.
Also drag and drop.
Also just converted a .avi file to AppleTv .m4v file with QTpro. Changed the extension to .mp4 and dragged and dropped with no issue?
Used the same script to convert a .m4v to mp4 and add to iTunes with no issues.
Did I read it wrong above that you had no issue when using your orig mp4 script in terminal??.
The only thing I am not doing is running it from SaBnzbd.
You read correctly, I changed the script though the trouble shooting, removing things like AtomicParsly, and going back to a known working state.
So I worked on the issue of the script being called getting the error from the AppleScript that Variable Video was undefined.
I added the ‘use terms from…’ and many other things to see why the script could be run from terminal and not executed from SABnzbd.
I then went back to the original source that was for iPod/iPhone, not AppleTV. The author had a newer version, that I cut and pasted. It worked for iPhone encoding.
Through many steps, I reduce down to only changing the encoding from iPhone to AppleTV (this had the mp4 extension). It Failed from SABnzbn. I did NOT re-try the terminal execution of the script, as I thought HandBreakCLI was broke.
After seeing no chatter on issues with the AppleTV profile, I from the terminal executed
I then tried to drag this file to iTunes via Finder and iTunes would no accept it. The library area turned blue, and the PLUS icon appeared, but dropping the file did not result in a copy.
I then was going to encode the same ‘somevideo.avi’ using the HandBreak GUI. There I noticed that the file extension was m4v not mp4 as the default extension. I went back to the file created with terminal execution of HandBreakCLI, and renamed it. This newly renamed file was drag’d and dropped onto iTunes with success.
I changed all .mp4 to .m4v in the script, and it is working fine from SABnzbd.
Same results. Drag video file over as a .mp4 Library area turns blue, and PLUS sign appears, drop. Nothing is added. Change extension to .m4v, and repeat, file is added.
Third Mac
Mac BookPro 2.8Ghz Intel Core 2 Duo
OSX 10.6.2
iTunes 9.0.2
Same results. Required the .m4v extension
All test were done on same file. I check the file with Atomicparsley and only Atom set is @too and is Handbreak