Skip to content

Instantly share code, notes, and snippets.

@oparrish
Created August 25, 2011 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oparrish/1170588 to your computer and use it in GitHub Desktop.
Save oparrish/1170588 to your computer and use it in GitHub Desktop.
Bash script for transcoding videos to AppleTV format to be used by an Alfred extension
#!/bin/bash
#transcode.sh
#By Don Southard aka @binaryghost
#
#[UPDATE 6/01/2011 08:01:15PM]
#Idea and initialcode for Alfred progress bar by Andreas Heiberg
#Completly rewritten code by Don Southard
#
#[UPDATE 6/02/2011 08:58:09PM]
#Support for queueing, added information about queue status and ETA'S and the move the old file to trash feature
#by Andreas Heiberg
#[UPDATE 8/25/2011 08:56:00AM]
#Support for spaces in path names
#by Owen Parrish
#Save the field seprator to restore later. This will avoid problems later if path names have a space in them.
SAVEIFS=$IFS
IFS=$'\n'
#for every selected file transcode with handbrakeCLI and move the old file to the trash
for f in "$@"
do
INPUT="$f"
#Modified by Andreas Heiberg
VIDEO=${INPUT%.*}
LOG="/Users/owen/Library/Logs/HandbrakeCLI.log"
LOG2="/Users/owen/Library/Logs/HandbrakeCLI_Converted.log"
#Checks how many videos are waiting to be transcoded
TOTALCOUNT=$#
#Makes a varible that shows the current videos count. (This will later be used to say this is video 1 of 4 fx.)
VIDEOCOUNT=$(($VIDEOCOUNT + 1))
#Change this to path of your Alfred settings folder
SETTINGSFOLDER=$(cd `dirname $0` && pwd)
#Starts the conversion in the background with the settings seen below
#Output for log code by Adreas Heiberg
($SETTINGSFOLDER/HandBrakeCLI -i "$f" -o "$VIDEO".m4v --preset="AppleTV 2" 1> "$LOG") &
#Read original discription and save it, so it can be restored at the end
DESC=$(defaults read $SETTINGSFOLDER/info subtitle)
#Run this loop until HandBrakeCLI is no longer running
while [ "$(ps ax | grep -v grep | grep HandBrakeCLI)" ]
do
#convert current Handbrake log file in LF format
tr '\r' '\n' < $LOG > $LOG2
#Extracts percentage of last line
PROCENTAGE=$(tail -1 $LOG2 | awk 'BEGIN { FS = " " } ; { print $6}')
#Extracts ETA times from the las line
ETA=$(tail -1 $LOG2 | awk 'BEGIN { FS = " " } ; { print $14}')
#Write the current videos number in the queue and the the total number of queued videos, completion in percentage and ETA times from the last line to the description
defaults write $SETTINGSFOLDER/info subtitle "Conversion started: Video $VIDEOCOUNT of $TOTALCOUNT is $PROCENTAGE% completed - ETA ${ETA%)*}"
sleep 5
done
#Return description to original setting
defaults write $SETTINGSFOLDER/info subtitle "$DESC"
#Moves the old file to the trash
mv "$f" ~/.Trash/
#Deletes the log files completely
rm $LOG
rm $LOG2
#Update the trash through apple script
osascript <<-eof1
tell application "Finder"
update trash
end tell
eof1
done
#restore field seperator
IFS=$SAVEIFS
#play the Glass sound when done
afplay /System/Library/Sounds/Glass.aiff
#Setting up the growl notifications
echo "Your video has been transcoded to AppleTV format."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment