diff --git a/scenedetect/common.py b/scenedetect/common.py index f3846693..e7142e0f 100644 --- a/scenedetect/common.py +++ b/scenedetect/common.py @@ -455,6 +455,9 @@ def get_timecode( secs = self.frame_num / float(self.frame_rate) else: secs = self.seconds + if not use_rounding: + # Allow for float error when truncating, e.g. 0.29 * 100 == 28.999999999999996. + secs = math.floor(round(secs * 10**precision, 6)) / 10**precision hrs = int(secs / _SECONDS_PER_HOUR) secs -= hrs * _SECONDS_PER_HOUR mins = int(secs / _SECONDS_PER_MINUTE) diff --git a/tests/test_timecode.py b/tests/test_timecode.py index 863e9fe3..2468074f 100644 --- a/tests/test_timecode.py +++ b/tests/test_timecode.py @@ -372,6 +372,30 @@ def test_precision(): assert FrameTimecode(990, fps).get_timecode(precision=0, use_rounding=False) == "00:00:00" +def test_precision_truncation(): + """Truncating must drop the extra digits, never round up into the last digit that is kept.""" + # 1.99996667s, e.g. a frame position from a 90 kHz time base. + tc = FrameTimecode(Timecode(pts=179997, time_base=Fraction(1, 90000)), Fraction(30000, 1001)) + assert tc.get_timecode(precision=3, use_rounding=False) == "00:00:01.999" + assert tc.get_timecode(precision=2, use_rounding=False) == "00:00:01.99" + assert tc.get_timecode(precision=0, use_rounding=False) == "00:00:01" + assert tc.get_timecode(precision=3, use_rounding=True) == "00:00:02.000" + + # Frame 911 is at 37.996s. + tc = FrameTimecode(911, Fraction(24000, 1001)) + assert tc.get_timecode(precision=1, use_rounding=False) == "00:00:37.9" + assert tc.get_timecode(precision=1, use_rounding=True) == "00:00:38.0" + + tc = FrameTimecode(59.99996, 24.0) + assert tc.get_timecode(use_rounding=False, nearest_frame=False) == "00:00:59.999" + + # Float error must not truncate exact values down, e.g. 504 / (24000 / 1001) == 21.021. + tc = FrameTimecode(504, Fraction(24000, 1001)) + assert tc.get_timecode(precision=3, use_rounding=False) == "00:00:21.021" + tc = FrameTimecode(61.29, 24.0) + assert tc.get_timecode(precision=2, use_rounding=False, nearest_frame=False) == "00:01:01.29" + + def test_rational_framerate_precision(): """Rational framerates should round-trip frame/second conversions without drift.""" fps = Fraction(24000, 1001) diff --git a/website/pages/changelog.md b/website/pages/changelog.md index 99338e7e..35fb1578 100644 --- a/website/pages/changelog.md +++ b/website/pages/changelog.md @@ -859,3 +859,4 @@ Development ## PySceneDetect 0.8 (TBD) - [general] The root `pyproject.toml` is now `scenedetect` again (it had a development-only `scenedetect-core` name since 0.7.1). `pip install -e .` now installs the same distribution as PyPI. The `[opencv]` and `[opencv-headless]` extras of the development config were removed because `opencv-python` is now a declared dependency [#558](https://github.com/Breakthrough/PySceneDetect/issues/558) + - [bugfix] `FrameTimecode.get_timecode()` with `use_rounding=False` now truncates instead of sometimes rounding up the last digit. Previously 0.4996 seconds with `precision=2` gave `00:00:00.50`, and 1.99997 seconds gave `00:00:01.000` instead of `00:00:01.999`. [#602](https://github.com/Breakthrough/PySceneDetect/pull/602)