How to add a lot of values?

In a previous post, I learned how to extract duration from ffmpeg for a single file.

 ffmpeg -i file.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d , 

which outputs something similar to 00:08:07.98 .

What I would like to end up with is a script where I can say

 get_duration.sh * 

and he will add all the lengths of the duration and output something similar to 04:108:1107.198 .

It does not have to convert minutes to hours and so on. It would be nice if =)

I can list the entire length on

 for f in *; do ffmpeg -i "$f" 2>&1 | grep Duration | awk '{print $2}' | tr -d ,; done 

But how to add these weird formatted numbers?

+4
source share
1 answer

Using the following awk script should work fine:

 BEGIN { h=0; m=0; s=0; cs=0; } /Duration:/ { split($2, time_arr, ":"); h = h + time_arr[1]; m = m + time_arr[2]; split(time_arr[3],time_arr2,"."); s = s + time_arr2[1]; cs = cs + time_arr2[2]; } END { s = s + int(cs/100); cs = cs % 100; m = m + int(s / 60); s = s % 60; h = h + int(m / 60); m = m % 60; printf "%02d:%02d:%02d.%02d\n", h, m, s, cs; } 

Put this in add_durations.awk , then you can do:

 for f in *; do ffmpeg -i "$f" 2>&1; done | awk -f add_durations.awk 

Please note that this also converts hours, etc. for you:).

+6
source

All Articles