Convert video with service in Mac OS
I have some .avi
videos in my photo archive on the external HDD. I decided to import them to iCLoud Photo Library in order to keep them safe. But Photos.app doesn’t accept .avi
, so I needed to convert them into .mov
.
Of course, FFmpeg is a default choice for such task. But I have quite a lot of .avi
files, and I don’t want to call FFmpeg for every each one of them from terminal, I want to select several, right-click on them and just select something like “FFmpeg this”.
And that can be done with Automator.
Here’s the plan:
- Create a new Service that gets files from Finder;
- This Service should iterate through selected files and run a FFmpeg script for each;
- Play a sound and show a notification when job is done.
And that’s how you do that. Launch an Automator and create a new Service:
Change receives
to “files and folders” in “Finder.app”:
Add action Run AppleScript
from Library:
The contents of the script would be as follows:
on run {input, parameters}
# logging script
set logScript to load script "/path/to/your/scripts/write2log.scpt"
# just to count files for notification
set fcount to 0
try # encoding
repeat with i in input
# get path to file
set fname to POSIX path of i
# using axe cut the extension and quotes from end
set o to text 1 thru -5 of fname
# run ffmpeg for a file
do shell script "/path/to/ffmpeg -i \"" & fname & "\" -crf 18 -y \"" & o & ".mov\""
# increment the counter
set fcount to fcount + 1
end repeat
on error ex
# log exception
write2log("/path/to/some.log", ex) of logScript
# and exit
error "failed"
end try
try # play sound
set doneSound to quoted form of "/path/to/some/sound.mp3"
do shell script "afplay " & doneSound
on error ex
write2log("/path/to/some.log", ex) of logScript
end try
# if you prefer simple system sound
#say "Done"
#beep
# for using in notification
return fcount
end run
Logging script I described in another article. And afplay
is a console tool for playing audio.
The only thing left is to display a notification. To include a number of converted files (fcount
from return statement of AppleScript) into it, you need to add a transitional step for creating a variable (action Set Value of Variable
) and passing it to final action Display Notification
.
Here’s the complete workflow:
Now you save it with the name “FFmpeg this”, and it will be available under Services menu in Finder:
If video doesn't play in your browser, you can download it here.
In this example the sound is played in the same moment the notifications is shown, which is not true, bacause these events go one after another as it clearly follows from the workflow. I made it like that just for the sake of understanding.
So, in this article I showed:
- How to create a new Service in Mac OS, that can be called by right-clicking on a file in Finder;
- How to iterate through a set of files operating with their paths. I got some hints from here;
- How to pass a variable between actions in a workflow. At first that was problematic, but then I discovered this example;
- How to play a sound file and show a notification when job is done.
Cool, huh.
Social networks
Zuck: Just ask
Zuck: I have over 4,000 emails, pictures, addresses, SNS
smb: What? How'd you manage that one?
Zuck: People just submitted it.
Zuck: I don't know why.
Zuck: They "trust me"
Zuck: Dumb fucks