Video¶
Video Streams¶
- class av.video.stream.VideoStream¶
Bases:
Stream- average_rate¶
The average frame rate of this video stream.
This is calculated when the file is opened by looking at the first few frames and averaging their rate.
- Type:
fractions.Fraction | None
- base_rate¶
The base frame rate of this stream.
This is calculated as the lowest framerate at which the timestamps of frames can be represented accurately. See AVStream.r_frame_rate for more.
- Type:
fractions.Fraction | None
- decode(Packet packet: Packet | None = None)¶
Decode a
Packetand return a list ofVideoFrame.- Return type:
See also
This is a passthrough to
CodecContext.decode().
- display_aspect_ratio¶
The guessed display aspect ratio (DAR) of this stream.
This is calculated from
VideoStream.guessed_sample_aspect_ratio().- Type:
fractions.Fraction | None
- encode(VideoFrame frame: VideoFrame | None = None)¶
Encode an
VideoFrameand return a list ofPacket.See also
This is mostly a passthrough to
CodecContext.encode().
- guessed_rate¶
The guessed frame rate of this stream.
This is a wrapper around av_guess_frame_rate, and uses multiple heuristics to decide what is “the” frame rate.
- Type:
fractions.Fraction | None
- sample_aspect_ratio¶
The guessed sample aspect ratio (SAR) of this stream.
This is a wrapper around av_guess_sample_aspect_ratio, and uses multiple heuristics to decide what is “the” sample aspect ratio.
- Type:
fractions.Fraction | None
Video Codecs¶
- class av.video.codeccontext.VideoCodecContext¶
Bases:
CodecContext- bits_per_coded_sample¶
The number of bits per sample in the codedwords. It’s mandatory for this to be set for some formats to decode properly.
Wraps AVCodecContext.bits_per_coded_sample.
- Type:
- color_primaries¶
Describes the RGB/XYZ matrix of the colorspace.
Wraps AVFrame.color_primaries.
- Type:
- color_range¶
Describes the signal range of the colorspace.
Wraps AVFrame.color_range.
- Type:
- color_trc¶
Describes the linearization function (a.k.a. transformation characteristics) of the colorspace.
Wraps AVFrame.color_trc.
- Type:
- colorspace¶
Describes the YUV/RGB transformation matrix of the colorspace.
Wraps AVFrame.colorspace.
- Type:
- display_aspect_ratio¶
- encoded_frame_count¶
- format¶
- framerate¶
The frame rate, in frames per second.
- Type:
- height¶
- qmax¶
The maximum quantiser value of an encoded stream.
Wraps AVCodecContext.qmax.
- Type:
- qmin¶
The minimum quantiser value of an encoded stream.
Wraps AVCodecContext.qmin.
- Type:
- reformatter¶
- sample_aspect_ratio¶
- width¶
Video Formats¶
- class av.video.format.VideoFormat¶
Bases:
object>>> format = VideoFormat('rgb24') >>> format.name 'rgb24'
- bits_per_pixel¶
- chroma_height(luma_height=0)¶
Height of a chroma plane relative to a luma plane.
- Parameters:
luma_height (int) – Height of the luma plane; defaults to
self.height.
- chroma_width(luma_width=0)¶
Width of a chroma plane relative to a luma plane.
- Parameters:
luma_width (int) – Width of the luma plane; defaults to
self.width.
- components¶
- has_palette¶
Pixel format has a palette in data[1], values are indexes in this palette.
- height¶
- is_bayer¶
The pixel format contains Bayer data.
- is_big_endian¶
Pixel format is big-endian.
- is_bit_stream¶
All values of a component are bit-wise packed end to end.
- is_planar¶
At least one pixel component is not in the first data plane.
- is_rgb¶
The pixel format contains RGB-like data (as opposed to YUV/grayscale).
- name¶
Canonical name of the pixel format.
- padded_bits_per_pixel¶
- width¶
- class av.video.format.VideoFormatComponent¶
Bases:
object- bits¶
Number of bits in the component.
- height¶
The height of this component’s plane.
Requires the parent
VideoFormatto have a height.
- index¶
- is_alpha¶
Is this component an alpha channel?
- is_chroma¶
Is this component a chroma channel?
- is_luma¶
Is this component a luma channel?
- plane¶
The index of the plane which contains this component.
- width¶
The width of this component’s plane.
Requires the parent
VideoFormatto have a width.
Video Frames¶
- class av.video.frame.VideoFrame¶
Bases:
FrameA single video frame.
- Parameters:
>>> frame = VideoFrame(1920, 1080, 'rgb24')
Structural¶
- VideoFrame.width¶
Width of the image, in pixels.
- VideoFrame.height¶
Height of the image, in pixels.
- VideoFrame.format¶
The
VideoFormatof the frame.
- VideoFrame.planes¶
A tuple of
VideoPlaneobjects.
Types¶
- VideoFrame.key_frame¶
Is this frame a key frame?
Wraps AVFrame.key_frame.
- VideoFrame.interlaced_frame¶
Is this frame an interlaced or progressive?
- VideoFrame.pict_type¶
Returns an integer that corresponds to the PictureType enum.
Wraps AVFrame.pict_type
- Type:
Conversions¶
- VideoFrame.reformat(width=None, height=None, format=None, src_colorspace=None, dst_colorspace=None, interpolation=None)¶
Create a new
VideoFramewith the given width/height/format/colorspace.See also
VideoReformatter.reformat()for arguments.
- VideoFrame.to_rgb(**kwargs)¶
Get an RGB version of this frame.
Any
**kwargsare passed toVideoReformatter.reformat().>>> frame = VideoFrame(1920, 1080) >>> frame.format.name 'yuv420p' >>> frame.to_rgb().format.name 'rgb24'
- VideoFrame.to_image(**kwargs)¶
Get an RGB
PIL.Imageof this frame.Any
**kwargsare passed toVideoReformatter.reformat().Note
PIL or Pillow must be installed.
- VideoFrame.to_ndarray(channel_last=False, **kwargs)¶
Get a numpy array of this frame.
Any
**kwargsare passed toVideoReformatter.reformat().The array returned is generally of dimension (height, width, channels).
- Parameters:
channel_last (bool) – If True, the shape of array will be (height, width, channels) rather than (channels, height, width) for the “yuv444p” and “yuvj444p” formats.
Note
Numpy must be installed.
Note
For formats which return an array of
uint16,float16orfloat32, the samples will be in the system’s native byte order.Note
For
pal8, an(image, palette)tuple will be returned, with the palette being in ARGB (PyAV will swap bytes if needed).Note
For
gbrpformats, channels are flipped to RGB order.
- static VideoFrame.from_image(img)¶
Construct a frame from a
PIL.Image.
- static VideoFrame.from_ndarray(array, format='rgb24', channel_last=False)¶
Construct a frame from a numpy array.
- Parameters:
channel_last (bool) – If False (default), the shape for the yuv444p and yuvj444p is given by (channels, height, width) rather than (height, width, channels).
Note
For formats which expect an array of
uint16,float16orfloat32, the samples must be in the system’s native byte order.Note
for
pal8, an(image, palette)pair must be passed. palette must have shape (256, 4) and is given in ARGB format (PyAV will swap bytes if needed).Note
for
gbrpformats, channels are assumed to be given in RGB order.
Video Planes¶
Video Reformatters¶
- class av.video.reformatter.VideoReformatter¶
Bases:
objectAn object for reformatting size and pixel format of
VideoFrame.It is most efficient to have a reformatter object for each set of parameters you will use as calling
reformat()will reconfigure the internal object.- reformat(VideoFrame frame, width=None, height=None, format=None, src_colorspace=None, dst_colorspace=None, interpolation=None, src_color_range=None, dst_color_range=None)¶
Create a new
VideoFramewith the given width/height/format/colorspace.Returns the same frame untouched if nothing needs to be done to it.
- Parameters:
width (int) – New width, or
Nonefor the same width.height (int) – New height, or
Nonefor the same height.format (
VideoFormatorstr) – New format, orNonefor the same format.src_colorspace (
Colorspaceorstr) – Current colorspace, orNonefor the frame colorspace.dst_colorspace (
Colorspaceorstr) – Desired colorspace, orNonefor the frame colorspace.interpolation (
Interpolationorstr) – The interpolation method to use, orNoneforBILINEAR.src_color_range (
color rangeorstr) – Current color range, orNonefor theUNSPECIFIED.dst_color_range (
color rangeorstr) – Desired color range, orNonefor theUNSPECIFIED.
Enums¶
- class av.video.reformatter.Interpolation(*values)¶
Bases:
IntEnumWraps the
SWS_*flags.Interpolation Name
Flag Value
Meaning in FFmpeg
FAST_BILINEAR
0x1
Fast bilinear
BILINEAR
0x2
Bilinear
BICUBIC
0x4
Bicubic
X
0x8
Experimental
POINT
0x10
Nearest neighbor / point
AREA
0x20
Area averaging
BICUBLIN
0x40
Luma bicubic / chroma bilinear
GAUSS
0x80
Gaussian
SINC
0x100
Sinc
LANCZOS
0x200
Bicubic spline
- class av.video.reformatter.Colorspace(*values)¶
Bases:
IntEnumWraps the
SWS_CS_*flags. There is a bit of overlap in these names which comes from FFmpeg and backwards compatibility.Colorspace Name
Flag Value
Meaning in FFmpeg
ITU709
0x1
-
FCC
0x4
-
ITU601
0x5
-
ITU624
0x5
-
SMPTE170M
0x5
-
SMPTE240M
0x7
-
DEFAULT
0x5
-