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 a
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
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
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 }'
sox --info -D file --> duration in seconds
sox --info -d file --> duration in HH:mm:ss.ss
sox --info file --> metadata
(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
ffmpeg -i <audiofile> 2>&1 | grep Duration