Skip to content

Commit

Permalink
[frame_timecode] Fix UnboundedLocalError for strings with too many ":…
Browse files Browse the repository at this point in the history
…" characters

Fixes #476
  • Loading branch information
Breakthrough committed Jan 21, 2025
1 parent 58c6c15 commit 22ff352
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions scenedetect/frame_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ def _parse_timecode_string(self, input: str) -> int:
# Timecode in string format 'HH:MM:SS[.nnn]' or 'MM:SS[.nnn]'
elif input.find(":") >= 0:
values = input.split(":")
if len(values) not in (2, 3):
raise ValueError("Invalid timecode (too many separators).")
# Case of 'HH:MM:SS[.nnn]'
if len(values) == 3:
hrs, mins = int(values[0]), int(values[1])
Expand Down
11 changes: 11 additions & 0 deletions tests/test_frame_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ def test_timecode_string():
assert FrameTimecode(timecode="00:00:02.0000", fps=1).frame_num == 2
assert FrameTimecode(timecode="00:00:02.0001", fps=1).frame_num == 2

# MM:SS[.nnn] is also allowed
assert FrameTimecode(timecode="00:01", fps=1).frame_num == 1
assert FrameTimecode(timecode="00:01.9999", fps=1).frame_num == 2
assert FrameTimecode(timecode="00:02.0000", fps=1).frame_num == 2
assert FrameTimecode(timecode="00:02.0001", fps=1).frame_num == 2

# Conversion edge cases
assert FrameTimecode(timecode="00:00:01", fps=10).frame_num == 10
assert FrameTimecode(timecode="00:00:00.5", fps=10).frame_num == 5
assert FrameTimecode(timecode="00:00:00.100", fps=10).frame_num == 1
Expand All @@ -135,6 +142,10 @@ def test_timecode_string():
assert FrameTimecode(timecode="01:00:00.000", fps=1).frame_num == 3600
assert FrameTimecode(timecode="01:00:00.001", fps=1).frame_num == 3600

# Check too many ":" characters (https://github.com/Breakthrough/PySceneDetect/issues/476)
with pytest.raises(ValueError):
FrameTimecode(timecode="01:01:00:00.001", fps=1)


def test_get_frames():
"""Test FrameTimecode get_frames() method."""
Expand Down

0 comments on commit 22ff352

Please sign in to comment.