Get The Duration of a Video File?

Is there any way to get the duration of a video file and save it as a variable in KM?

I am appending their duration to video file names — easily done with, e.g.: Name Mangler — as part of a larger macro involving renaming each movie file's parent folder.

I checked the drop-down list of file attributes in the KM Action "Get File Attribute", but I don't find it there, nor is it listed on that Action's Help page among the attributes accessible.


This AppleScript returns the duration of a selected video in seconds.

set macPath to (choose file) as text
tell application "System Events"
	set ts to time scale of movie file macPath
	set dur to duration of movie file macPath
	set movieTime to dur / ts
end tell

Thank you!

I am yet green behind the horns ... I figured out how to execute this and pass the result to additional Actions, but I don't yet know how to make it work on multiple files selected (in, in my case, Path Finder, using Chris' Get Items AppleScript).

Below is what I have so far for appending the duration in mmm'ss format to a movie file's name. Getting the duration and appending it works — though I'd be happy for suggestions regarding what I've done there.

How do I send the Paths to the AppleScript that gets the movie duration? I tried changing "choose file" to "Path" in the first line of the AppleScript, but it did not work.


  • What is the syntax for the math function MOD? I found it in the KM 6 Help, but I didn't manage to make it work.
  • What units are time scale and duration in the AppleScript. It puzzles me that dur / ts yields seconds.

_Attempt to get Movie Duration and append it to file name.kmmacros (6.5 KB)

What happened? was reading your source then poof gone?? :flushed:

Sorry Bill — screwed up. All fixed, I hope.

I'm learning and the source code really helps...

Thank you Kirby :wink:

I am just a beginner. Not sure I have anything to teach in this field :blush: . Fortunately, there are others here who do.

1 Like

Hey Kirby,

Get the Keyboard Maestro variable from your AppleScript.

tell application "Keyboard Maestro Engine"
  set myPath to value of variable "Path"
end tell

Keep in mind that compiled script files run faster than text scripts. For some jobs that little bit of extra speed makes a lot of difference.


1 Like

It appears this script no longer works. I have successfully used it previously but I now get following error message:

script error: Expected end of line but found application constant or consideration. (-2741)

Can anyone possibly troubleshoot this or suggest an alternative method of getting the duration of a video file?


To answer my own question I'm now using exiftool to save the output to a variable where I can use regex to extract the data I want thus making the AppleScript I enquired about redundant.

I found exiftool better than mediainfo for the job because mediainfo doesn't seem to include the seconds if the video is over an hour whereas exiftool does.

Users must install exiftool directly or via MacPorts or Homebrew – it does not come stock on macOS.