Skip to content

Commit

Permalink
Parsing timecodes of form MM:SS[.nnn] (#433)
Browse files Browse the repository at this point in the history
* Updated timecode parsing

* Update docs for time command
  • Loading branch information
wjs018 authored Oct 6, 2024
1 parent 08eb26e commit db952d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
4 changes: 2 additions & 2 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -692,9 +692,9 @@ Options

Set start/end/duration of input video.

Values can be specified as frames (NNNN), seconds (NNNN.NNs), or timecode (HH:MM:SS.nnn). For example, to process only the first minute of a video:
Values can be specified as frames (NNNN), seconds (NNNN.NNs), or timecode (HH:MM:SS.nnn or MM:SS.nnn). For example, to process only the first minute of a video:

``scenedetect -i video.mp4 time --end 00:01:00``
``scenedetect -i video.mp4 time --end 1:00``

``scenedetect -i video.mp4 time --duration 60s``

Expand Down
13 changes: 10 additions & 3 deletions scenedetect/frame_timecode.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,11 +283,18 @@ def _parse_timecode_string(self, input: str) -> int:
if timecode < 0:
raise ValueError("Timecode frame number must be positive.")
return timecode
# Timecode in string format 'HH:MM:SS[.nnn]'
# Timecode in string format 'HH:MM:SS[.nnn]' or 'MM:SS[.nnn]'
elif input.find(":") >= 0:
values = input.split(":")
hrs, mins = int(values[0]), int(values[1])
secs = float(values[2]) if "." in values[2] else int(values[2])
# Case of 'HH:MM:SS[.nnn]'
if len(values) == 3:
hrs, mins = int(values[0]), int(values[1])
secs = float(values[2]) if "." in values[2] else int(values[2])
# Case of 'MM:SS[.nnn]'
elif len(values) == 2:
hrs = 0
mins = int(values[0])
secs = float(values[1]) if "." in values[1] else int(values[1])
if not (hrs >= 0 and mins >= 0 and secs >= 0 and mins < 60 and secs < 60):
raise ValueError("Invalid timecode range (values outside allowed range).")
secs += (hrs * 60 * 60) + (mins * 60)
Expand Down

0 comments on commit db952d4

Please sign in to comment.