miguelmarques

Q: 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

Close

Q: Get Total Duration/Length of Selected Audio Files

  • All replies
  • Helpful answers

  • by Camelot,Solvedanswer

    Camelot Camelot Jul 10, 2013 11:03 AM in response to miguelmarques
    Level 8 (47,243 points)
    Mac OS X
    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))

  • by miguelmarques,

    miguelmarques miguelmarques Jul 11, 2013 4:34 AM in response to Camelot
    Level 1 (0 points)
    Jul 11, 2013 4:34 AM in response to Camelot

    Thanks, that worked perfectly!

  • by miguelmarques,

    miguelmarques miguelmarques Oct 29, 2013 6:48 AM in response to miguelmarques
    Level 1 (0 points)
    Oct 29, 2013 6:48 AM in response to miguelmarques

    This script stopped working with the new Mavericks update.

     

    The result is always a "0:00". Any ideas how to fix it?

     

    Thanks

  • by Opperman,

    Opperman Opperman Feb 5, 2014 10:05 AM in response to miguelmarques
    Level 1 (20 points)
    Feb 5, 2014 10:05 AM in response to miguelmarques

    I just tried this script on a batch of audio files under 10.9.1, and it worked perfectly for me. I might modify it just to display hours:minutes:seconds, however.

  • by sepphold,

    sepphold sepphold Aug 6, 2014 5:21 AM in response to Opperman
    Level 1 (0 points)
    Aug 6, 2014 5:21 AM in response to Opperman

    Hi,

     

    I tried this script on 10.8.5. But it always comes back 0:00

    I am a no-coder. I do understand the logic of it but not the language.

     

    Maybe you have any suggestions?

  • by VikingOSX,

    VikingOSX VikingOSX Aug 6, 2014 7:17 AM in response to miguelmarques
    Level 7 (20,606 points)
    Mac OS X
    Aug 6, 2014 7:17 AM in response to miguelmarques

    Alternative:

     

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

    set capture to {}

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

     

    set capture to do shell script audCmd & myAudioFile

    display dialog word 3 of capture

  • by miguelmarques,

    miguelmarques miguelmarques Aug 18, 2014 3:46 AM in response to VikingOSX
    Level 1 (0 points)
    Aug 18, 2014 3:46 AM in response to VikingOSX

    This one gives a syntax error, sorry!

  • by miguelmarques,

    miguelmarques miguelmarques Aug 18, 2014 3:49 AM in response to sepphold
    Level 1 (0 points)
    Aug 18, 2014 3:49 AM in response to sepphold

    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...

  • by Jacques Rioux,Helpful

    Jacques Rioux Jacques Rioux Aug 18, 2014 7:08 AM in response to miguelmarques
    Level 4 (3,408 points)
    Mac OS X
    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))

  • by miguelmarques,

    miguelmarques miguelmarques Aug 18, 2014 7:41 AM in response to Jacques Rioux
    Level 1 (0 points)
    Aug 18, 2014 7:41 AM in response to Jacques Rioux

    Thank you! That worked

  • by VikingOSX,

    VikingOSX VikingOSX Aug 20, 2014 7:47 AM in response to miguelmarques
    Level 7 (20,606 points)
    Mac OS X
    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 script audCmd & myAudioFile & audSubCmd1 & audSubCmd2

    display dialog capture