Skip to content

Instantly share code, notes, and snippets.

@jwoglom
Created January 3, 2021 00:51
Show Gist options
  • Save jwoglom/a3bd37f97396d03e7ad035d1ef7bc847 to your computer and use it in GitHub Desktop.
Save jwoglom/a3bd37f97396d03e7ad035d1ef7bc847 to your computer and use it in GitHub Desktop.
video-length.py: sums the duration of all video files passed as arguments using ffmpeg
#!/usr/bin/env python3
# video-length.py: sums the duration of all video files passed as arguments using ffmpeg
import subprocess
import sys
import datetime
files = sys.argv[1:]
CMD = "ffmpeg -i file:\"%s\" 2>&1 | grep Duration | cut -d ' ' -f 4 | sed s/,//"
times = []
for f in files:
r = subprocess.run(CMD % f, shell=True, stdout=subprocess.PIPE)
times.append(r.stdout.decode().strip())
def sumTimes(timeList):
delta = datetime.timedelta(seconds=0)
for t in timeList:
h, m, s = t.split(":")
delta += datetime.timedelta(hours=float(h))
delta += datetime.timedelta(minutes=float(m))
delta += datetime.timedelta(seconds=float(s))
return str(delta)
print(sumTimes(times))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment