Skip to content

Instantly share code, notes, and snippets.

@pongo
Last active September 27, 2024 09:09
Show Gist options
  • Save pongo/6f5a153be5f3c4b306977a41e0605be3 to your computer and use it in GitHub Desktop.
Save pongo/6f5a153be5f3c4b306977a41e0605be3 to your computer and use it in GitHub Desktop.
Get video FPS in python via ffmpeg (ffprobe)
from pathlib import Path
from typing import TypeAlias
import ffmpeg # https://github.com/kkroening/ffmpeg-python
FPS: TypeAlias = float
def get_fps(video_path: str | Path) -> FPS | None:
try:
probe = ffmpeg.probe(video_path)
except ffmpeg.Error as e:
print(e)
return None
video = next((stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
if video is None:
print('No video stream found')
return None
r_frame_rate = video['r_frame_rate']
if '/' not in r_frame_rate:
print('Wrong r_frame_rate:', r_frame_rate)
return None
r1, r2 = r_frame_rate.split('/')
return float(r1) / float(r2)
print(get_fps('video.mp4'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment