Common

scenedetect.common Module

This module contains common types and functions used throughout PySceneDetect.

This includes FrameTimecode which is used as a way for PySceneDetect to store frame-accurate timestamps of each cut. This is done by also specifying the video framerate with the timecode, allowing a frame number to be converted to/from a floating-point number of seconds, or string in the form “HH:MM:SS[.nnn]” where the [.nnn] part is optional.

A FrameTimecode can be created by specifying a timecode (int for number of frames, float for number of seconds, or str in the form “HH:MM:SS” or “HH:MM:SS.nnn”) with a framerate:

frames = FrameTimecode(timecode = 29, fps = 29.97)
seconds_float = FrameTimecode(timecode = 10.0, fps = 10.0)
timecode_str = FrameTimecode(timecode = "00:00:10.000", fps = 10.0)

Arithmetic/comparison operations with FrameTimecode objects is also possible, and the other operand can also be of the above types:

x = FrameTimecode(timecode = "00:01:00.000", fps = 10.0)
# Can add int (frames), float (seconds), or str (timecode).
print(x + 10)
print(x + 10.0)
print(x + "00:10:00")
# Same for all comparison operators.
print((x + 10.0) == "00:01:10.000")

FrameTimecode objects can be added and subtracted, however the current implementation disallows negative values, and will clamp negative results to 0.

Warning

Be careful when subtracting FrameTimecode objects or adding negative amounts of frames/seconds. In the example below, c will be at frame 0 since b > a, but d will be at frame 5:

a = FrameTimecode(5, 10.0)
b = FrameTimecode(10, 10.0)
c = a - b   # b > a, so c == 0
d = b - a
assert(c == 0)
assert(d == 5)
class scenedetect.common.FrameTimecode(timecode=None, fps=None)

Object for frame-based timecodes, using the video framerate to compute back and forth between frame number and seconds/timecode.

A timecode is valid only if it complies with one of the following three types/formats:
  1. Timecode as str in the form “HH:MM:SS[.nnn]” (“01:23:45” or “01:23:45.678”)

  2. Number of seconds as float, or str in form “SSSS.nnnn” (“45.678”)

  3. Exact number of frames as int, or str in form NNNNN (456 or “456”)

Parameters:
  • timecode (int | float | str | Timecode | FrameTimecode) – A frame number (int), number of seconds (float), timecode string in the form ‘HH:MM:SS’ or ‘HH:MM:SS.nnn’, or a Timecode.

  • fps (int | float | str | FrameTimecode) – The framerate or FrameTimecode to use as a time base for all arithmetic.

Raises:
  • TypeError – Thrown if either timecode or fps are unsupported types.

  • ValueError – Thrown when specifying a negative timecode or framerate.

equal_framerate(fps)

Equal Framerate: Determines if the passed framerate is equal to that of this object.

Parameters:

fps – Framerate to compare against within the precision constant defined in this module (see MAX_FPS_DELTA).

Returns:

True if passed fps matches the FrameTimecode object’s framerate, False otherwise.

Return type:

bool

get_framerate()

Get Framerate: Returns the framerate used by the FrameTimecode object.

Returns:

Framerate of the current FrameTimecode object, in frames per second.

Return type:

float

get_frames()

Get the current time/position in number of frames. This is the equivalent of accessing the self.frame_num property (which, along with the specified framerate, forms the base for all of the other time measurement calculations, e.g. the get_seconds() method).

If using to compare a FrameTimecode with a frame number, you can do so directly against the object (e.g. FrameTimecode(10, 10.0) <= 10).

Returns:

The current time in frames (the current frame number).

Return type:

int

get_seconds()

Get the frame’s position in number of seconds.

If using to compare a FrameTimecode with a frame number, you can do so directly against the object (e.g. FrameTimecode(10, 10.0) <= 1.0).

Returns:

The current time/position in seconds.

Return type:

float

get_timecode(precision=3, use_rounding=True)

Get a formatted timecode string of the form HH:MM:SS[.nnn].

Parameters:
  • precision (int) – The number of decimal places to include in the output [.nnn].

  • use_rounding (bool) – Rounds the output to the desired precision. If False, the value will be truncated to the specified precision.

Returns:

The current time in the form "HH:MM:SS[.nnn]".

Return type:

str

class scenedetect.common.Interpolation(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Interpolation method used for image resizing. Based on constants defined in OpenCV.

AREA = 3

Pixel area relation resampling. Provides moire’-free downscaling.

CUBIC = 2

Bicubic interpolation.

LANCZOS4 = 4

Lanczos interpolation over 8x8 neighborhood.

LINEAR = 1

Bilinear interpolation.

NEAREST = 0

Nearest neighbor interpolation.

class scenedetect.common.Timecode(pts, time_base)

Timing information associated with a given frame.

Parameters:
  • pts (int) –

  • time_base (Fraction) –

pts: int

Presentation timestamp of the frame in units of time_base.

time_base: Fraction

The base unit in which pts is measured.

scenedetect.common.CropRegion

Type hint for rectangle of the form X0 Y0 X1 Y1 for cropping frames. Coordinates are relative to source frame without downscaling.

alias of Tuple[int, int, int, int]

scenedetect.common.CutList

Type hint for a list of cuts, where each timecode represents the first frame of a new shot.

alias of List[FrameTimecode]

scenedetect.common.MAX_FPS_DELTA: float = 1e-05

Maximum amount two framerates can differ by for equality testing.

scenedetect.common.SceneList

Type hint for a list of scenes in the form (start time, end time).

alias of List[Tuple[FrameTimecode, FrameTimecode]]

scenedetect.common.TimecodePair

Named type for pairs of timecodes, which typically represents the start/end of a scene.

alias of Tuple[FrameTimecode, FrameTimecode]