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

gen_pts

GENPTS

0x1

Generate missing pts even if it requires parsing future frames.

ign_idx

IGNIDX

0x2

Ignore index.

non_block

NONBLOCK

0x4

Do not block when reading packets from input.

ign_dts

IGNDTS

0x8

Ignore DTS on frames that contain both DTS & PTS.

no_fill_in

NOFILLIN

0x10

Do not infer any values from other values, just return what is stored in the container.

no_parse

NOPARSE

0x20

Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no parsing -> no frames. Also seeking to frames can not work if parsing to find frame boundaries has been disabled.

no_buffer

NOBUFFER

0x40

Do not buffer frames when possible.

custom_io

CUSTOM_IO

0x80

The caller has supplied a custom AVIOContext, don’t avio_close() it.

discard_corrupt

DISCARD_CORRUPT

0x100

Discard frames marked corrupted.

flush_packets

FLUSH_PACKETS

0x200

Flush the AVIOContext every packet.

bit_exact

BITEXACT

0x400

When muxing, try to avoid writing any random/volatile data to the output. This includes any random IDs, real-time timestamps/dates, muxer version, etc. This flag is mainly intended for testing.

sort_dts

SORT_DTS

0x10000

Try to interleave outputted packets by dts (using this flag can slow demuxing down).

fast_seek

FAST_SEEK

0x80000

Enable fast, but inaccurate seeks for some formats.

shortest

SHORTEST

0x100000

Stop muxing when the shortest stream stops.

auto_bsf

AUTO_BSF

0x200000

Add bitstream filters as requested by the muxer.

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

Create a new stream, and return it.

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

  • rate – The frame rate for video, and sample rate for audio. Examples for video include 24, 23.976, and Fraction(30000,1001). Examples for audio include 48000 and 44100.

  • template – Copy codec from another Stream instance.

  • options (dict) – Stream options.

  • **kwargs – Set attributes of the stream.

Returns:

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
class av.format.Flags

Bases: EnumFlag

ContainerFormat Attribute

Flags Name

Flag Value

Meaning in FFmpeg

no_file

NOFILE

0x1

-

need_number

NEEDNUMBER

0x2

Needs ‘%d’ in filename.

show_ids

SHOW_IDS

0x8

Show format stream IDs numbers.

global_header

GLOBALHEADER

0x40

Format wants global header.

no_timestamps

NOTIMESTAMPS

0x80

Format does not need / have any timestamps.

generic_index

GENERIC_INDEX

0x100

Use generic index building code.

ts_discont

TS_DISCONT

0x200

Format allows timestamp discontinuities. Note, muxers always require valid (monotone) timestamps

variable_fps

VARIABLE_FPS

0x400

Format allows variable fps.

no_dimensions

NODIMENSIONS

0x800

Format does not need width/height

no_streams

NOSTREAMS

0x1000

Format does not require any streams

no_bin_search

NOBINSEARCH

0x2000

Format does not allow to fall back on binary search via read_timestamp

no_gen_search

NOGENSEARCH

0x4000

Format does not allow to fall back on generic search

no_byte_seek

NO_BYTE_SEEK

0x8000

Format does not allow seeking by bytes

allow_flush

ALLOW_FLUSH

0x10000

Format allows flushing. If not set, the muxer will not receive a NULL packet in the write_packet function.

ts_nonstrict

TS_NONSTRICT

0x20000

Format does not require strictly increasing timestamps, but they must still be monotonic.

ts_negative

TS_NEGATIVE

0x40000

Format allows muxing negative timestamps. If not set the timestamp will be shifted in av_write_frame and av_interleaved_write_frame so they start from 0. The user or muxer can override this through AVFormatContext.avoid_negative_ts

seek_to_pts

SEEK_TO_PTS

0x4000000

Seeking is based on PTS