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

Get durations of audio files

I'm new to AppleScript so I'm not sure where to begin with code.

I have a bunch of audio files in a folder that I need to get the durations of. I'd prefer to not have to import all the files into iTunes.
Ideally I'd like the filename and its respective duration to be output to a default spreadsheet (I have Excel) listing the filename in one column and the duration in the other. If not, just having the filename and the duration on the same line in a text file would be workable too.
The duration would need to have the format of X:XX.

iMac, Mac OS X (10.5.8)

Posted on Apr 15, 2010 2:06 PM

Reply
Question marked as Best reply

Posted on Apr 15, 2010 3:38 PM

I wrote this code example some time ago. Maby this could help

set aFile to choose file

tell application "Finder" to set filename to name of aFile

set AppleScript's text item delimiters to " = "

set duration to text item 2 of (do shell script "mdls -name kMDItemDurationSeconds " & quoted form of (POSIX path of aFile))

set AppleScript's text item delimiters to "."

set total_seconds to text item 1 of duration

set AppleScript's text item delimiters to ""

set showsec to (total_seconds as integer) mod 60

set showmin to (total_seconds as integer) div 60

display dialog filename & return & return & "Duration:" & return & showmin & " min " & showsec & " sec"



It uses the spotlight-index to read track-information via mdls (lists the metadata attributes for the specified file), so this would only work on volumes spotlight has indexed.
5 replies
Question marked as Best reply

Apr 15, 2010 3:38 PM in response to chsinclair

I wrote this code example some time ago. Maby this could help

set aFile to choose file

tell application "Finder" to set filename to name of aFile

set AppleScript's text item delimiters to " = "

set duration to text item 2 of (do shell script "mdls -name kMDItemDurationSeconds " & quoted form of (POSIX path of aFile))

set AppleScript's text item delimiters to "."

set total_seconds to text item 1 of duration

set AppleScript's text item delimiters to ""

set showsec to (total_seconds as integer) mod 60

set showmin to (total_seconds as integer) div 60

display dialog filename & return & return & "Duration:" & return & showmin & " min " & showsec & " sec"



It uses the spotlight-index to read track-information via mdls (lists the metadata attributes for the specified file), so this would only work on volumes spotlight has indexed.

Apr 15, 2010 8:04 PM in response to hubionmac

Okay, what you had will display the results, but I can't copy and paste the results from the display box.

Here is what I've tried to do when adding a repeat loop:

tell application "Finder"
set textFile to choose file with prompt "Choose a text file."
set theFolder to choose folder
repeat with theFile in (get files of theFolder)
set AppleScript's text item delimiters to " = "
set duration to (get text item 2 of (do shell script "mdls -name kMDItemDurationSeconds " & quoted form of (POSIX path of theFile)))
set AppleScript's text item delimiters to "."
set total_seconds to text item 1 of duration
set AppleScript's text item delimiters to ""
set showsec to (total_seconds as integer) mod 60
set showmin to (total_seconds as integer) div 60
tell application "TextEdit"
write theFile & " " & showmin & ":" & showsec & return to textFile
end tell
end repeat
end tell

but I keep getting the following error:
error "Can’t make quoted form of POSIX path of item 1 of {«class docf» \"01 Track 01.m4a\" of «class cfol» \"TEST\" of «class cfol» \"Desktop\" of «class cfol» \"charliesinclair\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\", «class docf» \"02 The Wonderful.mp3\" of «class cfol» \"TEST\" of «class cfol» \"Desktop\" of «class cfol» \"charliesinclair\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\", «class docf» \"02 Track 02.m4a\" of «class cfol» \"TEST\" of «class cfol» \"Desktop\" of «class cfol» \"charliesinclair\" of «class cfol» \"Users\" of «class sdsk» of application \"Finder\"} into type Unicode text." number -1700 from quoted form of POSIX path of item 1 of {«class docf» "01 Track 01.m4a" of «class cfol» "TEST" of «class cfol» "Desktop" of «class cfol» "charliesinclair" of «class cfol» "Users" of «class sdsk», «class docf» "02 The Wonderful.mp3" of «class cfol» "TEST" of «class cfol» "Desktop" of «class cfol» "charliesinclair" of «class cfol» "Users" of «class sdsk», «class docf» "02 Track 02.m4a" of «class cfol» "TEST" of «class cfol» "Desktop" of «class cfol» "charliesinclair" of «class cfol» "Users" of «class sdsk»} to Unicode text


any ideas?

Apr 15, 2010 10:22 PM in response to chsinclair

POSIX path doesn't like Finder file specifiers, so you will need to convert to something else, such as text or an alias. Also note that in addition to the Spotlight attribute being missing if the file is not indexed, there will not be an attribute if an application has not added the metadata to the Spotlight database. Usually items on the startup disk are indexed, but things like downloads and external disks may not have had the audio metadata imported. The following script checks for a missing attribute and writes the results to a text file:

<pre style="
font-family: Monaco, 'Courier New', Courier, monospace;
font-size: 10px;
font-weight: normal;
margin: 0px;
padding: 5px;
border: 1px solid #000000;
width: 720px; height: 340px;
color: #000000;
background-color: #DAFFB6;
overflow: auto;"
title="this text can be pasted into the Script Editor">
set theFolder to (choose folder with prompt "Select a folder containing the audio files:")
set outputFile to (choose file name with prompt "Where do you want to save the results?" default name "Audio File Durations.txt" default location (path to desktop))
set output to {} -- this will be the resulting data
tell application "Finder" to set theFiles to (files of theFolder whose kind contains "Audio File") as alias list

repeat with aFile in theFiles -- build data list
tell application "Finder" to set theName to name of aFile
set aFile to POSIX path of aFile
set theTime to (do shell script "mdls -name kMDItemDurationSeconds -raw " & quoted form of aFile)
if theTime is "(null)" then -- no attribute
set theTime to "no time"
else -- format to mm:ss
set theTime to round theTime rounding as taught in school
set timeString to 10000 + 100 * (theTime mod hours div minutes) -- minutes
set timeString to (timeString + (theTime mod minutes)) as text -- seconds
tell timeString to set theTime to (text 2 thru 3) & ":" & (text 4 thru 5)
end if
-- set the end of output to aFile & tab & theTime & return
set the end of output to theName & tab & theTime & return
end repeat

try -- write the data to a file
set theOpenFile to (open for access outputFile with write permission)
set eof of theOpenFile to 0 -- overwrite
write (output as text) to theOpenFile starting at eof
close access theOpenFile
on error --- make sure file is closed
try
close access theOpenFile
end try
end try
</pre>

Get durations of audio files

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.