Created
December 28, 2011 02:37
-
-
Save ento/1525902 to your computer and use it in GitHub Desktop.
Calculate the total length of music/sound files in a folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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