Skip to content

Instantly share code, notes, and snippets.

@boffbowsh
Created May 7, 2011 13:13
Show Gist options
  • Select an option

  • Save boffbowsh/960486 to your computer and use it in GitHub Desktop.

Select an option

Save boffbowsh/960486 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo
echo
echo
echo "APPLETV SCRIPT DETAILS:"
# You need to put this script where SAB is set for the script location
# SAB | Config | Folders | Post-Processing Scripts Folder
# You can modify this location, but make sure the script goes where you are telling
# SAB where the scripts are located.
# For me it works best in to set the Default User Script to AppleTV; SAB | Config | Switches |
# Some locations below are hardcoded in this script
# All of the settings in this script which may vary from machine to machine are preceeded with:
# "##### The line below contains a user defined location setting"
# Search for the 5 pound signs and update the lines below them all to suit your system.
# There are 3 applications you need to install for this script to properly function
#
# 1) HandbrakeCLI (encodes the video for AppleTV)
# Download from: http://handbrake.fr/downloads.php
# Free
#
# 2) Mencoder (used for joining 2 AVI's)
# Download from: http://www.mplayerhq.hu/design7/dload.html#binaries
# Mencoder is one of the files in the MPlayer download
# Free
#
# 3) iDentify (Tags Movies with info and cover art)
# iDentify is fully capable to tag TV Shows also, but I find that since I'm a
# Watch and Delete person for TV, I'm fine with basic Show, Season, and Episide tags
# which I can do without aid of a third party application. But for Movies, I want it all.
# Download from: http://www.macupdate.com/info.php/id/33814/identify-2
# Free for a version with limited use, pay the $10 for full version, it is worth it
#
# Output Parameters from SABnzbd+
# SABnzbd pushes these parameters to the post processing script
DIR=$1
NZB_FILE=$2
NAME=$3
NZB_ID=$4
CATEGORY=$5
GROUP=$6
STATUS=$7
# Set NAME to all lower case for more reliable testing
NAMElc=$(echo $NAME|tr '[A-Z]' '[a-z]')
CATEGORYlc=$(echo $CATEGORY|tr '[A-Z]' '[a-z]')
# You can set 'fake' output parameters from SABnzbd+ here for testing on any file in any folder
# DIR="/Users/randyharris/Desktop/The.Daily.Show.2010.04.20.John.OHara.iTouch-MW"
# NZB_FILE="The.Daily.Show.2010.04.20.John.OHara.iTouch-MW.nzb"
# NAME="The.Daily.Show.2010.04.20.John.OHara.iTouch-MW"
# NZB_ID=""
# CATEGORY="tv"
# GROUP="alt.binaries.teevee"
# STATUS="0"
# When I woke up this morning my girlfriend asked me 'Did you sleep good?'
#
# I said 'No, I made a few mistakes.'
#
# - Steven Wright
#======================================================================================
#======================================================================================
#
# This section of the script is for Movies.
#
# Note that I don't even attempt to mess with Subtitles, largely because HandBrake won't
# burn subtitles from MKV source material - stinks I know. So make other arrangements
# for subtitled movies.
#
# In SABnzbd SORTING be certain to Enable Movie Sorting.
# You can use any sort string that you prefer, but please use this string for Multi-part label:
# " CD%1" (don't include the quotes)
#
# The script will check and join 2 AVI's so that you have a single video file,
# it will check and convert VIDEO_TS file structure to ISO so that HandBrakeCLI can convert it
# The file is then converted for AppleTV.
#
# Then the file is opened in iDentify. I have iDentify setup so that I preview the tag information
# that it found for the movie before I process it (hard code it to the movie.)
# My iDentify preferences that are important
# GENERAL, YES to Automatically Add to iTunes After Tagging
# Note you have to donate $10 to enable this feature, well worth it to me.
# FILE RENAMING, NO to Rename movies after Tagging (this can be fine to use if you set it up how you like.)
# Setting the variables for Destination Folder
##### The line below contains a user defined location setting
dest_folder="/Volumes/Storage/iTunes/Movies/"
if [[ $CATEGORYlc == movies ]]
then
echo " - Processing as a Movie"
echo
# Won't print error in for loop if there are no video files in the folder
shopt -s nullglob
# Join 2 AVIs
# Some XviD releases are packaged into two files that will fit onto a CDROM. Since we are
# watching movies on an AppleTV it's more convenient to have the movie in 1 file not 2.
# This section will look for two AVI files, CD1 and CD2 and combine them into a single file
# then create a sub-folder and move the two original AVIs into that folder for safe keeping
cd "$DIR"
# The SAB sort string should name multiple AVI's with capital CD1 and CD2
# This section will look for two AVI's with CD1 and CD2 and join them.
for i in *CD2.avi; do
echo " - An AVI file with 'CD2' was found:"
##### The line below contains a user defined location setting
/Applications/mencoder -ovc copy -oac copy *CD1.avi *CD2.avi -o "$NAME.avi" > /dev/null 2>&1
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Combine completed"
mkdir "Unjoined AVIs"
mv *CD1.avi "Unjoined AVIs/."
mv *CD2.avi "Unjoined AVIs/."
echo " - Old AVI's moved into 'Unjoined AVIs'"
echo
done
# Just in case CD1 and CD2 are actually cd1 and cd2
for i in *cd2.avi; do
echo
echo " - An AVI file with 'cd2' was found:"
##### The line below contains a user defined location setting
/Applications/mencoder -ovc copy -oac copy *cd1.avi *cd2.avi -o "$NAME.avi" > /dev/null 2>&1
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Combine completed"
mkdir "Unjoined AVIs"
mv *cd1.avi "Unjoined AVIs/."
mv *cd2.avi "Unjoined AVIs/."
echo " - Old AVI's moved into 'Unjoined AVIs'"
echo
done
# While HandbrakeCLI can encode DVD rips in the VIDEO_TS structure, it is easier for me
# to convert to an ISO image, has to do with logic flow of the script if no source is found.
# Check for DVD Rip into VIDEO_TS format and converting to ISO
cd "$DIR"
if [[ -e $(find . -name VIDEO_TS.IFO) ]]; then
ifo=$(find . -name VIDEO_TS.IFO)
echo "folder/file: $found"
videots=$(echo $ifo|sed 's/[vV][iI][dD][eE][oO][_][tT][sS][.][iI][fF][oO].*//g')
videotsroot=$(echo $folder|sed 's/[vV][iI][dD][eE][oO][_][tT][sS].*//g')
echo
echo "VIDEO_TS Found, converting to an ISO"
hdiutil makehybrid -iso -o "$DIR/$NAME.iso" "$videotsroot"
echo " - Conversion to ISO complete"
echo
fi
# Convert the source video file to AppleTV M4V
#
# First this section checks to be sure that you don't already have an AppleTV file with the
# same name in the Destination Folder. If not, it will convert all video files to the
# Destination Folder. Ideally there will only be 1 file converted, but sometimes there are
# other video files that could be present. To help reduce the amount of unwanted video files
# it is recommended that you go into SABnzbd | Config | Switches | and set "Ignore Samples"
# to Do not download.
#
# After the encoding is complete the movie will be opened into iDentify for tagging/cover art.
# Lastly the folder containing the source video and possibly original CD1/CD2 AVI's will be moved
# into the Trash bin so that they are out of your way but retreivable until you empty your Trash.
# The encoding section into two sections because 1) Video files and 2) VIDEO_TS structure
# This is the first of two sections - it is looking for movies in single files, MKV, ISO, etc.
cd "$DIR"
for i in *.mkv *.avi *.m4v *.mp4 *.wmv *.iso *.img; do
# Setting the variables for Destination File
dest_file="${i%.*}"".m4v"
# If there is already an M4V file stop
if [[ -e $dest_folder$dest_file ]]; then
echo " - Destination file already exists, skipping $i"
continue
fi
echo "Transcoding"
echo
echo " - Destination folder:"
echo " $dest_folder"
echo
echo " - Destination file:"
echo " $dest_file"
# Convert the source video file to an AppleTV M4V file using HandBrake
##### The line below contains a user defined location setting
if ffprobe -show_streams "$i" | grep h264 ; then
ffmpeg -i "$i" -vcodec copy -acodec libfaac -ab 160k -ac 2 -ar 48000 "$dest_folder$dest_file" > /dev/null 2>&1
else
/Applications/HandBrakeCLI -i "$i" -o "$dest_folder$dest_file" --longest --preset="AppleTV" > /dev/null 2>&1
fi
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Transcode completed"
echo
# Use iDentify to Tag and import into iTunes
echo "Opening in iDentify to Tag and inject into iTunes as a Movie"
##### The line below contains a user defined location setting
open -a /Applications/iDentify.app "$dest_folder$dest_file"
# Move the source folder to the Trash Bin
echo
echo "Moving source files to the Trash Bin"
cd ~/
mv "$DIR" ~/.Trash
# Post Processing as a Movie complete
echo
echo "Movie is AppleTV ready, check/apply Tags in iDentify."
done
#======================================================================================
#======================================================================================
#
# I download Formula1 races from a "My Searches" search at www.NZBs.org
# Since F1 races don't have Season and Episode information I had to make
# a custom section that would recognize the F1 races and import them gracefully
# with proper tag information.
#
# If you don't need this section you can you can either just leave it and it won't
# hurt anything or you can delete from just below the 'done' after
# the movie section above through the 'done' at the bottom of this section
elif [[ $NAMElc == formula1* ]]
then
echo " - Processing as a Formula1 race."
echo
# Won't print error in for loop if there are no video files in the folder
shopt -s nullglob
# Navigate to folder with the video file
cd "$DIR"
# Regex expression parse Tag information from the filename
# This parses information from the Job Name for tagging and file naming
# regex="^([[:alnum:]]+)\.([0-9]+)\.([[:alpha:]]+)\.(.*)$"
regex="^([[:alnum:]]+)\.([0-9]+)\.(.*)$"
if [[ $CATEGORYlc -eq "tv" && $NAME =~ $regex ]]; then
show_name=${BASH_REMATCH[1]}
season=${BASH_REMATCH[2]}
tag=${BASH_REMATCH[3]}
fi
# Dress up the Job Name to look how I want it in iTunes
# Change periods to spaces
tag2=$(echo $tag|sed 's/\./ /g')
# Change 'Grand Prix' to 'GP'
tag3=$(echo $tag2|sed 's/Grand Prix /GP/g')
# Strip everything after "PDTV" or "HDTV" or "720P" or "WS"
tag4=$(echo $tag3|sed 's/[pP][dD][tT][vV].*//g')
tag5=$(echo $tag4|sed 's/[hH][dD][tT][vV].*//g')
tag6=$(echo $tag5|sed 's/[7][2][0][pP].*//g')
tag7=$(echo $tag6|sed 's/[wW][sS].*//g')
# Strip any trailing spaces
tag8=$(echo $tag7|sed 's/[ \t]*$//')
# Make sure the first letter of every word is Capitalized
tag9=$( echo "${tag8}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')
# Add "Round" information, will have to update this table every season.
# Race schedules can be found on Wikipedia
if [[ $NAME =~ Bahrain ]]; then round="R01" && episode="01"
elif [[ $NAME =~ Australian ]]; then round="R02" && episode="02"
elif [[ $NAME =~ Malaysian ]]; then round="R03" && episode="03"
elif [[ $NAME =~ Chinese ]]; then round="R04" && episode="04"
elif [[ $NAME =~ Spanish ]]; then round="R05" && episode="05"
elif [[ $NAME =~ Monaco ]]; then round="R06" && episode="06"
elif [[ $NAME =~ Turkish ]]; then round="R07" && episode="07"
elif [[ $NAME =~ Canadian ]]; then round="R08" && episode="08"
elif [[ $NAME =~ European ]]; then round="R09" && episode="09"
elif [[ $NAME =~ British ]]; then round="R10" && episode="10"
elif [[ $NAME =~ German ]]; then round="R11" && episode="11"
elif [[ $NAME =~ Hungarian ]]; then round="R12" && episode="12"
elif [[ $NAME =~ Belgian ]]; then round="R13" && episode="13"
elif [[ $NAME =~ Italian ]]; then round="R14" && episode="14"
elif [[ $NAME =~ Singapore ]]; then round="R15" && episode="15"
elif [[ $NAME =~ Japanese ]]; then round="R16" && episode="16"
elif [[ $NAME =~ Korean ]]; then round="R17" && episode="17"
elif [[ $NAME =~ Brazilian ]]; then round="R18" && episode="18"
elif [[ $NAME =~ Abu ]]; then round="R19" && episode="19"
else round="" && episode=""
fi
# Setting the variables for Destination File and Folder
##### The line below contains a user defined location setting
episode_name=$round" "$tag9
dest_folder="/Users/randyharris/Movies/TV Shows/Racing/"
dest_file=$show_name" "$season" - "$episode_name".m4v"
dest_false=" - .m4v"
# The Regex line only works on the expected format, so just in case
# the file name isn't as expected; Formula1.YYYY.Venue.TagInfo
# then we'll use the Job Source as the file name instead
if [[ $dest_file = $dest_false ]]; then
show_name="Formula1"
season=$(date '+%Y')
episode="01"
episode_name="$NAME"
dest_file="$NAME.m4v"
fi
# Convert the source video file to AppleTV M4V
#
# First this section checks to be sure that you don't already have an AppleTV file with the
# same name in the Destination Folder. If not, it will convert all video files to the
# Destination Folder. Ideally there will only be 1 file converted, but sometimes there are
# other video files that could be present. To help reduce the amount of unwanted video files
# it is recommended that you go into SABnzbd | Config | Switches | and set "Ignore Samples"
# to Do not download.
#
# After the encoding is complete the Formula1 race will be injected into iTunes with basic tag
# information, Show Name = Formula1, Episode name = Venue, Season is taken from the Job Name,
# Episode is hard coded to 01.
#
# Lastly the folder containing the source video will be moved into the Trash bin so that they
# are out of your way but retreivable until you empty your Trash.
for i in *.mkv *.avi *.m4v *.mp4 *.wmv *.iso *.img; do
echo "Transcoding the race for AppleTV."
# If there is already an M4V file stop
if [[ -e $dest_folder$dest_file ]]; then
echo " - Destination file already exists, skipping" $i
continue
fi
# Convert the source video file to an AppleTV M4V file using HandBrake
##### The line below contains a user defined location setting
/Applications/HandBrakeCLI -i "$i" -o "$dest_folder$dest_file" --longest --preset="AppleTV" > /dev/null 2>&1
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Transcode completed"
echo
# Add converted video file into iTunes
echo
echo "Tagging and injecting into iTunes"
echo " - TV Show or Movie: $CATEGORY"
echo " - Show Name: $show_name"
echo " - Season: $season"
echo " - Episode: $episode"
echo " - Episode Name: $episode_name"
echo " - File: $dest_file"
echo " - Folder: $dest_folder"
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$dest_folder" & "$dest_file"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
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"
end tell
APPLESCRIPT
echo " - Tag and inject completed"
echo
# Move the source folder to the Trash Bin
echo
echo "Putting the original source files into the Trash Bin"
cd ~/
mv "$DIR" ~/.Trash
# Post Processing for the F1 race complete
echo
echo "The Formula1 race is on your AppleTV ready for viewing."
done
#======================================================================================
#======================================================================================
# I download MotoGP races from a "My Searches" search at www.NZBs.org
# Since MotoGP races don't have Season and Episode information I had to make
# a custom section that would recognize the MotoGP races and import them gracefully
# with proper tag information.
#
# If you don't need this section you can you can either just leave it and it won't
# hurt anything or you can delete from just below the 'done' after
# the movie section above through the 'done' at the bottom of this section
elif [[ $NAMElc == motogp* ]]
then
echo " - Processing as a MotoGP race."
echo
# Won't print error in for loop if there are no video files in the folder
shopt -s nullglob
# Navigate to folder with the video file
cd "$DIR"
# Regex expression parse Tag information from the filename
# This parses information from the Job Name for tagging and file naming
# regex="^([[:alnum:]]+)\.([0-9]+)\.([[:alpha:]]+)\.(.*)$"
regex="^([[:alnum:]]+)\.([0-9]+)\.(.*)$"
if [[ $CATEGORYlc -eq "tv" && $NAME =~ $regex ]]; then
show_name=${BASH_REMATCH[1]}
season=${BASH_REMATCH[2]}
tag=${BASH_REMATCH[3]}
fi
# Dress up the Job Name to look how I want it in iTunes
# Change periods to spaces
tag2=$(echo $tag|sed 's/\./ /g')
# Change 'Grand Prix' to 'GP'
tag3=$(echo $tag2|sed 's/Grand Prix /GP/g')
# Strip everything after "PDTV" or "HDTV" or "720P" or "WS"
tag4=$(echo $tag3|sed 's/[pP][dD][tT][vV].*//g')
tag5=$(echo $tag4|sed 's/[hH][dD][tT][vV].*//g')
tag6=$(echo $tag5|sed 's/[7][2][0][pP].*//g')
tag7=$(echo $tag6|sed 's/[wW][sS].*//g')
# Strip any trailing spaces
tag8=$(echo $tag7|sed 's/[ \t]*$//')
# Make sure the first letter of every word is Capitalized
tag9=$( echo "${tag8}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')
# Add "Round" information, will have to update this table every season.
# Race schedules can be found on Wikipedia
if [[ $NAME =~ Qatar ]]; then round="R01" && episode="01"
elif [[ $NAME =~ Spanish ]]; then round="R02" && episode="02"
elif [[ $NAME =~ French ]]; then round="R03" && episode="03"
elif [[ $NAME =~ Italian ]]; then round="R04" && episode="04"
elif [[ $NAME =~ British ]]; then round="R05" && episode="05"
elif [[ $NAME =~ Dutch ]]; then round="R06" && episode="06"
elif [[ $NAME =~ Catalan ]]; then round="R07" && episode="07"
elif [[ $NAME =~ German ]]; then round="R08" && episode="08"
elif [[ $NAME =~ States ]]; then round="R09" && episode="09"
elif [[ $NAME =~ Czech ]]; then round="R10" && episode="10"
elif [[ $NAME =~ Indianapolis ]]; then round="R11" && episode="11"
elif [[ $NAME =~ Marino ]]; then round="R12" && episode="12"
elif [[ $NAME =~ Aragon ]]; then round="R13" && episode="13"
elif [[ $NAME =~ Japanese ]]; then round="R14" && episode="14"
elif [[ $NAME =~ Malaysian ]]; then round="R15" && episode="15"
elif [[ $NAME =~ Australian ]]; then round="R16" && episode="16"
elif [[ $NAME =~ Portuguese ]]; then round="R17" && episode="17"
elif [[ $NAME =~ Valenc ]]; then round="R18" && episode="18"
else round="" && episode=""
fi
# Setting the variables for Destination File and Folder
##### The line below contains a user defined location setting
episode_name=$round" "$tag9
dest_folder="/Users/randyharris/Movies/TV Shows/Racing/"
dest_file=$show_name" "$season" - "$episode_name".m4v"
dest_false=" - .m4v"
# The Regex line only works on the expected format, so just in case
# the file name isn't as expected; MotoGP.YYYY.Venue.TagInfo
# then we'll use the Job Source as the file name instead
if [[ $dest_file = $dest_false ]]; then
show_name="MotoGP"
season=$(date '+%Y')
episode="01"
episode_name="$NAME"
dest_file="$NAME.m4v"
fi
# Convert the source video file to AppleTV M4V
#
# First this section checks to be sure that you don't already have an AppleTV file with the
# same name in the Destination Folder. If not, it will convert all video files to the
# Destination Folder. Ideally there will only be 1 file converted, but sometimes there are
# other video files that could be present. To help reduce the amount of unwanted video files
# it is recommended that you go into SABnzbd | Config | Switches | and set "Ignore Samples"
# to Do not download.
#
# After the encoding is complete the MotoGP race will be injected into iTunes with basic tag
# information, Show Name = MotoGP, Episode name = Venue, Season is taken from the Job Name,
# Episode is hard coded to 01.
#
# Lastly the folder containing the source video will be moved into the Trash bin so that they
# are out of your way but retreivable until you empty your Trash.
for i in *.mkv *.avi *.m4v *.mp4 *.wmv *.iso *.img; do
echo "Transcoding the race for AppleTV."
# If there is already an M4V file stop
if [[ -e $dest_folder$dest_file ]]; then
echo " - Destination file already exists, skipping" $i
continue
fi
# Convert the source video file to an AppleTV M4V file using HandBrake
##### The line below contains a user defined location setting
/Applications/HandBrakeCLI -i "$i" -o "$dest_folder$dest_file" --longest --preset="AppleTV" > /dev/null 2>&1
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Transcode completed"
echo
# Add converted video file into iTunes
echo
echo "Tagging and injecting into iTunes"
echo " - TV Show or Movie: $CATEGORY"
echo " - Show Name: $show_name"
echo " - Season: $season"
echo " - Episode: $episode"
echo " - Episode Name: $episode_name"
echo " - File: $dest_file"
echo " - Folder: $dest_folder"
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$dest_folder" & "$dest_file"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
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"
end tell
APPLESCRIPT
echo " - Tag and inject completed"
echo
# Move the source folder to the Trash Bin
echo
echo "Putting the original source files into the Trash Bin"
cd ~/
mv "$DIR" ~/.Trash
# Post Processing for the F1 race complete
echo
echo "The MotoGP race is on your AppleTV ready for viewing."
done
#======================================================================================
#======================================================================================
# I download World Rally Car races from a "My Searches" search at www.NZBs.org
# Since WRC races don't have Season and Episode information I had to make
# a custom section that would recognize the WRC races and import them gracefully
# with proper tag information.
#
# If you don't need this section you can you can either just leave it and it won't
# hurt anything or you can delete from just below the 'done' after
# the movie section above through the 'done' at the bottom of this section
elif [[ $NAMElc == wrc* ]]
then
echo " - Processing as a WRC race."
echo
# Won't print error in for loop if there are no video files in the folder
shopt -s nullglob
# Navigate to folder with the video file
cd "$DIR"
# Regex expression parse Tag information from the filename
# This parses information from the Job Name for tagging and file naming
# regex="^([[:alnum:]]+)\.([0-9]+)\.([[:alpha:]]+)\.(.*)$"
regex="^([[:alnum:]]+)\.([0-9]+)\.(.*)$"
if [[ $CATEGORYlc -eq "tv" && $NAME =~ $regex ]]; then
show_name=${BASH_REMATCH[1]}
season=${BASH_REMATCH[2]}
tag=${BASH_REMATCH[3]}
fi
# Dress up the Job Name to look how I want it in iTunes
# Change periods to spaces
tag2=$(echo $tag|sed 's/\./ /g')
# Change 'Grand Prix' to 'GP'
tag3=$(echo $tag2|sed 's/Grand Prix /GP/g')
# Strip everything after "PDTV" or "HDTV" or "720P" or "WS"
tag4=$(echo $tag3|sed 's/[pP][dD][tT][vV].*//g')
tag5=$(echo $tag4|sed 's/[hH][dD][tT][vV].*//g')
tag6=$(echo $tag5|sed 's/[7][2][0][pP].*//g')
tag7=$(echo $tag6|sed 's/[wW][sS].*//g')
# Strip any trailing spaces
tag8=$(echo $tag7|sed 's/[ \t]*$//')
# Make sure the first letter of every word is Capitalized
tag9=$( echo "${tag8}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')
# Add "Round" information, will have to update this table every season.
# Race schedules can be found on Wikipedia
if [[ $NAME =~ Sweden ]]; then round="R01" && episode="01"
elif [[ $NAME =~ Mexico ]]; then round="R02" && episode="02"
elif [[ $NAME =~ Jordan ]]; then round="R03" && episode="03"
elif [[ $NAME =~ Turkey ]]; then round="R04" && episode="04"
elif [[ $NAME =~ Zealand ]]; then round="R05" && episode="05"
elif [[ $NAME =~ Portug ]]; then round="R06" && episode="06"
elif [[ $NAME =~ Bulgaria ]]; then round="R07" && episode="07"
elif [[ $NAME =~ Finland ]]; then round="R08" && episode="08"
elif [[ $NAME =~ Deut ]]; then round="R09" && episode="09"
elif [[ $NAME =~ Japan ]]; then round="R10" && episode="10"
elif [[ $NAME =~ France ]]; then round="R11" && episode="11"
elif [[ $NAME =~ Espa ]]; then round="R12" && episode="12"
elif [[ $NAME =~ Wales ]]; then round="R13" && episode="13"
else round="" && episode=""
fi
# Setting the variables for Destination File and Folder
##### The line below contains a user defined location setting
episode_name=$round" "$tag9
dest_folder="/Users/randyharris/Movies/TV Shows/Racing/"
dest_file=$show_name" "$season" - "$episode_name".m4v"
dest_false=" - .m4v"
# The Regex line only works on the expected format, so just in case
# the file name isn't as expected; WRC.YYYY.Venue.TagInfo
# then we'll use the Job Source as the file name instead
if [[ $dest_file = $dest_false ]]; then
show_name="WRC"
season=$(date '+%Y')
episode="01"
episode_name="$NAME"
dest_file="$NAME.m4v"
fi
# Convert the source video file to AppleTV M4V
#
# First this section checks to be sure that you don't already have an AppleTV file with the
# same name in the Destination Folder. If not, it will convert all video files to the
# Destination Folder. Ideally there will only be 1 file converted, but sometimes there are
# other video files that could be present. To help reduce the amount of unwanted video files
# it is recommended that you go into SABnzbd | Config | Switches | and set "Ignore Samples"
# to Do not download.
#
# After the encoding is complete the WRC race will be injected into iTunes with basic tag
# information, Show Name = WRC, Episode name = Venue, Season is taken from the Job Name,
# Episode is hard coded to 01.
#
# Lastly the folder containing the source video will be moved into the Trash bin so that they
# are out of your way but retreivable until you empty your Trash.
for i in *.mkv *.avi *.m4v *.mp4 *.wmv *.iso *.img; do
echo "Transcoding the race for AppleTV."
# If there is already an M4V file stop
if [[ -e $dest_folder$dest_file ]]; then
echo " - Destination file already exists, skipping" $i
continue
fi
# Convert the source video file to an AppleTV M4V file using HandBrake
##### The line below contains a user defined location setting
/Applications/HandBrakeCLI -i "$i" -o "$dest_folder$dest_file" --longest --preset="AppleTV" > /dev/null 2>&1
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Transcode completed"
echo
# Add converted video file into iTunes
echo
echo "Tagging and injecting into iTunes"
echo " - TV Show or Movie: $CATEGORY"
echo " - Show Name: $show_name"
echo " - Season: $season"
echo " - Episode: $episode"
echo " - Episode Name: $episode_name"
echo " - File: $dest_file"
echo " - Folder: $dest_folder"
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$dest_folder" & "$dest_file"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
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"
end tell
APPLESCRIPT
echo " - Tag and inject completed"
echo
# Move the source folder to the Trash Bin
echo
echo "Putting the original source files into the Trash Bin"
cd ~/
mv "$DIR" ~/.Trash
# Post Processing for the F1 race complete
echo
echo "The WRC race is on your AppleTV ready for viewing."
done
#======================================================================================
#======================================================================================
else
# This section of the scripts is for TV Shows / Series that contain Season and Episode info
# You need to have SABnzbd SORTING set to match what this script is looking for (or change
# the regex line to suit your output.
#
# Enable TV Sorting in SAB and use this string:
# "%sn - S%0sE%0e/%sn - S%0sE%0e.%ext" (don't include the quotes)
# Which results in this example:
# /Users/username/desktop/Show Name - S01E05/Show Name - S01E05.avi
echo " - Processing as a TV Show / Series"
# Won't print error in for loop if there are no video files in the folder
shopt -s nullglob
# Regex expression parse Tag information from the filename
# This parses information from the Job Name for tagging and file naming
regex="^(.+) - [Ss]([[:digit:]]+)[Ee]([[:digit:]]+).*$"
# Navigate to folder with the video file
cd "$DIR"
# Convert the source video file to AppleTV M4V
#
# First this section checks to be sure that you don't already have an AppleTV file with the
# same name in the Destination Folder. If not, it will convert all video files to the
# Destination Folder. Ideally there will only be 1 file converted, but sometimes there are
# other video files that could be present. To help reduce the amount of unwanted video files
# it is recommended that you go into SABnzbd | Config | Switches | and set "Ignore Samples"
# to Do not download.
#
# After the encoding is complete the Formula1 race will be injected into iTunes with basic tag
# information, Show Name = Formula1, Episode name = Venue, Season is hard coded to the current year,
# Episode is hard coded to 01.
#
# Lastly the folder containing the source video will be moved into the Trash bin so that they
# are out of your way but retreivable until you empty your Trash.
for i in *.mkv *.avi *.m4v *.mp4 *.wmv *.iso *.img; do
NAME=${i%.*}
if [[ $CATEGORYlc -eq "tv" && $NAME =~ $regex ]]; then
show_name=${BASH_REMATCH[1]}
season=${BASH_REMATCH[2]}
episode=${BASH_REMATCH[3]}
fi
# Setting the variables for Destination File and Folder
dest_file=$show_name" - S"$season"E"$episode".m4v"
##### The line below contains a user defined location setting
dest_folder="/Volumes/Storage/iTunes/TV/"
dest_false=" - SE.m4v"
# Using info available if no SxxExx information
# Some shows don't have Season and Episode information, this section will check
# and if no Season and Episode information exists, then it will base tag info
# Off of the Job Name and current year, and set Episode to 01.
if [[ $dest_file = $dest_false ]]; then
echo " - Proceeding without SxxExx info"
echo
# Dress up the Job Name to look how I want it in iTunes
# Change periods to spaces
tag2=$(echo $NAME|sed 's/\./ /g')
# Strip everything after "PDTV" or "HDTV" or "720P" or "WS" or "iTouch" or "DVDrip" or "XviD"
tag3=$(echo $tag2|sed 's/[pP][dD][tT][vV].*//g')
tag4=$(echo $tag3|sed 's/[hH][dD][tT][vV].*//g')
tag5=$(echo $tag4|sed 's/[7][2][0][pP].*//g')
tag6=$(echo $tag5|sed 's/[wW][sS].*//g')
tag7=$(echo $tag6|sed 's/[iI][tT][oO][uU][cC][hH].*//g')
tag8=$(echo $tag7|sed 's/[dD][vV][dD][rR][iI][pP].*//g')
tag9=$(echo $tag8|sed 's/[xX][vV][iI][dD].*//g')
# Strip any trailing spaces
tag10=$(echo $tag9|sed 's/[ \t]*$//')
# Make sure the first letter of every word is Capitalized
tag11=$( echo "${tag10}" | awk '{for(i=1;i<=NF;i++)sub(/./,toupper(substr($i,1,1)),$i)}1')
show_name=$tag11
season=$(date '+%Y')
episode="01"
dest_file="$tag11.m4v"
fi
# If there is already an M4V file stop
if [[ -e "$dest_folder$dest_file" ]]; then
echo " - An M4V with the same name already exisits,"
echo " - skipping $i"
continue
fi
# Convert the source video file to an AppleTV M4V file using HandBrake
echo "Transcoding the TV Show"
##### The line below contains a user defined location setting
if /usr/local/bin/ffprobe -show_streams "$i" | grep h264 >/dev/null 2>&1 ; then
/usr/local/bin/ffmpeg -i "$i" -vcodec copy -acodec libfaac -ab 160k -ac 2 -ar 48000 "$dest_folder$dest_file" > /dev/null 2>&1
else
/Applications/HandBrakeCLI -i "$i" -o "$dest_folder$dest_file" --longest --preset="AppleTV" > /dev/null 2>&1
fi
# Note the " > /dev/null 2>&1" at the end of the line directs output from HandBrakeCLI away from the script log
echo " - Transcode completed"
echo
# If HandBrake did not exit gracefully, continue with next iteration
if [[ $? -ne 0 ]]; then
continue
fi
# Add converted video file into iTunes
echo
echo "Tagging and injecting into iTunes"
echo " - TV Show: $show_name"
echo " - Season: $season"
echo " - Episode: $episode"
echo " - File: $dest_file"
echo " - Folder: $dest_folder"
osascript <<APPLESCRIPT
tell application "iTunes"
set posix_path to "$dest_folder" & "$dest_file"
set mac_path to posix_path as POSIX file
set video to (add mac_path)
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"
end tell
APPLESCRIPT
echo " - Tag and inject completed"
echo
# Move the source folder to the Trash Bin
echo "Putting the original source files into the Trash Bin"
echo
cd ~/
mv "$DIR" ~/.Trash
# Post Processing for TV Show complete
echo "The TV Show is on your AppleTV ready for viewing."
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment