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 nearsest 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 becase 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

Output Containers

class av.container.OutputContainer

Bases: Container

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_stream(codec_name, rate=None)

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

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

  • options (dict) – Stream options.

  • **kwargs – Set attributes for the stream.

Return type:

The new Stream.

add_stream_from_template(Stream template, **kwargs)

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

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

  • **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)
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.options
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(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Flag

ContainerFormat Attribute

Flags Name

Flag Value

Meaning in FFmpeg