Declaration of VAR

and some other stuff

Convert video with service in Mac OS

2016-08-25 10:54:33 +0200

2016-08-25 10:54:33 +0200 | Comments

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 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:

  1. Create a new Service that gets files from Finder;
  2. This Service should iterate through selected files and run a FFmpeg script for each;
  3. 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 “”:

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"
    # 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:

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:

  1. How to create a new Service in Mac OS, that can be called by right-clicking on a file in Finder;
  2. How to iterate through a set of files operating with their paths. I got some hints from here;
  3. How to pass a variable between actions in a workflow. At first that was problematic, but then I discovered this example;
  4. How to play a sound file and show a notification when job is done.

Cool, huh.