Skip to content

Commit 5900c8c

Browse files
committed
[timecode] Ensure timecodes always use most accurate representation when calculating seconds #168
1 parent b3c542a commit 5900c8c

File tree

4 files changed

+8
-21
lines changed

4 files changed

+8
-21
lines changed

scenedetect/common.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,26 +352,19 @@ def get_seconds(self) -> float:
352352
)
353353
return self.seconds
354354

355-
def get_timecode(
356-
self, precision: int = 3, use_rounding: bool = True, nearest_frame: bool = True
357-
) -> str:
355+
def get_timecode(self, precision: int = 3, use_rounding: bool = True) -> str:
358356
"""Get a formatted timecode string of the form HH:MM:SS[.nnn].
359357
360358
Args:
361359
precision: The number of decimal places to include in the output ``[.nnn]``.
362360
use_rounding: Rounds the output to the desired precision. If False, the value
363361
will be truncated to the specified precision.
364-
nearest_frame: Ensures that the timecode is moved to the nearest frame boundary if this
365-
object has a defined framerate, otherwise has no effect.
366362
367363
Returns:
368364
str: The current time in the form ``"HH:MM:SS[.nnn]"``.
369365
"""
370366
# Compute hours and minutes based off of seconds, and update seconds.
371-
if nearest_frame and self.framerate:
372-
secs = self.frame_num / self.framerate
373-
else:
374-
secs = self.seconds
367+
secs = self.seconds
375368
hrs = int(secs / _SECONDS_PER_HOUR)
376369
secs -= hrs * _SECONDS_PER_HOUR
377370
mins = int(secs / _SECONDS_PER_MINUTE)

scenedetect/scene_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ def detect_scenes(
423423
end_time: ty.Optional[FrameTimecode] = None,
424424
frame_skip: int = 0,
425425
show_progress: bool = False,
426-
callback: ty.Optional[ty.Callable[[np.ndarray, int], None]] = None,
426+
callback: ty.Optional[ty.Callable[[np.ndarray, FrameTimecode], None]] = None,
427427
frame_source: ty.Optional[VideoStream] = None,
428428
) -> int:
429429
"""Perform scene detection on the given video using the added SceneDetectors, returning the

tests/test_api.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ def test_api_scene_manager_callback(test_video_file: str):
108108
"""Demonstrate how to use a callback with the SceneManager detect_scenes method."""
109109
import numpy
110110

111-
from scenedetect import ContentDetector, SceneManager, open_video
111+
from scenedetect import ContentDetector, FrameTimecode, SceneManager, open_video
112112

113113
# Callback to invoke on the first frame of every new scene detection.
114-
def on_new_scene(frame_img: numpy.ndarray, frame_num: int):
115-
print("New scene found at frame %d." % frame_num)
114+
def on_new_scene(frame_img: numpy.ndarray, frame_num: FrameTimecode):
115+
print("New scene found at {frame_num}")
116116

117117
video = open_video(test_video_file)
118118
scene_manager = SceneManager()

tests/test_timecode.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,7 @@ def test_get_timecode():
192192
assert FrameTimecode(timecode="00:00:02.0000", fps=1.0).get_timecode() == "00:00:02.000"
193193
assert FrameTimecode(timecode="00:00:00.5", fps=10.0).get_timecode() == "00:00:00.500"
194194
# If a value is provided in seconds, we store that value internally now.
195-
assert (
196-
FrameTimecode(timecode="00:00:01.501", fps=10.0).get_timecode(nearest_frame=False)
197-
== "00:00:01.501"
198-
)
199-
assert (
200-
FrameTimecode(timecode="00:00:01.501", fps=10.0).get_timecode(nearest_frame=True)
201-
== "00:00:01.500"
202-
)
195+
assert FrameTimecode(timecode="00:00:01.501", fps=10.0).get_timecode() == "00:00:01.501"
203196

204197

205198
def test_equality():
@@ -239,6 +232,7 @@ def test_equality():
239232

240233
assert FrameTimecode(timecode="00:00:00.5", fps=10.0) == "00:00:00.500"
241234
assert FrameTimecode(timecode="00:00:01.500", fps=10.0) == "00:00:01.500"
235+
assert FrameTimecode(timecode="999999:59:59.999999", fps=10.0) == "999999:59:59.999999"
242236

243237

244238
def test_addition():

0 commit comments

Comments
 (0)