Created
November 20, 2022 15:04
-
-
Save glowinthedark/61bbc2401099f0d98aa505af9ea60c33 to your computer and use it in GitHub Desktop.
Get total duration of media files in the current 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
#!/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