5

How would I search a directory of mp3 files for songs between a certain length. Ex:

findmp3 -min=03:00 -max=03:15 /music/mp3/ebm/

Would return all mp3 files in the emb/ directory having a song length between 3:00 and 3:15 minutes in length.

I use Linux Mint, Ubuntu, as well as CentOS.

  • Please always include your OS. Solutions very often depend on the Operating System being used. Are you using Unix, Linux, BSD, OSX, something else? Which version? – terdon Oct 13 '13 at 20:06
  • added to question. – a coder Oct 14 '13 at 14:50
3

First install mp3info, it should be in the repositories of your distribution (you have not said, so I assume you are using Linux of some sort). If you have a debian-based distribution, you can do so with

sudo apt-get install mp3info

Once mp3info is installed, you can search the directory music for songs of a certain length with this command:

find music/ -name "*mp3" | 
  while IFS= read -r f; do 
   length=$(mp3info -p "%S" "$f"); 
   if [[ "$length" -ge "180" && "$length" -le "195" ]]; then 
     echo "$f"; 
   fi;  
done

The command above will search music/ for mp3 files, and print their name and length if that length is grater or equal to 180 seconds (3:00) and less than or equal to 195 seconds (3:15). See man mp3info for more details on its output format.

If you want to be able to enter times in MM:SS format, it gets a bit more complex:

#!/usr/bin/env bash

## Convert MM:SS to seconds.
## The date is random, you can use your birthday if you want.
## The important part is not specifying a time so that 00:00:00
## is returned.
d=$(date -d "1/1/2013" +%s);

## Now add the number of minutes and seconds
## you give as the first argument
min=$(date -d "1/1/2013 00:$1" +%s);
## The same for the second arument
max=$(date -d "1/1/2013 00:$2" +%s);

## Search the target directory for files
## of the correct length.
find "$3" -name "*mp3" | 
  while IFS= read -r file; do 
   length=$(mp3info -p "%m:%s" "$file"); 
   ## Convert the actual length of the song (mm:ss format)
   ## to seconds so it can be compared.
   lengthsec=$(date -d "1/1/2013 00:$length" +%s);

   ## Compare the length to the $min and $max
   if [[ ($lengthsec -ge $min ) && ($lengthsec -le $max ) ]]; then 
       echo "$file"; 
   fi; 
done

Save the script above as findmp3 and run it like this:

findmp3 3:00 3:15 music/
  • This works nicely. I modified your script to include the length of each song as it loops through the directories. – a coder Oct 14 '13 at 15:38
  • @acoder lol, I had that too at the beginning and removed it as unneeded :). – terdon Oct 14 '13 at 15:42
  • Btw here is my modified script: pastebin.com/ykYJiC6Q – a coder Jan 25 at 15:57
3

I doubt that there's an existing findmp3 tool. Following the unix philosophy, it can be built from find to find .mp3 files, another tool to report the length of each file that find finds, and some shell/text processing glue.

SoX is a commonly available utility to work with sound files (sox is to sound what sed or awk are to text files). The command soxi displays information about a sound file. In particular, soxi -D prints the duration in seconds.

For each .mp3 file, the snippet below calls soxi and parses its output. If the duration is within the desired range, the sh call returns a success status, so the -print action is executed to print the file name.

find /music/mp3/ebm -type f -name .mp3 -exec sh -c '
    d=$(soxi -D "$0")
    d=${d%.*} # truncate to an integer number of seconds
    [ $((d >= 3*60 && d < 3*60+15)) -eq 1 ]
' {} \; -print

In bash, ksh93 or zsh, you can use recursive globbing instead of find. In ksh, run set -o globstar first. In bash, run shopt -s globstar first. Note that in bash (but not in ksh or zsh), **/ recurses through symbolic links to directories.

for f in /music/mp3/ebm/**/*.mp3; do
  d=$(soxi -D "$0")
  d=${d%.*} # truncate to an integer number of seconds (needed in bash only, ksh93 and zsh understand floating point numbers)
  if ((d >= 3*60 && d < 3*60+15)); then
    echo "$f"
  fi
done
  • of course, your soxi should be suppor mp3 format.thank you Gilles. – PersianGulf Oct 13 '13 at 22:02
1

Using ffmpeg:

find . -name \*.mp3|while IFS= read -r l;do ffprobe -v 0 -i "$l" -show_streams|awk -F= '$1=="duration"&&$2>=180&&$2<=195'|read&&echo "$l";done

mp3info oneliner:

find . -name \*.mp3 -exec mp3info -p '%S %f\n' {} +|awk '$1>=180&&$1<=195'|cut -d' ' -f2-

Or in OS X:

mdfind 'kMDItemDurationSeconds>=180&&kMDItemDurationSeconds<=195&&kMDItemContentType=public.mp3' -onlyin .

0
ffmpeg -i  yourmp3.mp3  2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g'  |awk {'print $1'}

With above command, you can get your duration, So you can script to specify a domain for duration. Also you can use:

find yourPath -iname "*mp3" -exec ffmpeg -i  {}   2>&1 | grep Duration | sed 's/Duration: \(.*\), start/\1/g'  |awk {'print $1'}

Replace yourPath with root of your mp3 repository.

  • 1
    This doesn't answer the question (though it's certainly a good starting point for the script that would be necessary), and you'd be better off using ffprobe's -show_format option, like: ffprobe -show_format -i file.mp3 | sed -n '/duration/s/.*=//p' – evilsoup Oct 13 '13 at 21:44
  • i do apt-file search ffprobe and i didn't any result on sid debian repository. – PersianGulf Oct 13 '13 at 21:57
  • Strange, it is part of the libav-tools package. – terdon Oct 13 '13 at 22:14
  • Are you sure? wich distro?i installed libav-tools, but it has avprobe, Do you mean avprobe? – PersianGulf Oct 13 '13 at 22:42
  • No. He means ffprobe. It is not part of unstable. Removed back in 2011. E.g. for i386 stable vs. unstable – Runium Oct 13 '13 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.