Skip to content

Instantly share code, notes, and snippets.

@alyssadev
Last active July 1, 2021 23:16
Show Gist options
  • Save alyssadev/88dd2c63b37ae4adca8b4bb68f2ff837 to your computer and use it in GitHub Desktop.
Save alyssadev/88dd2c63b37ae4adca8b4bb68f2ff837 to your computer and use it in GitHub Desktop.
A script to get total duration of files provided in arguments. Python3 and ffprobe
#!/usr/bin/env python3
from sys import stderr, argv, exit
import subprocess
from math import floor
def hms(inp):
s,ms = divmod(inp,1)
m,s = divmod(s,60)
h,m = divmod(m,60)
return f"{h:02.0f}:{m:02.0f}:{s:02.0f}.{floor(ms*100):02.0f}"
def get_video_duration(fileloc) :
command = ['ffprobe',
'-v', 'fatal',
'-show_entries', 'stream=duration',
'-of', 'default=noprint_wrappers=1:nokey=1',
fileloc, '-sexagesimal']
ffmpeg = subprocess.Popen(command, stderr=subprocess.PIPE ,stdout = subprocess.PIPE )
out, err = ffmpeg.communicate()
if(err) : print(err)
out = out.decode('utf-8').strip()
return map(float,out.split(":"))
def main():
tot = 0.0
for fn in argv[1:]:
h,m,s = get_video_duration(fn)
tot += h*60*60 + m*60 + s
print(f"{hms(tot)}", end="\r", file=stderr)
print(hms(tot))
return 0
if __name__ == "__main__":
try:
exit(main())
except KeyboardInterrupt:
exit(127)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment