check duration of audio files on the command-line

Solution 1:

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.

Solution 2:

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 }'

Solution 3:

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

Solution 4:

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