Last active
September 27, 2024 09:09
-
-
Save pongo/6f5a153be5f3c4b306977a41e0605be3 to your computer and use it in GitHub Desktop.
Get video FPS in python via ffmpeg (ffprobe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print(get_fps('video.mp4')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment