Codecs

Descriptors

class av.codec.Codec(name, mode='r')

Bases: object

Parameters:
  • name (str) – The codec name.

  • mode (str) – 'r' for decoding or 'w' for encoding.

This object exposes information about an available codec, and an avenue to create a CodecContext to encode/decode directly.

>>> codec = Codec('mpeg4', 'r')
>>> codec.name
'mpeg4'
>>> codec.type
'video'
>>> codec.is_encoder
False
Codec.create(kind=None)

Create a CodecContext for this codec.

Parameters:

kind (str) – Gives a hint to static type checkers for what exact CodecContext is used.

Codec.is_decoder
Codec.is_encoder
Codec.descriptor
Codec.name
Codec.long_name
Codec.type

The media type of this codec.

E.g: 'audio', 'video', 'subtitle'.

Codec.id
Codec.frame_rates

A list of supported frame rates (fractions.Fraction), or None.

Codec.audio_rates

A list of supported audio sample rates (int), or None.

Codec.video_formats

A list of supported VideoFormat, or None.

Codec.audio_formats

A list of supported AudioFormat, or None.

Flags

Codec.properties
class av.codec.Properties(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Flag

Wraps AVCodecDescriptor.props (AV_CODEC_PROP_*).

Codec.capabilities

Get the capabilities bitmask of the codec.

This method returns an integer representing the codec capabilities bitmask, which can be used to check specific codec features by performing bitwise operations with the Capabilities enum values.

Example:

from av.codec import Codec, Capabilities

codec = Codec("h264", "w")

# Check if the codec can be fed a final frame with a smaller size.
# This can be used to prevent truncation of the last audio samples.
small_last_frame = bool(codec.capabilities & Capabilities.small_last_frame)
Return type:

int

class av.codec.Capabilities(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

Wraps AVCodec.capabilities (AV_CODEC_CAP_*).

Note that ffmpeg -codecs prefers the properties versions of INTRA_ONLY and LOSSLESS.

Contexts

class av.codec.context.CodecContext

Bases: object

CodecContext.codec
CodecContext.options

options: dict

static CodecContext.create(codec, mode=None)
CodecContext.open(bool strict=True)

Attributes

CodecContext.is_open
CodecContext.is_encoder
CodecContext.is_decoder
CodecContext.name
CodecContext.type
CodecContext.profile
CodecContext.time_base
CodecContext.bit_rate
CodecContext.bit_rate_tolerance
CodecContext.max_bit_rate
CodecContext.thread_count

How many threads to use; 0 means auto.

Wraps AVCodecContext.thread_count.

CodecContext.thread_type

One of ThreadType.

Wraps AVCodecContext.thread_type.

CodecContext.skip_frame

Returns one of the following str literals:

“NONE” Discard nothing “DEFAULT” Discard useless packets like 0 size packets in AVI “NONREF” Discard all non reference “BIDIR” Discard all bidirectional frames “NONINTRA” Discard all non intra frames “NONKEY Discard all frames except keyframes “ALL” Discard all

Wraps AVCodecContext.skip_frame.

CodecContext.extradata
CodecContext.extradata_size

Transcoding

CodecContext.parse(raw_input=None)

Split up a byte stream into list of Packet.

This is only effectively splitting up a byte stream, and does no actual interpretation of the data.

It will return all packets that are fully contained within the given input, and will buffer partial packets until they are complete.

Parameters:

raw_input (ByteSource) – A chunk of a byte-stream to process. Anything that can be turned into a ByteSource is fine. None or empty inputs will flush the parser’s buffers.

Returns:

list of Packet newly available.

CodecContext.encode(Frame frame=None)

Encode a list of Packet from the given Frame.

CodecContext.decode(Packet packet=None)

Decode a list of Frame from the given Packet.

If the packet is None, the buffers will be flushed. This is useful if you do not want the library to automatically re-order frames for you (if they are encoded with a codec that has B-frames).

CodecContext.flush_buffers()

Reset the internal codec state and discard all internal buffers.

Should be called before you start decoding from a new position e.g. when seeking or when switching to a different stream.

Enums and Flags

CodecContext.flags

Get and set the flags bitmask of CodecContext.

Return type:

int

class av.codec.context.Flags(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

CodecContext Attribute

Flags Name

Flag Value

Meaning in FFmpeg

CodecContext.flags2

Get and set the flags2 bitmask of CodecContext.

Return type:

int

class av.codec.context.Flags2(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: IntEnum

CodecContext Attribute

Flags2 Name

Flag Value

Meaning in FFmpeg

class av.codec.context.ThreadType(value, names=<not given>, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Flag

Which multithreading methods to use. Use of FF_THREAD_FRAME will increase decoding delay by one frame per thread, so clients which cannot provide future frames should not use it.

ThreadType Name

Flag Value

Meaning in FFmpeg

NONE

0x0

-

FRAME

0x1

Decode more than one frame at once

SLICE

0x2

Decode more than one part of a single frame at once

AUTO

0x3

Decode using both FRAME and SLICE methods.