Skip to content

Commit 22ff352

Browse files
committed
[frame_timecode] Fix UnboundedLocalError for strings with too many ":" characters
Fixes #476
1 parent 58c6c15 commit 22ff352

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

scenedetect/frame_timecode.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ def _parse_timecode_string(self, input: str) -> int:
286286
# Timecode in string format 'HH:MM:SS[.nnn]' or 'MM:SS[.nnn]'
287287
elif input.find(":") >= 0:
288288
values = input.split(":")
289+
if len(values) not in (2, 3):
290+
raise ValueError("Invalid timecode (too many separators).")
289291
# Case of 'HH:MM:SS[.nnn]'
290292
if len(values) == 3:
291293
hrs, mins = int(values[0]), int(values[1])

tests/test_frame_timecode.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ def test_timecode_string():
122122
assert FrameTimecode(timecode="00:00:02.0000", fps=1).frame_num == 2
123123
assert FrameTimecode(timecode="00:00:02.0001", fps=1).frame_num == 2
124124

125+
# MM:SS[.nnn] is also allowed
126+
assert FrameTimecode(timecode="00:01", fps=1).frame_num == 1
127+
assert FrameTimecode(timecode="00:01.9999", fps=1).frame_num == 2
128+
assert FrameTimecode(timecode="00:02.0000", fps=1).frame_num == 2
129+
assert FrameTimecode(timecode="00:02.0001", fps=1).frame_num == 2
130+
131+
# Conversion edge cases
125132
assert FrameTimecode(timecode="00:00:01", fps=10).frame_num == 10
126133
assert FrameTimecode(timecode="00:00:00.5", fps=10).frame_num == 5
127134
assert FrameTimecode(timecode="00:00:00.100", fps=10).frame_num == 1
@@ -135,6 +142,10 @@ def test_timecode_string():
135142
assert FrameTimecode(timecode="01:00:00.000", fps=1).frame_num == 3600
136143
assert FrameTimecode(timecode="01:00:00.001", fps=1).frame_num == 3600
137144

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

139150
def test_get_frames():
140151
"""Test FrameTimecode get_frames() method."""

0 commit comments

Comments
 (0)