Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions scenedetect/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions tests/test_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions website/pages/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading