Skip to content

Instantly share code, notes, and snippets.

@glowinthedark
Created November 20, 2022 15:04
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 glowinthedark/61bbc2401099f0d98aa505af9ea60c33 to your computer and use it in GitHub Desktop.
Save glowinthedark/61bbc2401099f0d98aa505af9ea60c33 to your computer and use it in GitHub Desktop.
Get total duration of media files in the current folder
#!/usr/bin/env python3
import datetime
import subprocess
import sys
from pathlib import Path
# USAGE:
# Total duration of MP4 files (default)
# mp4_duration_sum.py
#
# Total duration of MP3 files
# mp4_duration_sum.py '*.mp3'
#
# Total duration of AAC files
# mp4_duration_sum.py '*.aac'
#
# NOTE: any file type supported by ffmpeg/ffprobe can be used
def get_media_length(filename: str):
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", filename],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return int(float(result.stdout))
def seconds_to_delta(secs):
return str(datetime.timedelta(seconds=secs))
stats:dict = {}
for media in Path('.').glob(sys.argv[1]):
media_length_secs = get_media_length(str(media.absolute()))
length_delta = seconds_to_delta(media_length_secs)
print(f'{length_delta:0>8} {media.name}')
stats[media.name] = media_length_secs
total_seconds = sum(stats.values())
total_hours = datetime.timedelta(seconds=total_seconds).total_seconds()/3600
print(f'{"":-<64}')
print(f'Total time: {seconds_to_delta(total_seconds)} — {total_hours:0.2f} hours')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment