Question

I need to check the duration of a group of audio files. Is there a simple way to do this on the unix command-line?

> duration *

I have the amazing SoX app which has an option called stats that generates a set of audio info including duration. I am looking for a way to get only duration. I am flexible on output format, could be any of sample length, hh:mm:ss, or seconds. The latter would be my preference.

Was it helpful?

Solution

mp3info -p "%m:%02s\n" filename

gives you the length of the specified file in mm:ss format (mm can be greater than 59). For just the total number of seconds in the file, you'd use:

mp3info -p "%S\n" filename

To get the total length of all the mp3 files in seconds, AWK can help:

mp3info -p "%S\n" *.mp3 | awk 'BEGIN { s = 0 }; { s = s + $1 }; END { print s }'

OTHER TIPS

soxi -D filename
soxi -D *

Soxi queries metadata of audio files; D is the duration option. It supports globbing. Soxi's big brother sox does command-line audio processing.

ffmpeg -i <audiofile> 2>&1 | grep Duration

mediainfo will return to you the milliseconds of an audio file. Assuming the current directory only has audio files, the following

mediainfo --Inform="Audio;%Duration%" "Miley Cyrus - Wrecking Ball.mp3"

To calculate the duration of all audio in the local directory, this gist will help:

shopt -s nullglob
let playlist_duration_ms=0
for song_file in *.{mp3,ogg,m4a,flac,wav}; do
  playlist_duration_ms=$(expr $playlist_duration_ms + $(mediainfo --Inform="Audio;%Duration%" "$song_file"))
done
shopt -u nullglob

let playlist_duration_secs=$(expr $playlist_duration_ms / 1000)
let playlist_duration_mins=$(expr $playlist_duration_ms / 60000)
let playlist_duration_remaining_secs=$(expr $playlist_duration_secs - $(expr $playlist_duration_mins \* 60))

echo $playlist_duration_mins minutes, $playlist_duration_remaining_secs seconds

on OSX

Print the length of each audio file in the current dir:

afinfo * | awk '/estimated duration/ { print $3 }'

Include the filepath:

afinfo * | awk '/File:/ { song=$2 } /estimated duration/ { print song, $3 }'

In addition to cdosborn's answer, to calculate the length of all .mp3 files recursively in subfolders of current directory and show the total sum result in days:hours:minutes:seconds format:

In zsh:

afinfo **/*.mp3 | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'

In bash or sh:

find . -name "*.mp3" -exec afinfo {} \; | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'

The result is like this (7 days, 5 hours, 6 minutes, 58 seconds):

$ afinfo **/*.mp3 | awk '/estimated duration/ { print $3 }' | paste -sd+ - | bc | awk '{printf("%d:%02d:%02d:%02d\n",($1/60/60/24),($1/60/60%24),($1/60%60),($1%60))}'
7:05:06:58
$
sox --info -D file             --> duration in seconds
sox --info -d file             --> duration in HH:mm:ss.ss
sox --info file                --> metadata 

If you are interested in finding total duration of wav files in a directory using soxi you can use this:

soxi -D input_dir/*.wav | python -c "import sys;print(sum(float(l) for l in sys.stdin))

change input_dir according to your input directory. If you want to find max/min duration between all wav files feel free to change sum to max or min.

The raw duration in seconds can be obtained with a high degree of precision with the use of ffprobe of ffmpeg, as follows:

ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "filename.mp3" 2>/dev/null

The output, easy to use in further scripting, is formatted like this:

193.656236

Extending upon that, the following will measure the total duration in seconds of all .mp3 files in the current directory:

LENGTH=0; for file in *.mp3; do if [ -f "$file" ]; then LENGTH="$LENGTH+$(ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)"; fi; done; echo "$LENGTH" | bc

And to measure the total length of audio files of several extensions, another wildcard may be appended:

LENGTH=0; for file in *.mp3 *.ogg; do if [ -f "$file" ]; then LENGTH="$LENGTH+$(ffprobe -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)"; fi; done; echo "$LENGTH" | bc

A solution based on mplayer from commandlinefu by syssyphus that works with both audio and video files:

sudo apt-get install mplayer
find -type f -name "*.mp3" -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1

Get the total length of all video / audio in the current dir (and below) in H:m:s

Change the *.mp3 to whatever you want to match (e.g., *.avi, *.wav), you can remove it altogether if you want to check all files.

Example of output: 00:00:37

with ffprobe

ffprobe your_file.mp3 2>&1 | grep "Duration"

The output looks like this:

Duration: 00:44:33.50, start: 0.011995, bitrate: 128 kb/

Another soxi based answer including the file names and duration in hours, minutes and seconds format.

$for f in *amr; do printf "$f "; soxi -d $f; done

DGT20161216.amr 00:22:04.62
DGT20170108.amr 00:28:22.80
DGT20170117.amr 00:20:05.18

I expanded on Navid Naderi's answer here, and created a bash function that will give you a summary of every file's running time, plus some time totals in various formats (seconds, minutes, or total running time).

function sox_duration_total
{
  if [[ "$#" -lt 1 ]]; then
    echo "find files!"
    echo "  sox_duration_total *.wav"
    echo ""
    return
  fi
  for i in "$@"; do
    val=`soxi -d "$i"`
    echo "$val | $i"
  done
  soxi -D "$@" | python -c "import sys;print(\"\ntotal sec:    \" +str( sum(float(l) for l in sys.stdin)))"
  soxi -D "$@" | python -c "import sys;print(\"total min:    \" +str( sum(float(l) for l in sys.stdin)/60 ))"
  soxi -D "$@" | python -c "import sys;import datetime;print(\"running time: \" +str( datetime.timedelta(seconds=sum(float(l) for l in sys.stdin)) ))"
}

Output will look like:

00:01:40.97 | subatomicglue - inertialdecay - 01 - hard.wav
00:00:32.00 | subatomicglue - inertialdecay - 02 - acidbass.wav
00:04:50.80 | subatomicglue - inertialdecay - 03 - cause.of.a.new.dark.age.wav
00:02:08.00 | subatomicglue - inertialdecay - 04 - daybreak.falls.wav
00:05:20.75 | subatomicglue - inertialdecay - 05 - forestfloor.wav
00:03:56.32 | subatomicglue - inertialdecay - 06 - rabbithole.wav
00:03:50.40 | subatomicglue - inertialdecay - 07 - feedme.wav
00:06:03.97 | subatomicglue - inertialdecay - 08 - grand.wav
00:02:09.45 | subatomicglue - inertialdecay - 09 - strawberryflavoreddeath.wav
00:04:43.29 | subatomicglue - inertialdecay - 10 - subfloor.wav
00:03:36.96 | subatomicglue - inertialdecay - 11 - silicone.wav
00:04:28.47 | subatomicglue - inertialdecay - 12 - inertial decay.wav
00:01:23.81 | subatomicglue - inertialdecay - 13 - the.void.wav
00:01:18.86 | subatomicglue - inertialdecay - 14 - weet.wav

total sec:    2764.0637880000004
total min:    46.06772980000001
running time: 0:46:04.063788

mediainfo can do this, but mediainfo is one of those useful tools that's so badly documented that you almost need to know how to use it in order to learn how to use it (happens a lot in the linux world).

After hours of trials and reading high and low, I finally got it to generate a recursive comma-separated list of file names and their duration in milliseconds.

cd to the starting directory and issue the following command:

find "$(pwd)" -type f -print0 | xargs -0 -I {} mediainfo --Inform="General;%CompleteName%,%Duration%" {} > list.txt

The results will be output to list.txt.

(When you don't have afinfo at your disposal) I got it recursively for all my files

# install mp3info if not yet installed with
sudo apt-get install mp3info

with the find command, put the total seconds to a csv file (go to the directory with your e.g. mp3 files first)

find . -name "*.mp3" -exec mp3info {} -p "%S\r\n" >> totalSeconds.csv \;

Then open it in e.g. in LibreOffice and sum it up at the bottom (to get the hours) with

=SUM(A{start}:A{end})/60/60
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top