Skip to content

Instantly share code, notes, and snippets.

@pib
Created October 18, 2012 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pib/3913668 to your computer and use it in GitHub Desktop.
Save pib/3913668 to your computer and use it in GitHub Desktop.
Script to count the total time in a directory full of MP3s
from os import walk
from os.path import join
from subprocess import check_output
SEC_PER_MIN = 60
SEC_PER_HOUR = 60 * SEC_PER_MIN
SEC_PER_DAY = 24 * SEC_PER_HOUR
def split_time(seconds):
days, seconds = divmod(seconds, SEC_PER_DAY)
hours, seconds = divmod(seconds, SEC_PER_HOUR)
minutes, seconds = divmod(seconds, SEC_PER_MIN)
return days, hours, minutes, seconds
def get_song_seconds(path):
len_str = check_output(['mp3info', '-p', '%S', path])
return int(len_str)
def all_song_lengths(base_path):
for dirpath, dirnames, filenames in walk(base_path):
for filename in filenames:
if filename.endswith('.mp3'):
yield get_song_seconds(join(dirpath, filename))
def main():
total_seconds = sum(all_song_lengths('.'))
print 'Total Seconds:', total_seconds
days, hours, minutes, seconds = split_time(total_seconds)
print "{} day{}, {} hour{}, {} minute{}, {} second{}".format(
days, 's' if days != 1 else '',
hours, 's' if hours != 1 else '',
minutes, 's' if minutes != 1 else '',
seconds, 's' if seconds != 1 else '')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment