I want to make an automator workflow to find certain audio files in a folder, but I want it to look at the duration of those files. I'm looking for quite short audio samples of under 2 seconds. It doesn't appear to have the ability to filter by duration. Is there any other way to accomplish this in Automator?

Alternatively I can easily make a Smart Folder that contains what I'm looking for, but then Automator cannot find the contents of said Smart Folder in order to further manipulate them. Is there any way to get the contents of a smart folder in Automator?

  • What type of audio files? Are there different types, e.g. .mp3, .aiff, etc.? Are there only audio file in the target location? What are you going to do with the files with less than 2 second duration? I can give you a bash script that can isolate the audio files with less than 2 second duration if that's helpful. It can be use in a Run Shell Script action in Automator however it might be better if you add the workflow to the question too. – user3439894 Apr 18 '17 at 3:22
  • That would help, in fact someone is already working on a script that accomplishes my ultimate goal, but there's an issue with spaces we have not been able to overcome yet so I was exploring other solutions. See apple.stackexchange.com/questions/280950/… for the entire use case. – JVC Apr 18 '17 at 4:19
  • I took a quick look at what you linked in the comment and don't want to deal with it as it's late. I'll look at it later tomorrow. I'll post an answer that address the filtering of the music files with less than a 2 second duration of this question without regard to the other question until I can throughly examine the other question. – user3439894 Apr 18 '17 at 5:04
  • It depends on music format. macos doesn't have built in support for most useful audio formats (not even flac and opus), so for them it's certainly easier to use your own index and code. the added benefit would be that you can make it cross platform. – Display Name Apr 19 '17 at 5:42
  • I'm just filtering on wav and aif files, that part is easy. The problem now is getting the code provided below to work with fractional seconds. – JVC Apr 19 '17 at 5:43

Lets assume you have some action that's passing off files to a Filter Finder Items action, set as Find files where: All of the following are true and Kind is music.

This ensures what's passed to the next action, a Run Shell Script action, set as Shell: /bin/bash and Pass input as arguments are music files.

Replace the default code in the Run Shell Script action, having the above settings, with the following code:

for f in "$@"; do
    d="$(afinfo -r "$f" | awk '/estimated duration:/{print int($3)}')"
    if [[ $d -lt 2 ]]; then
        echo "$f"

The results of the above code with be the fully qualified pathname of music files with less than a 2 second duration and is passed to the next action as a list.

To address your second comment to my original answer, use the following code in place of the code above:

for f in "$@"; do
    d="$(afinfo -r "$f" | awk '/estimated duration:/{print $3}')"
    if (( $(echo "1.5 > $d" | bc -l) )); then
        echo "$f"
  • Well hot damn. So far with your script, one line from the script in the other question, and a little Automagic, I've got a working solution. I'll play with it a bit more to see if it breaks down anywhere and respond with results tomorrow. But as far as this particular question goes, your answer nails it. Thanks! – JVC Apr 18 '17 at 5:28
  • Rats, one other question. Can this be made to use non-integer durations? Like what if I want 1.5 seconds or less? I couldn't get it to work that way, not sure what's needed. – JVC Apr 18 '17 at 22:23
  • @Jonathan van Clute, I've updated the answer to handle floating point numbers. – user3439894 Apr 19 '17 at 5:47
  • So awesome! I had almost figured it out but wasn't likely going to get there for a while. Thanks again! – JVC Apr 19 '17 at 6:04
  • @Jonathan van Clute, BTW You can reverse "1.5 > $d" to "$d < 1.5" to make it easier to read and adjust the hard coded value of the equation. – user3439894 Apr 19 '17 at 6:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .