MCPcopy Create free account
hub / github.com/Breakthrough/PySceneDetect / framerate_to_fraction

Function framerate_to_fraction

scenedetect/common.py:126–145  ·  view source on GitHub ↗

Convert a framerate value to an exact rational Fraction. Detects NTSC-derived framerates of the form ``N * 1000/1001`` (e.g. 23.976 -> 24000/1001, 29.97 -> 30000/1001, 47.952 -> 48000/1001) for any positive integer ``N`` and returns their exact rational representation. Whole-number fram

(fps: "FrameRate")

Source from the content-addressed store, hash-verified

124
125
126def framerate_to_fraction(fps: "FrameRate") -> Fraction:
127 """Convert a framerate value to an exact rational Fraction.
128
129 Detects NTSC-derived framerates of the form ``N * 1000/1001`` (e.g. 23.976 -> 24000/1001,
130 29.97 -> 30000/1001, 47.952 -> 48000/1001) for any positive integer ``N`` and returns
131 their exact rational representation. Whole-number framerates are returned as
132 ``Fraction(N, 1)``. Other values fall back to ``limit_denominator(10000)`` for a clean
133 rational approximation. ``Fraction`` inputs are returned directly without conversion.
134 """
135 if fps <= MAX_FPS_DELTA:
136 raise ValueError("Framerate must be positive and greater than zero.")
137 if isinstance(fps, Fraction):
138 return fps
139 if fps == int(fps):
140 return Fraction(int(fps), 1)
141 # Invert fps = N * 1000/1001 to recover N, then verify within tolerance.
142 base = round(fps * 1001 / 1000)
143 if base > 0 and abs(base * 1000 / 1001 - fps) < _NTSC_DETECTION_TOLERANCE:
144 return Fraction(base * 1000, 1001)
145 return Fraction(fps).limit_denominator(10000)
146
147
148class Interpolation(Enum):

Callers 10

__init__Method · 0.90
__init__Method · 0.90
frame_rateMethod · 0.90
_open_captureMethod · 0.90
__init__Method · 0.90
_ensure_fractionalMethod · 0.85

Calls

no outgoing calls