Last active
January 1, 2017 23:00
-
-
Save richtr/8bc84b9346f9eb2ac945 to your computer and use it in GitHub Desktop.
Synology NAS (or any other AFP-based shared folder) to Handbrake auto-conversion of avi/mkv files to mp4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- AppleScript that mounts an AFP service (e.g. a Synology NAS Diskstation) then | |
-- searches for any .avi or .mkv files in the requested volume and converts all | |
-- the files it finds to .mp4 via HandbrakeCLI (which must be installed on your | |
-- machine at /Applications/HandbrakeCLI) | |
-- IMPORTANT: You may need to make sure your NAS does not sleep until ~4 hours of | |
-- inactivity has passed so it doesn't interrupt the mp4 conversion process of | |
-- this script. | |
-- Based loosely on the script published at http://cybernetnews.com/batch-convert-videos-handbrake-applescript/ | |
-- For more detailed installation instructions consult the URL above. | |
-- ***************** | |
-- AFP service host name | |
set afpHostName to "DiskStation._afpovertcp._tcp.local" | |
-- AFP service user name | |
set afpUser to "admin" | |
-- AFP service user password | |
set afpPassword to "password" | |
-- AFP root folder name to convert videos from (scans in sub-directories too) | |
set afpVolume to "downloads" | |
-- ***************** | |
-- EDITING BELOW THIS LINE IS LIKELY TO BREAK THINGS! | |
set sourceUrl to "afp://" & afpUser & ":" & afpPassword & "@" & afpHostName & "/" & afpVolume | |
repeat | |
repeat 1 times | |
try | |
-- Attempt to detect the requested AFP Host via the `dig` command | |
-- this throws us out of the try/catch block if unsuccessful (i.e. it times out) | |
do shell script "dig -p 5353 @224.0.0.251 " & afpHostName & " any" | |
log "Requested AFP Host found." | |
-- Mount the requested AFP Volume (attempts to connect 4 times at 15 seconds intervals) | |
repeat 4 times | |
if (list disks) contains afpVolume then exit repeat | |
mount volume sourceUrl | |
delay 15 | |
end repeat | |
if (list disks) does not contain afpVolume then exit repeat -- # simulated `continue` | |
log "Requested AFP Volume mounted." | |
-- Now run over the contents of the afpVolume | |
with timeout of (720 * 60) seconds | |
tell application "Finder" | |
--Get all AVI and MKV files that have not already been processed | |
set allFiles to every file of entire contents of (afpVolume as alias) whose ((name extension is "avi" or name extension is "mkv") and label index is not 6) -- 6 == 'green' == already processed files | |
log "Found files:" | |
log allFiles | |
log "Entering conversion loop" | |
--Repeat for all files in above folder | |
repeat with i from 1 to number of items in allFiles | |
set currentFile to (item i of allFiles) | |
log "Processing:" | |
log currentFile | |
try | |
--Assemble original and new file paths | |
set origFilepath to quoted form of POSIX path of (currentFile as alias) | |
set newFilepath to (characters 1 thru -5 of origFilepath as string) & "mp4'" | |
--Set to gray label to indicate processing | |
set label index of currentFile to 7 | |
-- Remove any existing mp4 file with the same name assuming the conversion process did not | |
-- complete last time around (otherwise this avi/mkv file would have been deleted already) | |
if my FileExists(newFilepath) then | |
log "Deleting previously unfinished conversion file:" | |
log newFilepath | |
my DeleteFile(newFilepath) | |
end if | |
log "Converting from:" | |
log origFilepath | |
log "Converting to:" | |
log newFilepath | |
--Start the conversion | |
log "Starting conversion. Please be patient..." | |
set shellCommand to "nice /Applications/HandBrakeCLI -i " & origFilepath & " -o " & newFilepath & " --preset 'Universal'" | |
do shell script shellCommand | |
log "...file conversion finished." | |
--Set the label to green in case file deletion fails | |
set label index of currentFile to 6 | |
--Delete original file | |
log "Removing old file:" | |
log origFilepath | |
my DeleteFile(origFilepath) | |
log "Converted file is at:" | |
log newFilepath | |
on error errmsg | |
log "An error occurred:" | |
log errmsg | |
--Set the label to red to indicate failure | |
set label index of currentFile to 2 | |
end try | |
end repeat | |
log "Leaving conversion loop" | |
end tell | |
end timeout | |
end try | |
end repeat | |
log "Video conversion script sleeping for 60 seconds." | |
-- Wait 60 seconds before attempting to run this script again | |
delay 60 | |
log "Video conversion script waking up." | |
end repeat | |
-- ***************** | |
-- Check if a file exists | |
-- from: https://stackoverflow.com/questions/3469389/applescript-testing-for-file-existence | |
on FileExists(theFile) -- (String) as Boolean | |
tell application "System Events" | |
if exists file theFile then | |
return true | |
else | |
return false | |
end if | |
end tell | |
end FileExists | |
-- Delete a file | |
on DeleteFile(filePath) | |
set shellCommand to "rm -f " & filePath | |
do shell script shellCommand | |
end DeleteFile |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment