Skip to content

Instantly share code, notes, and snippets.

@vijaysharmay
Created March 3, 2018 10:10
Show Gist options
  • Select an option

  • Save vijaysharmay/2374b4b87a5d132149138ebe290d5214 to your computer and use it in GitHub Desktop.

Select an option

Save vijaysharmay/2374b4b87a5d132149138ebe290d5214 to your computer and use it in GitHub Desktop.
import subprocess
import os
import glob
import math
from functools import reduce
def getLength(filename):
result = subprocess.Popen(
["ffprobe", filename],
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT
)
info = [x for x in result.stdout.readlines() if b"Duration" in x][0].strip()
info = info.split(b',')[0].replace(b'Duration: ', b'')
info = info.split(b'.')[0]
return split(info)
def split(duration):
keys = ['hours', 'minutes', 'seconds']
duration = map(int, duration.split(b':'))
return dict(zip(keys, duration))
def add_durations(d1, d2):
keys = ['hours', 'minutes', 'seconds']
hours, minutes, seconds = (0, 0, 0, )
seconds = d1['seconds'] + d2['seconds']
minutes = d1['minutes'] + d2['minutes']
hours = d1['hours'] + d2['hours']
if seconds > 60:
minutes += math.floor(seconds / 60)
seconds = math.floor(seconds % 60)
if minutes > 60:
hours += math.floor(minutes / 60)
minutes = math.floor(minutes % 60)
return dict(zip(keys, [hours, minutes, seconds]))
cwd = os.getcwd()
path = '.'
path = os.path.normpath(path)
for root, dirs, files in os.walk('.'):
if len(files) > 0:
depth = root[len(path) + len(os.path.sep):].count(os.path.sep)
if depth == 0:
os.chdir(os.path.join(cwd, root))
print(f'{root} - {reduce(add_durations, [getLength(filename) for filename in glob.iglob("**/*.mp4", recursive=True)])}')
os.chdir(cwd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment