Skip to content

Instantly share code, notes, and snippets.

@JasonSpine
Created January 23, 2017 09:02
Show Gist options
  • Save JasonSpine/54abddd0082af3b6c740f498c6a489f5 to your computer and use it in GitHub Desktop.
Save JasonSpine/54abddd0082af3b6c740f498c6a489f5 to your computer and use it in GitHub Desktop.
Get overall time of all mp3s in the current directory
import os,commands,re
path = './' # the directory path to calc
listing = os.listdir(path) # list all files
result=0 # result [seconds]
for i in listing:
if i[-4:].lower()==".mp3": # check the file extension
command = "ffmpeg -i '%s' 2>&1 | grep Duration"%i
durationString=commands.getoutput(command)
results=re.findall("(\d{2}):(\d{2}):(\d{2}).(\d{2})",durationString)
if results:
result+=int(results[0][0])*60*60
result+=int(results[0][1])*60
result+=int(results[0][2])
#formatting the result
hours=result/(60*60)
result-=hours*60*60
minutes=result/60
result-=minutes*60
seconds=result
print "Overall Mp3s time: %02d:%02d:%02d"%(hours,minutes,seconds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment