Parses a string based on the three possible forms (in timecode format, as an integer number of frames, or floating-point seconds, ending with 's'). Exact frame numbers (int) requires the `framerate` property was set when the timecode was created. Assuming a framerate of 30.0
(self, input: str)
| 472 | return round(seconds * self._rate) |
| 473 | |
| 474 | def _timecode_to_seconds(self, input: str) -> float: |
| 475 | """Parses a string based on the three possible forms (in timecode format, as an integer |
| 476 | number of frames, or floating-point seconds, ending with 's'). Exact frame numbers (int) |
| 477 | requires the `framerate` property was set when the timecode was created. Assuming a |
| 478 | framerate of 30.0 FPS, the strings '00:05:00.000', '00:05:00', '9000', '300s', and |
| 479 | '300.0' are all possible valid values. These values represent periods of time equal to |
| 480 | 5 minutes, 300 seconds, or 9000 frames (at 30 FPS). |
| 481 | |
| 482 | Raises: |
| 483 | ValueError: Value could not be parsed correctly. |
| 484 | """ |
| 485 | assert self._rate is not None and self._rate > MAX_FPS_DELTA |
| 486 | input = input.strip() |
| 487 | # Exact number of frames N |
| 488 | if input.isdigit(): |
| 489 | timecode = int(input) |
| 490 | if timecode < 0: |
| 491 | raise ValueError("Timecode frame number must be positive.") |
| 492 | return timecode / float(self._rate) |
| 493 | # Timecode in string format 'HH:MM:SS[.nnn]' or 'MM:SS[.nnn]' |
| 494 | elif input.find(":") >= 0: |
| 495 | values = input.split(":") |
| 496 | if len(values) not in (2, 3): |
| 497 | raise ValueError("Invalid timecode (too many separators).") |
| 498 | # Case of 'HH:MM:SS[.nnn]' |
| 499 | if len(values) == 3: |
| 500 | hrs, mins = int(values[0]), int(values[1]) |
| 501 | secs = float(values[2]) if "." in values[2] else int(values[2]) |
| 502 | # Case of 'MM:SS[.nnn]' |
| 503 | elif len(values) == 2: |
| 504 | hrs = 0 |
| 505 | mins = int(values[0]) |
| 506 | secs = float(values[1]) if "." in values[1] else int(values[1]) |
| 507 | if not (hrs >= 0 and mins >= 0 and secs >= 0 and mins < 60 and secs < 60): |
| 508 | raise ValueError("Invalid timecode range (values outside allowed range).") |
| 509 | secs += (hrs * 60 * 60) + (mins * 60) |
| 510 | return secs |
| 511 | # Try to parse the number as seconds in the format 1234.5 or 1234s |
| 512 | if input.endswith("s"): |
| 513 | input = input[:-1] |
| 514 | if not input.replace(".", "").isdigit(): |
| 515 | raise ValueError("All characters in timecode seconds string must be digits.") |
| 516 | as_float = float(input) |
| 517 | if as_float < 0.0: |
| 518 | raise ValueError("Timecode seconds value must be positive.") |
| 519 | return as_float |
| 520 | |
| 521 | def _get_other_as_frames(self, other: "TimecodeLike") -> int: |
| 522 | """Get the frame number from `other` for arithmetic operations.""" |
no outgoing calls
no test coverage detected