Page 1 of 2

My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: December 10th, 2009, 5:41 pm
by imthenachoman
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.

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 "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
Thanks!

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: December 10th, 2009, 7:01 pm
by rAf
Nice. I'll try to test it.
Thanks for sharing !

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 9:49 am
by kcossabo
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?

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 10:22 am
by imthenachoman
I updated the script a bit. Here is the latest version I have been using that works great.

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 "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

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 10:24 am
by imthenachoman
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?

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 5:24 pm
by kcossabo
let me try the new script, the first one I would get 'unexpected end of file' with out the space, now I have been getting a

321:328: execution error: The variable video is not defined. (-2753)

QQ, where is HandBreakCLI for your script to pick it up with no path?

I am running 10.6 OSX and Version: 0.5.0rc3 os Sabnzbd iTune 9.0.2

I like the addition of MKV, thank you for sharing.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 5:53 pm
by imthenachoman
I put handbrakecli in /usr/bin which is part of the path.

I'm pretty sure there can't be a space after the << becuase thats what tells bash to send all the output to osascript.

Can you try my script in Terminal and paste the exact output.

I don't have 0.5.0 rc3. Let me try installing it and seeing if that breaks anything...
kcossabo wrote: let me try the new script, the first one I would get 'unexpected end of file' with out the space, now I have been getting a

321:328: execution error: The variable video is not defined. (-2753)

QQ, where is HandBreakCLI for your script to pick it up with no path?

I am running 10.6 OSX and Version: 0.5.0rc3 os Sabnzbd iTune 9.0.2

I like the addition of MKV, thank you for sharing.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 5:58 pm
by kcossabo
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.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 6:07 pm
by imthenachoman
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.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 7:11 pm
by kcossabo
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.

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 "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

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 7:22 pm
by imthenachoman
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.

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 "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

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 8:07 pm
by kcossabo
it is pointing to a problem with HandBreakCLI and the AppleTV Profile.

I took your script, and it work flawless
I uncommented the Sets for Season Episode.... worked flawless
I changed the profile to AppleTV,
/Users/kevincossaboon/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - iPhone_processing.mp4" --preset="iPhone & iPod Touch"
and I get
x264 [info]: SSIM Mean Y:0.9884926
x264 [info]: PSNR Mean Y:47.202 U:52.151 V:51.791 Avg:48.262 Global:47.601 kb/s:797.52
[19:53:03] libhb: work result = 0

Rip done!
HandBrake has exited.
271:278: execution error: The variable video is not defined. (-2753)
I will now try your suggestion of re-commenting the extras. If I even try to drag and drop the mp4 onto iTunes using Finder it will not add.

Thank you again for the guidance.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 24th, 2010, 8:12 pm
by imthenachoman
Ahh. Have you tried the HandBrake IRC channel on Freenode?
kcossabo wrote: it is pointing to a problem with HandBreakCLI and the AppleTV Profile.

I took your script, and it work flawless
I uncommented the Sets for Season Episode.... worked flawless
I changed the profile to AppleTV,
/Users/kevincossaboon/HandBreak-SABnzbd/HandBreakCLI/HandBrakeCLI -i "$i" -o "${i%.*}"" - iPhone_processing.mp4" --preset="iPhone & iPod Touch"
and I get
x264 [info]: SSIM Mean Y:0.9884926
x264 [info]: PSNR Mean Y:47.202 U:52.151 V:51.791 Avg:48.262 Global:47.601 kb/s:797.52
[19:53:03] libhb: work result = 0

Rip done!
HandBrake has exited.
271:278: execution error: The variable video is not defined. (-2753)
I will now try your suggestion of re-commenting the extras. If I even try to drag and drop the mp4 onto iTunes using Finder it will not add.

Thank you again for the guidance.


Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 25th, 2010, 8:49 am
by kcossabo
???

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.

Re: My Script : AVI to iPhone to iTunes : OS X + Handbrake :

Posted: January 25th, 2010, 9:18 am
by kcossabo
;D That was it, all in the extension!!!!

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 "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
in iTunes with Tags.......  ;D