Timecode

Timecode is good.

class timecode.Timecode(timecode: str | int | Timecode, mode: CountingMode | None = None, rate: int | None = None)

Timecode representing a given frame number and rate

Specifying The Counting Mode

A CountingMode can be set during the creation of a Timecode object by setting the mode parameter.

NonDropFrame and DropFrame counting modes are available in the timecode.modes subpackage.

Timecode will default to NonDropFrame mode, unless otherwise specified.

>>> from timecode import Timecode
>>> from timecode.modes import DropFrame
..
>>> Timecode(86400, mode=DropFrame())
<Timecode 00;48;02;28 @ 30 DF>

The default counting mode can be set program-wide by assigning a CountingMode class to timecode.Timecode.DEFAULT_MODE

Note

Also included in the timecode.modes subpackage is an abstract class CountingMode, which can be subclassed to make your own weird little counting modes. Give it a shot!

See also: Counting Modes

Specifying The Rate

Timecode will default to the DEFAULT_RATE set by the CountingMode. Or, it may be explicitly set with the rate parameter.

>>> from timecode import Timecode
..
>>> Timecode("01:00:00:00", rate=30)
<Timecode 01:00:00:00 @ 30 NDF>

Warning

The CountingMode will validate the specified rate and may throw an exception if the rate is inappropriate. For example, DropFrame only accepts frame rates which are multiples of 30.

Math

So you got all these timecodes goin’, but what do you do with them? Well I guess you can add them together:

>>> # Two Timecodes
>>> Timecode("01:00:01:00") + Timecode("02:03")
<Timecode 01:00:03:03 @ 24 NDF>
..
>>> # A Timecode and some frames
>>> Timecode("59:59:00") + 24
<Timecode 01:00:00:00 @ 24 NDF>

Oh! You can resample() from one kind to another:

>>> from timecode import Timecode
>>> from timecode.modes import DropFrame, NonDropFrame
..
>>> Timecode("00:48:20:12", mode=NonDropFrame()).resample(rate=30)
<Timecode 00:48:20:15 @ 30 NDF>
..
>>> Timecode("00:48:20:15", rate=30, mode=NonDropFrame()).resample(mode=DropFrame())
<Timecode 00;48;23;13 (88) @ 30 DF>

More Info

See timecode.Timecode in the API Documentation.