Running FFmpeg in Automator/AppleScript

applescriptautomatorquicktimeterminalvideo

I have written an apple script previously to automate a task I do in my work many times.

With Apple updating to Catalina I will lose the use of Quicktime 7 (which is part of my Image sequencing workflow) I want to take the opportunity rewrite my script.

I'm taking it one step at a time and the first is running FFmpeg.

I have written a script:

ffmpeg -r 25 -f image2 -pattern_type glob -i '*.JPG' -codec:v prores_ks -profile:v 0 imagemagick_TL_Test_01.mov

This works in Terminal if I navigate to the folder and run it. Great.

I now want to find a way to make the action drag and droppable.

I have tried to adapt my old code to include this at the relevant section to run the ffpmeg on the dropped folder but I have encountered error after error. Ideally renaming the output file to have the grandparent folder name and save in the parent folder.

on open dd

    repeat with d in dd
        set d to d's contents
        tell application "Finder"
            set seq1 to (d's file 1 as alias)
            set dparent to d's container as alias
            set mov to "" & dparent & (dparent's name) & ".mov"
        end tell
        do shell script "d=" & d's POSIX path's quoted form & "
    /opt/local/bin/ffmpeg -r 25 -f image2 -i \"" & seq1 & "\" '*.JPG' -codec:v prores_ks -profile:v 0 \"" & dparent & ".mov\" && exit
    "
    end repeat
end open

This gives me the error :

[image2 @ 0x7ff9cd000000] Could find no file with path '***:Users:***:Desktop:imagemagick_TL_Test:01:_DAN7741.JPG' and index in the range 0-4
***:Users:***:Desktop:imagemagick_TL_Test:01:_DAN7741.JPG: No such file or directory

Any pointers would be greatly appreciated! Thank you!

Best Answer

An important consideration when programming is to come up with a plan for debugging your code. Here is how I would have coded the code for debugging.

It's difficult to debug an on open handler. I add an on run handler where I can dummie up the input to a common routine that is called by on run and on open. fyi: I corrected how the input was processed from on open.

(* 
   AskDifferent question:
     https://apple.stackexchange.com/questions/366992/running-ffmpeg-in-automator-applescript
 Use log statements.

 It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.

For testing, run in the Script Editor.
    1) Click on the Event Log tab to see the output from the log statement
    2) Click on Run

 *)
on run
    --  debug lines
    set desktopPath to (path to desktop) as string

    -- here is a log statment.
    log "desktopPath = " & desktopPath

    -- Be sure to select a file on your DESKTOP.
    set see to alias (desktopPath & "auto-show-2015.mp4")

    -- Simulate dropped items list.
    set dropped_items to {see}

    common(dropped_items)

end run


-- Gets invoked here when something is dropped on this AppleScript icon

on open dropped_items

    common(dropped_items)

end open



on common(dd)

    repeat with d in dd
        set d to d's contents
        log d
        tell application "Finder"
            --set seq1 to (d's file 1 as alias)  -- dropped items come in as an array of aliased items. 
            set seq1 to d
            set dparent to d's container as alias
            set mov to "" & dparent & (dparent's name) & ".mov"
            log "mov is " & mov
        end tell

        set toUnix to "d=" & d's POSIX path's quoted form & " /opt/local/bin/ffmpeg -r 25 -f image2 -i \"" & seq1 & "\" '*.JPG' -codec:v prores_ks -profile:v 0 \"" & dparent & ".mov\" && exit
    "
        log "toUnix is " & toUnix
        set fromUnix to do shell script toUnix
        log "fromUnix is " & fromUnix
    end repeat
end common