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

Get Total Duration/Length of Selected Audio Files

Hi there,


How can I create an AppleScript that adds the total length/duration selected audio files.


I know how I can get the duration of a single file, doing this:


set a to choose file
do shell script "afinfo "&quoted form of (posix path of a)&"|grep duration"


But I wanted to have the total length, if possible in minutes, of all selected audio files.

Posted on Jul 9, 2013 9:24 AM

Reply
Question marked as Best reply

Posted on Jul 10, 2013 11:03 AM

There are numerous approaches, but since you've already started with afinfo, I'll keep with that.

The first step is to extract the actual duration from the afinfo output:


set duration to (word -2 of (do shell script "/usr/bin/afinfo " & quoted form of (POSIX path of a) & "|grep duration")) as number


now you can just wrap this in a loop, adding up the numbers as you go. This will end up with a total number of seconds, and a little math gets you the minutes:seconds format:


-- initialize our counter:

set total_duration to 0

-- note you can now select multiple files

set file_list to choose file with multiple selections allowed

-- and loop through them

repeat with each_file in file_list

try

set this_duration to (word -2 of (do shell script "/usr/bin/afinfo " & quoted form of (POSIX path of each_file) & "|grep duration")) as number

set total_duration to total_duration + this_duration

end try

end repeat


-- now convert your seconds to minutes:


set num_minutes to total_duration div 60

set num_seconds to total_duration mod 60 as integer


display dialog "Total duration is " & num_minutes & ":" & (text -2 through -1 of ("0" & num_seconds))

11 replies
Question marked as Best reply

Jul 10, 2013 11:03 AM in response to miguelmarques

There are numerous approaches, but since you've already started with afinfo, I'll keep with that.

The first step is to extract the actual duration from the afinfo output:


set duration to (word -2 of (do shell script "/usr/bin/afinfo " & quoted form of (POSIX path of a) & "|grep duration")) as number


now you can just wrap this in a loop, adding up the numbers as you go. This will end up with a total number of seconds, and a little math gets you the minutes:seconds format:


-- initialize our counter:

set total_duration to 0

-- note you can now select multiple files

set file_list to choose file with multiple selections allowed

-- and loop through them

repeat with each_file in file_list

try

set this_duration to (word -2 of (do shell script "/usr/bin/afinfo " & quoted form of (POSIX path of each_file) & "|grep duration")) as number

set total_duration to total_duration + this_duration

end try

end repeat


-- now convert your seconds to minutes:


set num_minutes to total_duration div 60

set num_seconds to total_duration mod 60 as integer


display dialog "Total duration is " & num_minutes & ":" & (text -2 through -1 of ("0" & num_seconds))

Aug 18, 2014 7:08 AM in response to miguelmarques

Hi,


miguelmarques wrote:


Same here, this stopped working in Mavericks and every file or files I try the output is now always "0:00". Any ideas what's wrong with it? It used to work just fine before...

This is because the script try to convert a string as number, it only work when the decimal separator is the dot in the system preferences.


"262.51" as number doesn't work on my computer --> error "Can’t make \"262.51\" into type number." number -1700 from "262.51" to number

"262,51" as number work on my computer --> 262.51


Try this script:

set n to 0

set file_list to choose file with multiple selections allowed

repeat with each_file in file_list

try

set n to do shell script "/usr/bin/afinfo " & (quoted form of POSIX path of each_file) & "| awk '/duration:/{print $3+" & n & "}'"

end try

end repeat

set total_duration to run script n -- convert the string in the variable to AppleScript number

set num_minutes to total_duration div 60

set num_seconds to total_duration mod 60 as integer

display dialog "Total duration is " & num_minutes & ":" & (text -2 through -1 of ("0" & num_seconds))

Aug 20, 2014 7:47 AM in response to miguelmarques

Because, somehow, between my AppleScript editor, where it worked, to this site, the quotes around the audio file got transformed into stylized quotes. The other factor is that you get this really long numeric string in return. You can control that string formatting in the following example that works on 10.9.4.


set myAudioFile to POSIX path of ((path to home folder) as text) & "audio.mp4"

set capture to 0 as number

set audCmd to "/usr/bin/mdls -name kMDItemDurationSeconds "

set audSubCmd1 to " | egrep -o --color=never \"(\\d+\\.\\d{3})\""

set audSubCmd2 to " | xargs -n 1 -I {} printf \"%3.3f\" {}"


set capture to do shell scriptaudCmd & myAudioFile & audSubCmd1 & audSubCmd2

display dialogcapture

Get Total Duration/Length of Selected Audio Files

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