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()

Create a CodecContext for this codec.

Codec.is_encoder
Codec.is_encoder
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

Flag property of Properties

class av.codec.Properties

Bases: av.enum.EnumFlag

Wraps AVCodecDescriptor.props (AV_CODEC_PROP_*).

Codec Attribute

Properties Name

Flag Value

Meaning in FFmpeg

intra_only

INTRA_ONLY

0x1

Codec uses only intra compression. Video and audio codecs only.

lossy

LOSSY

0x2

Codec supports lossy compression. Audio and video codecs only. Note: A codec may support both lossy and lossless compression modes.

lossless

LOSSLESS

0x4

Codec supports lossless compression. Audio and video codecs only.

reorder

REORDER

0x8

Codec supports frame reordering. That is, the coded order (the order in which the encoded packets are output by the encoders / stored / input to the decoders) may be different from the presentation order of the corresponding frames. For codecs that do not have this property set, PTS and DTS should always be equal.

bitmap_sub

BITMAP_SUB

0x10000

Subtitle codec is bitmap based Decoded AVSubtitle data can be read from the AVSubtitleRect->pict field.

text_sub

TEXT_SUB

0x20000

Subtitle codec is text based. Decoded AVSubtitle data can be read from the AVSubtitleRect->ass field.

Codec.capabilities

Flag property of Capabilities

class av.codec.Capabilities

Bases: av.enum.EnumFlag

Wraps AVCodec.capabilities (AV_CODEC_CAP_*).

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

Codec Attribute

Capabilities Name

Flag Value

Meaning in FFmpeg

draw_horiz_band

DRAW_HORIZ_BAND

0x1

Decoder can use draw_horiz_band callback.

dr1

DR1

0x2

Codec uses get_buffer() for allocating buffers and supports custom allocators. If not set, it might not use get_buffer() at all or use operations that assume the buffer was allocated by avcodec_default_get_buffer.

truncated

TRUNCATED

0x8

-

hwaccel

HWACCEL

0x10

-

delay

DELAY

0x20

Encoder or decoder requires flushing with NULL input at the end in order to give the complete and correct output. NOTE: If this flag is not set, the codec is guaranteed to never be fed with with NULL data. The user can still send NULL data to the public encode or decode function, but libavcodec will not pass it along to the codec unless this flag is set. Decoders: The decoder has a non-zero delay and needs to be fed with avpkt->data=NULL, avpkt->size=0 at the end to get the delayed data until the decoder no longer returns frames. Encoders: The encoder needs to be fed with NULL data at the end of encoding until the encoder no longer returns data. NOTE: For encoders implementing the AVCodec.encode2() function, setting this flag also means that the encoder must set the pts and duration for each output packet. If this flag is not set, the pts and duration will be determined by libavcodec from the input frame.

small_last_frame

SMALL_LAST_FRAME

0x40

Codec can be fed a final frame with a smaller size. This can be used to prevent truncation of the last audio samples.

hwaccel_vdpau

HWACCEL_VDPAU

0x80

-

subframes

SUBFRAMES

0x100

Codec can output multiple frames per AVPacket Normally demuxers return one frame at a time, demuxers which do not do are connected to a parser to split what they return into proper frames. This flag is reserved to the very rare category of codecs which have a bitstream that cannot be split into frames without timeconsuming operations like full decoding. Demuxers carrying such bitstreams thus may return multiple frames in a packet. This has many disadvantages like prohibiting stream copy in many cases thus it should only be considered as a last resort.

experimental

EXPERIMENTAL

0x200

Codec is experimental and is thus avoided in favor of non experimental encoders

channel_conf

CHANNEL_CONF

0x400

Codec should fill in channel configuration and samplerate instead of container

neg_linesizes

NEG_LINESIZES

0x800

-

frame_threads

FRAME_THREADS

0x1000

Codec supports frame-level multithreading

slice_threads

SLICE_THREADS

0x2000

Codec supports slice-based (or partition-based) multithreading.

param_change

PARAM_CHANGE

0x4000

Codec supports changed parameters at any point.

auto_threads

AUTO_THREADS

0x8000

Codec supports avctx->thread_count == 0 (auto).

variable_frame_size

VARIABLE_FRAME_SIZE

0x10000

Audio encoder supports receiving a different number of samples in each call.

avoid_probing

AVOID_PROBING

0x20000

Decoder is not a preferred choice for probing. This indicates that the decoder is not a good choice for probing. It could for example be an expensive to spin up hardware decoder, or it could simply not provide a lot of useful information about the stream. A decoder marked with this flag should only be used as last resort choice for probing.

hardware

HARDWARE

0x40000

Codec is backed by a hardware implementation. Typically used to identify a non-hwaccel hardware decoder. For information about hwaccels, use avcodec_get_hw_config() instead.

hybrid

HYBRID

0x80000

Codec is potentially backed by a hardware implementation, but not necessarily. This is used instead of AV_CODEC_CAP_HARDWARE, if the implementation provides some sort of internal fallback.

encoder_reordered_opaque

ENCODER_REORDERED_OPAQUE

0x100000

This codec takes the reordered_opaque field from input AVFrames and returns it in the corresponding field in AVCodecContext after encoding.

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)
CodecContext.close(bool strict=True)

Attributes

CodecContext.is_open
CodecContext.is_encoder
CodecContext.is_decoder
CodecContext.name
CodecContext.type
CodecContext.profile
CodecContext.time_base
CodecContext.ticks_per_frame
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

One of SkipType.

Wraps ffmpeg: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).

Flags

CodecContext.flags

Flag property of Flags.

class av.codec.context.Flags

Bases: av.enum.EnumFlag

CodecContext Attribute

Flags Name

Flag Value

Meaning in FFmpeg

unaligned

UNALIGNED

0x1

Allow decoders to produce frames with data planes that are not aligned to CPU requirements (e.g. due to cropping).

qscale

QSCALE

0x2

Use fixed qscale.

four_mv

4MV

0x4

4 MV per MB allowed / advanced prediction for H.263.

output_corrupt

OUTPUT_CORRUPT

0x8

Output even those frames that might be corrupted.

qpel

QPEL

0x10

Use qpel MC.

drop_changed

DROPCHANGED

0x20

Don’t output frames whose parameters differ from first decoded frame in stream.

pass1

PASS1

0x200

Use internal 2pass ratecontrol in first pass mode.

pass2

PASS2

0x400

Use internal 2pass ratecontrol in second pass mode.

loop_filter

LOOP_FILTER

0x800

loop filter.

gray

GRAY

0x2000

Only decode/encode grayscale.

psnr

PSNR

0x8000

error[?] variables will be set during encoding.

truncated

TRUNCATED

0x10000

Input bitstream might be truncated at a random location instead of only at frame boundaries.

interlaced_dct

INTERLACED_DCT

0x40000

Use interlaced DCT.

low_delay

LOW_DELAY

0x80000

Force low delay.

global_header

GLOBAL_HEADER

0x400000

Place global headers in extradata instead of every keyframe.

bitexact

BITEXACT

0x800000

Use only bitexact stuff (except (I)DCT).

ac_pred

AC_PRED

0x1000000

H.263 advanced intra coding / MPEG-4 AC prediction

interlaced_me

INTERLACED_ME

0x20000000

Interlaced motion estimation

closed_gop

CLOSED_GOP

0x-80000000

-

CodecContext.flags2

Flag property of Flags2.

class av.codec.context.Flags2

Bases: av.enum.EnumFlag

CodecContext Attribute

Flags2 Name

Flag Value

Meaning in FFmpeg

fast

FAST

0x1

Allow non spec compliant speedup tricks.

no_output

NO_OUTPUT

0x4

Skip bitstream encoding.

local_header

LOCAL_HEADER

0x8

Place global headers at every keyframe instead of in extradata.

drop_frame_timecode

DROP_FRAME_TIMECODE

0x2000

Timecode is in drop frame format. DEPRECATED!!!!

chunks

CHUNKS

0x8000

Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries.

ignore_crop

IGNORE_CROP

0x10000

Discard cropping information from SPS.

show_all

SHOW_ALL

0x400000

Show all frames before the first keyframe

export_mvs

EXPORT_MVS

0x10000000

Export motion vectors through frame side data

skip_manual

SKIP_MANUAL

0x20000000

Do not skip samples and export skip information as frame side data

ro_flush_noop

RO_FLUSH_NOOP

0x40000000

Do not reset ASS ReadOrder field on flush (subtitles decoding)

Enums

class av.codec.context.ThreadType

Bases: av.enum.EnumFlag

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

Either method.

class av.codec.context.SkipType

Bases: av.enum.EnumItem

SkipType Name

Flag Value

Meaning in FFmpeg

NONE

0x-10

Discard nothing

DEFAULT

0x0

Discard useless packets like 0 size packets in AVI

NONREF

0x8

Discard all non reference

BIDIR

0x10

Discard all bidirectional frames

NONINTRA

0x18

Discard all non intra frames

NONKEY

0x20

Discard all frames except keyframes

ALL

0x30

Discard all