Skip to content

Instantly share code, notes, and snippets.

@ento
Created December 28, 2011 02:37
Show Gist options
  • Save ento/1525902 to your computer and use it in GitHub Desktop.
Save ento/1525902 to your computer and use it in GitHub Desktop.
Calculate the total length of music/sound files in a folder
import re
import glob
import commands
duration_re = re.compile(r'(?P<hour>\d+):(?P<minute>\d+):(?P<second>\d+\.\d+),')
hour = minute = second = 0.0
for f in glob.glob("./*"):
s = commands.getstatusoutput('ffmpeg -i %s 2>&1 | egrep "Duration" | cut -d " " -f 4' % f)[1]
m = duration_re.match(s)
hour += int(m.group('hour'))
minute += int(m.group('minute'))
second += float(m.group('second'))
total_seconds = hour * 3600 + minute * 60 + second
tmin, tsec = divmod(total_seconds, 60)
thour, tmin = divmod(tmin, 60)
print thour, tmin, tsec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment