Skip to content

Instantly share code, notes, and snippets.

@meatyite
Last active March 31, 2019 12:16
Show Gist options
  • Save meatyite/c74c93dae2aa9aa26b975dcbc05eeab7 to your computer and use it in GitHub Desktop.
Save meatyite/c74c93dae2aa9aa26b975dcbc05eeab7 to your computer and use it in GitHub Desktop.
calculate_total_time_of_media.py
import subprocess, os, datetime
from glob import glob
from threading import Thread
from sys import argv
videoFileExtensions = ['mkv', 'mp4', 'ogv', 'avi', 'wmv', 'flv']
total_time = datetime.timedelta(hours=0, minutes=0, seconds=0)
def getLength(filename):
global total_time
result = subprocess.Popen(["ffprobe", filename], stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
for x in result.stdout.readlines():
x = x.decode()
if "Duration" in x:
x = x.replace('\n', '').replace(',','').replace(' Duration: ', '')[:-len(' start: 0.000000, bitrate: 333 kb/s ')].replace('.', '')
y = x.split(':')
total_time += datetime.timedelta(hours=int(y[0]), minutes=int(y[1]), seconds=int(y[2]))
print(total_time)
def isVideoFile(filename):
for videoFileExtension in videoFileExtensions:
if filename.endswith('.' + videoFileExtension):
return True
return False
def search_for_videos(folder):
files_and_folders = glob(folder + '/*')
for file_or_folder in files_and_folders:
if os.path.isfile(file_or_folder):
if isVideoFile(file_or_folder):
getLength(file_or_folder)
elif os.path.isdir(file_or_folder):
search_for_videos(file_or_folder)
search_for_videos(argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment