Enumerations and Flags

PyAV provides enumeration and flag classes that are similar to the stdlib enum module that shipped with Python 3.4.

PyAV’s enums are a little more forgiving to preserve backwards compatibility with earlier PyAV patterns. e.g., they can be freely compared to strings or integers for names and values respectively.


class av.enum.EnumItem

Bases: object

Enumerations are when an attribute may only take on a single value at once, and they are represented as integers in the FFmpeg API. We associate names with each value that are easier to operate with.

Consider av.codec.context.SkipType, which is the type of the CodecContext.skip_frame attribute:

>>> fh = av.open(video_path)
>>> cc = fh.streams.video[0].codec_context

>>> # The skip_frame attribute has a name and value:
>>> cc.skip_frame.name
>>> cc.skip_frame.value

>>> # You can compare it to strings and ints:
>>> cc.skip_frame == 'DEFAULT'
>>> cc.skip_frame == 0

>>> # You can assign strings and ints:
>>> cc.skip_frame = 'NONKEY'
>>> cc.skip_frame == 'NONKEY'
>>> cc.skip_frame == 32


class av.enum.EnumFlag

Bases: av.enum.EnumItem

Flags are sets of boolean attributes, which the FFmpeg API represents as individual bits in a larger integer which you manipulate with the bitwise operators. We associate names with each flag that are easier to operate with.

Consider CodecContextFlags, whis is the type of the CodecContext.flags attribute, and the set of boolean properties:

>>> fh = av.open(video_path)
>>> cc = fh.streams.video[0].codec_context

>>> cc.flags

>>> # You can set flags via bitwise operations with the objects, names, or values:
>>> cc.flags |= cc.flags.OUTPUT_CORRUPT
>>> cc.flags |= 'GLOBAL_HEADER'
>>> cc.flags

>>> # You can test flags via bitwise operations with objects, names, or values:
>>> bool(cc.flags & cc.flags.OUTPUT_CORRUPT)
>>> bool(cc.flags & 'QSCALE')

>>> # There are boolean properties for each flag:
>>> cc.output_corrupt
>>> cc.qscale

>>> # You can set them:
>>> cc.qscale = True
>>> cc.flags