Containers

Generic

class av.container.Container

Bases: object

options
container_options
stream_options
metadata_encoding
metadata_errors
open_timeout
read_timeout

Flags

av.container.Container.flags
class av.container.Flags

Wraps AVFormatContext.flags.

Container Attribute

Flags Name

Flag Value

Meaning in FFmpeg

Input Containers

class av.container.InputContainer

Bases: Container

bit_rate
close()
decode(streams=None, video=None, audio=None, subtitles=None, data=None)

Yields a series of Frame from the given set of streams:

for frame in container.decode():
    # Do something with `frame`.

See also

StreamContainer.get() for the interpretation of the arguments.

demux(streams=None, video=None, audio=None, subtitles=None, data=None)

Yields a series of Packet from the given set of Stream:

for packet in container.demux():
    # Do something with `packet`, often:
    for frame in packet.decode():
        # Do something with `frame`.

See also

StreamContainer.get() for the interpretation of the arguments.

Note

The last packets are dummy packets that when decoded will flush the buffers.

duration
seek(offset, *, backward=True, any_frame=False, stream=None)

Seek to a (key)frame nearest to the given timestamp.

Parameters:
  • offset (int) – Time to seek to, expressed in``stream.time_base`` if stream is given, otherwise in av.time_base.

  • backward (bool) – If there is not a (key)frame at the given offset, look backwards for it.

  • any_frame (bool) – Seek to any frame, not just a keyframe.

  • stream (Stream) – The stream who’s time_base the offset is in.

  • unsupported_frame_offset (bool) – offset is a frame index instead of a time; not supported by any known format.

  • unsupported_byte_offset (bool) – offset is a byte location in the file; not supported by any known format.

After seeking, packets that you demux should correspond (roughly) to the position you requested.

In most cases, the defaults of backwards = True and any_frame = False are the best course of action, followed by you demuxing/decoding to the position that you want. This is because to properly decode video frames you need to start from the previous keyframe.

See also

avformat_seek_file for discussion of the flags.

size
start_time
start_time_realtime

Start time of the stream in real world time, in microseconds since the Unix epoch, or None if unknown.

Only a few demuxers know this; RTSP computes it from RTCP sender reports, which is what makes it useful for aligning analytics with a camera’s clock.

Wraps AVFormatContext.start_time_realtime.

Output Containers

class av.container.OutputContainer

Bases: Container

add_attachment(str name: str, str mimetype: str, bytes data: bytes)

Create an attachment stream and embed its payload into the container header.

  • Only supported by formats that support attachments (e.g. Matroska).

  • No per-packet muxing is required; attachments are written at header time.

add_data_stream(codec_name=None)

Creates a new data stream and returns it.

Parameters:
  • codec_name (str | None) – Optional name of the data codec (e.g. ‘klv’)

  • options (dict) – Stream options.

Return type:

The new DataStream.

add_mux_stream(codec_name, rate=None)

Creates a new stream for muxing pre-encoded data without creating a CodecContext. Use this when you want to mux packets that were already encoded externally and no encoding/decoding is needed.

Parameters:
  • codec_name (str) – The name of a codec.

  • **kwargs – Set attributes for the stream (e.g. width, height, time_base).

Return type:

The new Stream.

add_stream(codec_name, rate=None, *, hwaccel=None)

Creates a new stream from a codec name and returns it. Supports video, audio, and subtitle streams.

Parameters:
  • codec_name (str) – The name of a codec.

  • options (dict) – Stream options.

  • hwaccel (HWAccel) – Optional settings for hardware-accelerated encoding. Only applies to video streams (e.g. h264_vaapi); software frames passed to encode() are uploaded to the device automatically.

  • **kwargs – Set attributes for the stream.

Return type:

The new Stream.

Warning

Configure every output stream before muxing the first packet. Writing the file header opens all stream codec contexts, after which structural properties such as format, dimensions, layout, and rate cannot change.

add_stream_from_template(Stream template: Stream, bool opaque: bool | None = None, **kwargs)

Creates a new stream from a template. Supports video, audio, subtitle, data and attachment streams.

Parameters:
  • template – Copy codec from another Stream instance.

  • opaque – If True, copy opaque data from the template’s codec context.

  • **kwargs – Set attributes for the stream.

Return type:

The new Stream.

close()
default_audio_codec

Returns the default audio codec this container recommends.

default_subtitle_codec

Returns the default subtitle codec this container recommends.

default_video_codec

Returns the default video codec this container recommends.

mux(packets)
mux_one(Packet packet: Packet)
start_encoding()

Write the file header! Called automatically.

supported_codecs

Returns a set of all codecs this format supports.

Formats

class av.format.ContainerFormat

Bases: object

Descriptor of a container format.

Parameters:
  • name (str) – The name of the format.

  • mode (str) – 'r' or 'w' for input and output formats; defaults to None which will grab either.

ContainerFormat.name
ContainerFormat.long_name
ContainerFormat.input

An input-only view of this format.

ContainerFormat.output

An output-only view of this format.

ContainerFormat.is_input
ContainerFormat.is_output
ContainerFormat.extensions

Flags

ContainerFormat.flags

Get the flags bitmask for the format.

Return type:

int

class av.format.Flags(*values)

Bases: Flag

ContainerFormat Attribute

Flags Name

Flag Value

Meaning in FFmpeg