1

I have a directory with a lot of mp3 files, and I need a simple way to find the accumulated duration for them. I know that I can find the duration for one file with

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

I also know that I can run this command on all mp3 files in a directory with the command

for file in *.mp3; do ffmpeg -i "$file" 2>&1 | grep Duration; done

This can be somewhat filtered with

for file in *.mp3; do ffmpeg -i "$file" 2>&1 | grep Duration | cut -f4 -d ' '; done

But how do I sum it all up? Using ffmpeg is not necessary. The output format is not so important either. Seconds or mm:ss or something similar will do. I would like it to look something like this:

$ <command>
84:33
2

You can get exactly the duration in seconds, then sum them with bc:

for file in *.mp3;do ffprobe -v error -select_streams a:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$file";done|paste -sd+|bc -l

Convert this number to HH:MM:SS format by yourself. e.g. https://stackoverflow.com/a/12199816/6481121

0

Ipors Sicer's answer worked perfectly. To add to his answer, I stripped everything after the dot and used this answer with a slight modification to get this:

secs=$(for file in *.mp3;
    do ffprobe -v error -select_streams a:0 -show_entries stream=duration -of default=noprint_wrappers=1:nokey=1 "$file";
done | paste -sd+|bc -l|cut -f1 -d '.'); 
printf '%d:%d\n' $(($secs/60)) $(($secs%60))

And I got this output:

87:2

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.