Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.


Question: Batch renaming files using metadata pulled from ffmpeg

I'm an absolute beginner trying to build a droplet (out of an existing droplet) that will let me drag a video file onto it, and rename that file with the frame rate before the extension. ie: TEST.mp4 drag to droplet TEST2997.mp4

I have absolutely no idea what I'm doing and most of this code is from another ffmpeg droplet I found. Any help or direction at all would be much appreciated.

Heres the nonfunctional code I have so far:

global fileName

global outputName

to split(someText, delimiter)

set AppleScript'stext item delimiters to delimiter

set someText to someText'stext items

set AppleScript'stext item delimiters to {""}

return someText

end split

on opennames

set ffmpeg to "ffmpeg"

set afterEncoding to "open .; afplay \"/System/Library/Sounds/Glass.aiff\"; "

set afterEncoding to afterEncoding & "echo \"Success!
Saved to same folder as original.\""

repeat with itemnum from 1 to count of names

-- inputPath: absolute path to file

set inputPath to POSIX path of (itemitemnum of names)

-- fileParent: absolute path to file's parent folder

tell application "Finder"

set fileParent to inputPath

set AppleScript'stext item delimiters to "/"

set fileParent to text items 1 thru -2 of fileParent as text

set fileParent to fileParent & "/"

end tell

-- fileName: file's name without extension

set extension to ""

set fileName to ""

tell application "Finder"

set inputName to name of file (itemitemnum of names)

set fileName to inputName

set AppleScript'stext item delimiters to "."

if number of text items of fileName > 1 then

set extension to text item (count of text items of fileName) of fileName as text

set fileName to text items 1 thru -2 of fileName as text

end if

end tell

set str to ffmpeg & " -i \"" & inputPath & "\" 2>&1 | sed -n \"s/.*, \\(.*\\) fp.*/\\1/p\""

set fpsInput to do shell scriptstr

set fpsInput to fpsInput as number

set outputName to fileName & fpsInput & "." & extension

set outputPath to fileParent & outputName

if checkExist(outputPath) then

tell application "Terminal"

do script "cd \"" & fileParent & "\"; " & ffmpeg & " -i '" & inputName & "' -c:v copy -c:a copy '" & outputName & "'; " & afterEncoding

end tell

end if

end repeat


end open


Posted on


May 26, 2018 5:47 AM in response to colonelmatto In response to colonelmatto

So you found a couple piles of Gilbert's Erector Set AppleScript code, and now want to build something from it. 😉

In the following code, I set the inputFilename to a movie file I had laying around named Dornbracht.mp4. I get its framerate from the Spotlight kMDItemTotalBitRate field of the movie file. Otherwise, this would be a dropped file.

Then, I wrote an AppleScript/Objective-C function (handler in AS terms) that receives the full path to the movie file, and the framerate as arguments. The function simply inserts the framerate between the full pathname of the file, and its extension, returning that new filename. The active return statement in the below code is simply cosmetic because it hides my username with a tilde (~) path convention. Ordinarily, one would use only the first return statement that sends back the full path of new filename.

Output from the build_outname function:

User uploaded file

Your drop handler and loop (commented below) would surround everything below the property statement in my example. You would get your framerate as you do presently. This is not an entire solution to your goal, but does eliminate considerable AppleScript eyestrain.

use framework "Foundation"

use AppleScriptversion "2.4" -- Yosemite or later

use scripting additions

property NSString : a reference to current application's NSString

-- on open dropped_items

-- repeat with anItem in dropped_items

-- set inputFilename to anItem

set inputFilename to POSIX path of ((path to movies folder as text) & "Dornbracht.mp4" as alias) as text

-- mdls output looks like: kMDItemTotalBitRate = 2634 Default awk separator is space, so grab third field

set framerate to (do shell script "mdls -name kMDItemTotalBitRate " & inputFilename & " | awk '{print $3}'")

display dialog my build_outname(inputFilename, framerate)


-- end repeat

-- end open

on build_outname(afile, arate)

-- insert framerate between afile's full path name and its extension

set nameStr to NSString'sstringWithString:POSIX path of afile

set basename to (nameStr'sstringByDeletingPathExtension())'s stringByAppendingString:arate as text

-- return (basename's stringByAppendingPathExtension:(nameStr's pathExtension)) as text

return ((basename'sstringByAppendingPathExtension:nameStr'spathExtension)'s stringByAbbreviatingWithTildeInPath) as text

end build_outname

May 26, 2018 5:47 AM

Reply Helpful
User profile for user: colonelmatto

Question: Batch renaming files using metadata pulled from ffmpeg