Skip to content

Instantly share code, notes, and snippets.

@gotev
Last active November 15, 2022 00:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotev/b89648006752813d93b4993ee453e37d to your computer and use it in GitHub Desktop.
Save gotev/b89648006752813d93b4993ee453e37d to your computer and use it in GitHub Desktop.
List mp4 files in a folder and get duration for each file and total duration
#!/usr/bin/env python3
import os
import re
import pathlib
import ffmpeg
import time
files = pathlib.Path('.').glob('*.mp4')
files = sorted(files, key=lambda x: float(re.findall("(\d+)", os.path.basename(x))[0]))
totalDuration = 0
for file in files:
info = ffmpeg.probe(file)
durationSeconds = round(float(info['format']['duration']))
totalDuration += durationSeconds
duration = time.strftime('%H:%M:%S', time.gmtime(durationSeconds))
print(str(file) + ' -> ' + duration)
print('Total Duration: ' + time.strftime('%H:%M:%S', time.gmtime(totalDuration)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment