Coverage for pyguymer3/media/return_video_frame_rate.py: 72%
25 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
« prev ^ index » next coverage.py v7.9.2, created at 2025-07-08 18:47 +0000
1#!/usr/bin/env python3
3# Define function ...
4def return_video_frame_rate(
5 fname,
6 /,
7 *,
8 cwd = None,
9 debug = __debug__,
10 ensureNFC = True,
11 ffprobePath = None,
12 playlist = -1,
13 timeout = 60.0,
14):
15 # Import standard modules ...
16 import shutil
18 # Import sub-functions ...
19 from .__ffprobe__ import __ffprobe__
20 from .ffprobe import ffprobe
22 # **************************************************************************
24 # Try to find the paths if the user did not provide them ...
25 if ffprobePath is None:
26 ffprobePath = shutil.which("ffprobe")
27 assert ffprobePath is not None, "\"ffprobe\" is not installed"
29 # **************************************************************************
31 # Make sure that this fname/playlist combination is in the global dictionary ...
32 if fname not in __ffprobe__:
33 __ffprobe__[fname] = {}
34 if playlist not in __ffprobe__[fname]:
35 if debug:
36 print(f"INFO: Running ffprobe(\"{fname}\", {playlist:d}) ...")
37 __ffprobe__[fname][playlist] = ffprobe(
38 fname,
39 cwd = cwd,
40 ensureNFC = ensureNFC,
41 ffprobePath = ffprobePath,
42 playlist = playlist,
43 timeout = timeout,
44 )
46 # Loop over streams ...
47 for stream in __ffprobe__[fname][playlist]["streams"]:
48 # Skip stream if it is not video ...
49 if stream["codec_type"].strip().lower() != "video":
50 continue
52 # Check format ...
53 if "/" not in stream["avg_frame_rate"]:
54 raise Exception("\"avg_frame_rate\" did not contain a \"/\"") from None
56 # Return frame rate ...
57 a = stream["avg_frame_rate"].split("/")[0] # [#]
58 b = stream["avg_frame_rate"].split("/")[1] # [s]
59 fps = -1.0 # [Hz]
60 if int(b) != 0:
61 fps = float(a) / float(b) # [Hz]
62 return fps # [Hz]
64 # Return error ...
65 return -1.0 # [Hz]