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