Page 1 of 1

[Linux] Apple TV 2 Post-Processing Script

Posted: September 9th, 2011, 7:40 pm
by abray1978
Inspired by many sites and posts, I've put together a basic Linux Bash Script that will convert Video files after SABNZBD downloads. This script will run HandBrakeCLI and convert all videos downloaded to a format compatible with the Apple TV 2.

I included some code to copy the converted video over to my NAS, so you can use this as well or simply comment out that code.

I am no scripting expert, so I'm sure there are improvements that can be made. Feel free to post any improvements you can think of!

Enjoy!

Code: Select all

#!/bin/bash

#Config Variables
HandBrakeCLI="/usr/bin/HandBrakeCLI"
Destination="/data/Videos/Movies"
NASDestination="/NAS/Videos/Movies"
Extension="m4v"
Preset="AppleTV 2"
DeleteSource="0" #0 = No, 1 = Yes

#Let's get the variables from sabNZBD
DIR="$1"
NZB_FILE="$2"
NAME="$3"
NZB_ID="$4"
CATEGORY="$5"
GROUP="$6"
STATUS="$7"

COUNT=0

function convert
{
        file="$1"
        newfile="$NAME".$Extension

        echo "CONVERT: Source File: $file"
        echo "CONVERT: Destination File: $newfile"      

        # Let's check that the Destination Dir is there:

        if [ -d "$Destination" ] 
        then
                #Convert
                echo "Starting Conversion..."
                $HandBrakeCLI --input "$file" --output "$Destination/$newfile" --preset="$Present"
                
                if [ $? -eq "1" ]
                then
                        echo "ERROR: Conversion Failed...  Terminating"
                        exit 1
                else
                        echo "CONVERT: SUCCESS!"
                        let COUNT=COUNT+1
                fi

                #Copy to NAS
                if [ -d "$NASDestination" ]
                then
                        cp "$Destination/$newfile" "$NASDestination"/ 
                else
                        echo "ERROR: NAS Destination doesn't exit...  Terminating"
                        exit 1
                fi
        else
                #Error
                echo "ERROR: Destination :Directory doesn't exist...  Terminating"
                exit 1
        fi
}

#Let's look through the directory and see if there are any video files
echo "Reviewing Directory: $DIR"
for file in "$DIR"/*
do
        echo "Examining file: $file"

        if [ -d "$file" ]
        then
                echo "Sub-directory found, examining..."
                #Relaucn script in Sub-Directory
                "$0" "$file" "$2" "$3" "$4" "$5" "$6" "$7"
        else
                case "${file##*.}" in
                        avi | mkv | mp4 | wmv | mpg | mpeg)
                                convert "$file"
                                ;;
                esac
        fi
done

if [ $DeleteSource -eq "1" ]
then
        #Delete the Source
        rm -rf "$DIR"
fi

if [ $COUNT -gt 0 ]
then
        echo "Success!!"
else
        echo "ERROR: No Videos files found to convert"
fi


Re: [Linux] Apple TV 2 Post-Processing Script

Posted: September 10th, 2011, 1:33 am
by JohnSCS
Great script for ATV2's not running XBMC, or if you download 1080p content or 720p larger than 8gb.