'Applescript to batch convert videos with ffmpeg

I'm trying to add padding to a large amount of videos in their folders. I created an app with AppleScriptEditor so I can drag and drop files and they're automatically converted. I found a script on the web, I edited it with the ffmpeg command I need, but it won't work because it wants to overwrite the source file.

on open argv
    set paths to ""
    repeat with f in argv
        set paths to paths & quoted form of POSIX path of f & " "
    end repeat
    tell application "Terminal"
        do script "for f in " & paths & "; do ffmpeg -i \"$f\"  -vf pad=\"9/8*iw:ih:(ow-iw)/2:0:color=black\" \"$f\"; done"
        activate
    end tell
end open

Note that I want to keep the filename, filetype and put the new file next to the old one but just add an underscore at the end of the new file, before the extension; e.g.: file.ext. > file_.ext



Solution 1:[1]

The actual ffmpeg command you are running via do script is:

ffmpeg -i "$f" -vf pad="9/8*iw:ih:(ow-iw)/2:0:color=black" "$f";

That does indeed tell ffmpeg the input_url and output_url are the same: $f. From your comment, try specifying a different output_url in a way similar to this change.

do script "for f in " & paths & "; do outfile=\"${f%.*}_.${f##*.}\"; ffmpeg -i \"$f\" -vf pad=\"9/8*iw:ih:(ow-iw)/2:0:color=black\" \"$outfile\"; done"

The specific parameter and substitution syntax may vary between shells. In zsh (and probably bash), the above creates a new outfile variable to then use in the ffmpeg command. It is set equal to the current loop's f value, but broken into three parts:

  • everything before the final . (so, the original path and filename)
  • new underscore _ and the .
  • everything after the final . (the original extension)

If you'd like to pop into Terminal itself and experiment with these substitutions, consider with a no-op example like this:

f="/example/path/filename with spaces.and.periods.mp4"; echo "   $f\n" "becomes\n" "  ${f%.*}_.${f##*.}";

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Joel Reid