Skip to content

Instantly share code, notes, and snippets.

@martin-martin
Last active July 28, 2021 13:14
Show Gist options
  • Save martin-martin/917e80bbc52ba34017e1ed94496f2966 to your computer and use it in GitHub Desktop.
Save martin-martin/917e80bbc52ba34017e1ed94496f2966 to your computer and use it in GitHub Desktop.
Check for correct frame rate and aspect ratio of each video lesson, and determine total length of the course
# Requires installation of `ffmpeg` and `ffmpeg-python`:
# - https://www.ffmpeg.org
# - https://pypi.org/project/ffmpeg-python/
import fractions
from pathlib import Path
import ffmpeg
recordings_folder = Path("/Users/martin/Documents/real_python/my-tutorials/ggplotting/recordings")
# Or, run in the current folder:
# recordings_folder = Path.cwd()
probes = {}
for mfile in recordings_folder.iterdir():
if mfile.suffix == ".mov":
probe = ffmpeg.probe(mfile)
video_streams = [stream for stream in probe["streams"] if stream["codec_type"] == "video"]
if len(video_streams) > 1:
raise Exception(f"{mfile.name}: More than one video stream.")
probes[mfile.name] = video_streams[0] # Usually there is only 1
data = {}
for filename, info in probes.items():
data[filename] = {
"duration_min": float(info["duration"]) / 60, # In minutes
"r_frame_rate": fractions.Fraction(info["r_frame_rate"]),
"resolution": fractions.Fraction(info["width"], info["height"])
}
total_course_length = 0
for filename, info in data.items():
if info["r_frame_rate"].numerator < 30:
print(f"{filename}: Low frame rate ({info['r_frame_rate']})")
if info["resolution"] != fractions.Fraction(16, 9):
print(f"{filename}: Wrong resolution ({info['resolution']})")
total_course_length += info["duration_min"]
print(f"{len(data)} lessons")
print(f"{total_course_length:.2f} minutes total")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment