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
11 changes: 11 additions & 0 deletions scenedetect/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@
# e.g. 23.976 should be detected as 24000/1001, 29.97 should be detected as 30000/1001, etc.
_NTSC_DETECTION_TOLERANCE: float = 1e-3

# Number of decimal places kept when scaling seconds to the output precision before truncating,
# e.g. 0.29 * 100 == 28.999999999999996 should truncate to 29, not 28. A double carries ~15-16
# significant digits, so for timestamps up to a few days scaling by 10**precision leaves an error
# around 1e-9 or less. Rounding to 6 places removes that error while still being far finer than
# any real frame boundary, so it never moves a value that is not actually on a boundary.
_TRUNCATION_FLOAT_DIGITS: int = 6


##
## Helpers
Expand Down Expand Up @@ -455,6 +462,10 @@ 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 (see _TRUNCATION_FLOAT_DIGITS).
scaled = round(secs * 10**precision, _TRUNCATION_FLOAT_DIGITS)
secs = math.floor(scaled) / 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 @@ -864,3 +864,4 @@ Development
- [bugfix] The CLI now exits with 1 when no frames could be read from the input video, or seeking to the `time -s`/`--start` time fails, instead of 0
- [bugfix] `--kernel-size`/`-k` values the detectors cannot use (even numbers, 1, 0 and negatives other than -1) now give a usage error instead of a traceback, and `-k -1` now means auto, like the config file. A config file with `kernel-size = 1` is now an error [#604](https://github.com/Breakthrough/PySceneDetect/issues/604)
- [bugfix] Out of range values for `detect-adaptive --frame-window` (below 1) and `save-images` `--num-images` (below 1), `--scale`, `--height` and `--width` (negative) now give a usage error instead of a traceback, and the same values are rejected in a config file [#609](https://github.com/Breakthrough/PySceneDetect/issues/609)
- [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