Using Applescript in Automator app to run ffmpeg

applescript

ffmpeg runs exactly like I want it to. I'm sure the following is not super clean but it does work with one exception: it simply adds the .mp4 to the original filename so that the new filename appears to have two extensions.

I've spent days searching for similar problems on multiple forums with no real help. I've tried copying and pasting suggestions with different errors. I would really like to drag a video file onto this applet and have it run ffmpeg the way I've specified in the shell portion with the resulting output file being created in the location of the input file but with a different extension.

I've tried the "filesString" way and the \"f\" + "{$f%.*}" and for whatever reason that doesn't seem to do anything but open terminal with the text of the shell. Other options result in a message stating that there's "NO SUITABLE OUTPUT" which I have finally figured out means, "whatever you're doing is great, but I'm going to give you this error until you take the spaces out of your file name/folders."

So:

  1. To drop a file and have it convert to file with the standards specified. (check)

  2. Have that output file go back to the folder where the input file lives. (check)

  3. But with only the new extension. (fail)

  4. A bonus would be to edit the script such that I could drop a folder of files onto the applet and have them convert 1 at a time. I believe I can drop a folder at this time but then it opens multiple instances of Terminal and processes simultaneously. I'd like to have the files process 1 at a time.


on run {input, parameters}
    repeat with file_ in input
        tell application "System Events"
            set inputFilename to name of (get properties of file_)
        end tell
        set inputFilePath to quoted form of (POSIX path of file_)
        set outputFilePath to quoted form of (POSIX path of file_)
        tell application "Terminal"
            activate
            do script "/usr/local/Cellar/ffmpeg/3.0.2/bin/ffmpeg -i " & inputFilePath & " -aspect 4:3 -vf yadif=0:-1:0,scale=720:486 -acodec libfdk_aac -cutoff 20000 -vcodec libx264 -preset medium -pix_fmt yuv420p " & outputFilePath & ".mp4"
        end tell
    end repeat
    return input
end run

Best Answer

This takes care of number 3, the double file extension issue.

I've modified the code below from your original code in the following ways:

Added the remove_extension(this_name) subroutine from the bottom of the Essential Sub-routines page.

Changed:

set outputFilePath to quoted form of (POSIX path of file_)

To:

set outputFilePath to quoted form of (my remove_extension(POSIX path of file_) & ".mp4")

Removed & ".mp4" from the end of the do script ... line.

I then tested it on an actual .mkv file and while the process started, nonetheless I did stop it shortly there after, as it was just a test to see the original filename.mkv was writing to filename.mp4, and it was.

on remove_extension(this_name)
    if this_name contains "." then
        set this_name to (the reverse of every character of this_name) as string
        set x to the offset of "." in this_name
        set this_name to (text (x + 1) thru -1 of this_name)
        set this_name to (the reverse of every character of this_name) as string
    end if
    return this_name
end remove_extension

on run {input, parameters}
    repeat with file_ in input
        tell application "System Events"
            set inputFilename to name of (get properties of file_)
        end tell
        set inputFilePath to quoted form of (POSIX path of file_)
        set outputFilePath to quoted form of (my remove_extension(POSIX path of file_) & ".mp4")
        tell application "Terminal"
            activate
            do script "/usr/local/Cellar/ffmpeg/3.0.2/bin/ffmpeg -i " & inputFilePath & " -aspect 4:3 -vf yadif=0:-1:0,scale=720:486 -acodec libfdk_aac -cutoff 20000 -vcodec libx264 -preset medium -pix_fmt yuv420p " & outputFilePath
        end tell
    end repeat
    return input
end run

If you want to use the AppleScript's text item delimiters method, wrapped in a subroutine, then replace the existing on remove_extension(this_name) subroutine with the following one, while leaving the others changes I made to your code as I did.

on remove_extension(this_name)
    try
        set oldDelims to AppleScript's text item delimiters
        set AppleScript's text item delimiters to {"."}
        if number of text items of this_name > 1 then
            set this_name to (text items 1 thru -2 of this_name as text)
        end if
        set AppleScript's text item delimiters to oldDelims
    on error
        set AppleScript's text item delimiters to oldDelims
    end try
    return this_name
end remove_extension

This is essentially doing the same as the original on remove_extension(this_name) subroutine I first used, it's just a different method using what was mentioned in user14492's answer, only presented in what I'd consider to be a more proper form.