4

In my previous post I learned how to extract the duration from ffmpeg for one file.

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

which would output 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 it would add all the duration lengths and output something similar to 04:108:1107.198.

It doesn't have to convert minutes to hours and so on. It would be nice though =)

I can list all the length by

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

But how do I add these strange formatted numbers?

Community
  • 1
  • 1
Louise
  • 6,193
  • 13
  • 36
  • 36

1 Answers1

6

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

Note this will also convert the hours etc for you :).

MGwynne
  • 3,512
  • 1
  • 23
  • 35
  • 4
    Be careful: I think your millisecond value will print 9 ms as 0.9, rather than 0.009. – Jonathan Leffler Jun 04 '11 at 22:07
  • Thanks! Hopefully corrected now. I stupidly misread it as "s.ms" not hundredth points of seconds. I've added zero padding for all numbers now, like the ffmpeg command. – MGwynne Jun 05 '11 at 06:38