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

Come up with a useful post-processing script? Share it here!
imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

I updated to use each file's file name for season and episode info in iTunes

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
randyharris
Full Member
Full Member
Posts: 146
Joined: January 21st, 2010, 5:36 pm

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

Post by randyharris »

kcossabo wrote: ;D That was it, all in the extension!!!!
Hi Kevin,

This is really close to what I was looking for, nice!

Question, might Atomic Parsley be a better way to tag the files?

Do you happen to know of a quick and dirty way to have the script set out an email to a predefined email address notifying that the job is complete, with the name of the file converted?

Thanks!
kcossabo
Newbie
Newbie
Posts: 13
Joined: November 2nd, 2009, 6:03 pm

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

Post by kcossabo »

WRT Atomic Parsley - I added it to my version when I was getting errors with the video variable, it is actually longer than using the applescript, as it seems to rewrite the file? Not much longer, but did not want to call another program that I needed to chase the path to.

When I saw imthenachoman's posting using iTune to add the Atom's I thought it was real cool.

WRT the email.... shouldn't be that hard. I am re-working the applescript to check the variable so it does not error out on an issue, and having the email confirmation is kool. I could be traveling, log into NewzBin, select files, and know that they are on appleTV when I get home.
imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

What does WRT stand for?

randyharris: Sending an e-mail is as easy as calling the mail command. I'm actually working on changing some of script to make it a tad more customizable. Might add e-mail to it.

kcossabo: check what variable in the Applescript?
kcossabo
Newbie
Newbie
Posts: 13
Joined: November 2nd, 2009, 6:03 pm

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

Post by kcossabo »

sorry about WRT, it stands for With Respect To....

I am trying the following to avoid the error I was getting with setting video and iTunes not accepting it;

Code: Select all

tell application "iTunes"
	set initunes to false 
	try 
	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
	set initunes to true
	end try
	
	 
	
end tell
APPLESCRIPT
I am not a strong programer, but I think the 'try' will avoid creating an error, then by setting the 'initunes' it can trigger an event (like email) if there was not issues with iTunes accepting it, and send a different one if it failed. It could also clean up (delete) the file if iTunes Consolidates files for you.



I like having a variable for the path of HandBrakeCLI, and I think a variable for the preset would be cool. That way you could just make a copy for iPod and one for AppleTv....

what are your thoughts?
Last edited by kcossabo on January 27th, 2010, 8:38 pm, edited 1 time in total.
imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

But what possible error might it generate? I haven't seen any errors yet...

And I agree on the variables for path and what not. That is what I am working on once I figure out how to enable mail in CLI.
kcossabo wrote: sorry about WRT, it stands for With Respect To....

I am trying the following to avoid the error I was getting with setting video and iTunes not accepting it;

Code: Select all

tell application "iTunes"
	set initunes to false 
	try 
	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
	set initunes to true
	end try
	
	 
	
end tell
APPLESCRIPT
I am not a strong programer, but I think the 'try' will avoid creating an error, then by setting the 'initunes' it can trigger an event (like email) if there was not issues with iTunes accepting it, and send a different one if it failed. It could also clean up (delete) the file if iTunes Consolidates files for you.



I like having a variable for the path of HandBrakeCLI, and I think a variable for the preset would be cool. That way you could just make a copy for iPod and one for AppleTv....

what are your thoughts?

imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

I am going to redo this script with these changes:

config section to change:
  • type of files to convert (avi, mkv, etc...)
  • prefix or postfix for converted file
  • destination for converted file (same directory as original or some other directory)
  • output format (same as HandBrake settings)
  • format SabNZBD is used to rename files, will be used to extract information from files
  • delete file after import into itunes
I might even re-write this in Python just to learn Python. Not sure.
kcossabo
Newbie
Newbie
Posts: 13
Joined: November 2nd, 2009, 6:03 pm

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

Post by kcossabo »

:)

Wow.

Let me know if I can help.

This is a very kool effort. I hope to port this to an 'Automator' action to so that you can click on a file, and start the encoding and iTunes addition.

You work is GREATLY appreciated.
randyharris
Full Member
Full Member
Posts: 146
Joined: January 21st, 2010, 5:36 pm

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

Post by randyharris »

kcossabo wrote: :)

Wow.

Let me know if I can help.

This is a very kool effort. I hope to port this to an 'Automator' action to so that you can click on a file, and start the encoding and iTunes addition.

You work is GREATLY appreciated.
Script is better, that way you can subscribe to RSS feeds and the entire process is hands off.
kcossabo
Newbie
Newbie
Posts: 13
Joined: November 2nd, 2009, 6:03 pm

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

Post by kcossabo »

Agree 100%

I just have years of odd video's around that right click-reencode-add-to-iTunes would be good for.
randyharris
Full Member
Full Member
Posts: 146
Joined: January 21st, 2010, 5:36 pm

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

Post by randyharris »

imthenachoman wrote: I am going to redo this script with these changes:

config section to change:
  • type of files to convert (avi, mkv, etc...)
  • prefix or postfix for converted file
  • destination for converted file (same directory as original or some other directory)
  • output format (same as HandBrake settings)
  • format SabNZBD is used to rename files, will be used to extract information from files
  • delete file after import into itunes
I might even re-write this in Python just to learn Python. Not sure.
Sounds great, I don't know if Kevin was modifying your script for his use or if they are separate, but something small we talked about offline was something you could integrate if interested (as a variable), and that was a working directory. The idea being that the safest way to proceed is to leave the original MKV/AVI where it is, copy it to a working directory, transcode it, after the transcode is complete, move it to the destination directory, and then delete the working copy. Sure this could be done by just moving the original to a working directory and then leaving or deleting when done - I see benefits both ways.

email notification was mentioned by me earlier, to me it's a great benefit to have a notification that the job took place and completed, I suppose SMS could be an alternate notification, maybe either or.

Very much appreciate what you're doing, I think more people might end up using it especially if it has variables that can be set without too much trouble.

:)
imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

http://forums.sabnzbd.org/index.php?topic=3562.msg25576

I could make it make a copy of the original, but since the original is never modified till the very end (if you want) I don't see why? Or am I mis-understanding what you're saying? If so I apologize.
imthenachoman
Jr. Member
Jr. Member
Posts: 59
Joined: March 2nd, 2009, 10:58 am
Contact:

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

Post by imthenachoman »

Oops....
User avatar
inpheaux
Administrator
Administrator
Posts: 563
Joined: January 16th, 2008, 9:14 pm

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

Post by inpheaux »

Locked