Filters

Split

Split is used when you need to reuse same stream as an input for multiple filters or codecs.

Note

Split should not be used if desired stream is connected directly to codecs and is not used elsewhere in filter graph.

class fffw.encoding.filters.Split(kind: StreamType, output_count: int = 2)

Audio or video split filter.

Splits audio or video stream to multiple output streams (2 by default). Unlike ffmpeg split filter this one does not allow passing multiple inputs.

Parameters:
  • kind – stream type.

  • output_count – number of output streams.

Example

split = ffmpeg.video | Split(VIDEO, output_count=3)

split | scale1
split | scale2
split | scale3

Concat

Concat combines two or more streams together, one after another. Resulting order is FIFO.

class fffw.encoding.filters.Concat(kind: StreamType, input_count: int = 2)

Concatenates multiple input stream to a single one.

Parameters:
  • kind – stream kind (for proper concatenated streams definitions).

  • input_count – number of input streams.

Example

concat = Concat(VIDEO, input_count=3)

preroll | concat
source | concat
postroll | concat

Overlay

Overlay combines two video streams together, one above another. Most common use case is adding a logo to a video stream. First input is “bottom layer”, second one is “top layer”. Top layer position can be adjusted by x and y arguments.

class fffw.encoding.filters.Overlay(x: int, y: int)

Combines two video streams one on top of another.

Parameters:
  • x – horizontal position of top image in bottom one.

  • y – vertical position of top image in bottom one.

Example

overlay = source | Overlay(x=100, y=100)
logo | overlay

Scale

Scale changes video dimensions. It has more capabilities like changing pixel format but that is not covered by filter implementation (please, inherit).

class fffw.encoding.filters.Scale(width: int | None = None, height: int | None = None)

Video scaling filter.

Parameters:
  • width – resulting video width

  • height – resulting video height

Example

scale = source | Scale(width=1920, height=1080)

Trim

Trim leaves only frames that match start - end interval.

Note

Without SetPTS filter trim does not adjust first frame timestamp, so resulting stream will have duration equal to Trim.end value, while first Trim.start seconds first frame will be shown frozen.

class fffw.encoding.filters.Trim(kind: StreamType, start: int | float | str | TS, end: int | float | str | TS)

Cuts the input so that the output contains one continuous subpart of the input.

Parameters:
  • kind – stream kind (for proper concatenated streams definitions).

  • start – start time of trimmed output

  • end – end time of trimmed output

Example

cut = source | Trim(VIDEO, start=60.0, end=120.0) | SetPTS(VIDEO)

SetPTS

SetPTS adjusts frame presentation timestamps.

Note

The only supported mode that transforms metadata correctly is PTS-STARTPTS, which aligns first frame to zero timestamp.

class fffw.encoding.filters.SetPTS(kind: StreamType, expr: str = 'PTS-STARTPTS')

Change the PTS (presentation timestamp) of the input frames.

$ ffmpeg -y -i source.mp4 -vf trim=start=3:end=4,setpts=PTS-STARTPTS -an test.mp4

Supported cases for metadata handling:

  • “PTS-STARTPTS” - resets stream start to zero.

Example

cut = source | Trim(AUDIO, start=60.0, end=120.0) | SetPTS(AUDIO)

Format

Format filter changes video stream pixel format.

class fffw.encoding.filters.Format(format: str | None = None)

Converts pixel format of video stream

Example

hw = source | Format('nv12') | Upload(Device(hardware='cuda', name='foo'))

Upload

Upload send video frames from host memory to gpu card.

class fffw.encoding.filters.Upload(device: Device | None = None)

Uploads a stream to a hardware device