Object Reference#

Reader#

Reader is the object that demultiplexes the raw stream or file data.

class avio.Reader(str desc)#

Initializes the media reader.

Parameters

desc (str) – Required media description e.g. “sample.mp4”.

duration()#
Returns

The duration of the stream in milliseconds.

Return type

int

start_time()#

Returns the start time of the media, usually zero, but not always.

Returns

The media start time in milliseconds.

Return type

int

bit_rate()#

Returns the bit rate of the stream. This determines the density of the compression, which will affect the accuracy of the uncompressed media. Higher bit rates will render more accurate display at the expense of larger file sizes or higher stream bandwidth.

Returns

The bit rate in bytes

Return type

int

start_from(int start)#

The reader can be instructed to start from a point in the middle of the stream.

Parameters

start (int) – The starting point in milliseconds

end_at(int end)#

The reader can be instructed to stop before the end of the stream.

Parameters

end (int) – The end point in milliseconds

has_video()#
Returns

Whether or not the stream has a video component

Return type

bool

width()#
Returns

The width of the video frames in pixels

Return type

int

height()#
Returns

The height of the video frames in pixels

Return type

int

frame_rate()#

The frame rate of the media is the number of video frames displayed per second. This parameter will affect the motion accuracy of the video. Higher frame rates will appear to move more smoothly. The frame rate is expressed as an AVRational, which is a fraction of two integers.

Returns

The video frame rate

Return type

avio.AVRational

pix_fmt()#

The pixel format of the video, expressed as a constant. See the discussion on pixel formats nearby for more detailed information.

Returns

Pixel Format

Return type

avio.AVPixelFormat

str_pix_fmt()#
Returns

The pixel format in a human readable string

Return type

str

video_codec()#

The codec describes the compression format for the media, expressed as a constant. See the discussion on codecs nearby for more detailed information.

Returns

The codec ID

Return type

avio.AVCodecID

str_video_codec()#
Returns

The codec ID in a human readable string

Return type

str

video_bit_rate()#
Returns

The bit rate of the video component of the multimedia

Return type

int

video_time_base()#

The time base describes a factor that determines the calculation of the presentation time stamp (pts). See the discussion on pts nearby for more detailed information.

Returns

The video time base

Return type

avio.AVRational

has_audio()#
Returns

Whether or not the stream has an audio component

Return type

bool

channels()#
Returns

Number of audio channels, e.g. stereo = 2

Return type

int

sample_rate()#
Returns

The number of samples per second of audio in the stream

Return type

int

frame_size()#
Returns

The number of samples contained in a single frame of audio data

Return type

int

channel_layout()#
Returns

The audio channel layout of the stream, expressed as a constant

Return type

int

str_channel_layout()#
Returns

The audio channel layout in a human readable string

Return type

str

sample_format()#
Returns

The audio sample format, expressed as a constant

Return type

avio.AVSampleFormat

str_sample_format()#
Returns

The audio sample format in a human readable string

Return type

str

audio_codec()#

The codec describes the compression format for the media, expressed as a constant. See the discussion on codecs nearby for more detailed information.

Returns

The codec ID

Return type

avio.AVCodecID

str_audio_codec()#
Returns

The codec ID in a human readable string

Return type

str

audio_bit_rate()#
Returns

The bit rate of the audio component of the multimedia

Return type

int

audio_time_base()#

The time base describes a factor that determines the calculation of the presentation time stamp (pts). See the discussion on pts nearby for more detailed information.

Returns

The audio time base

Return type

avio.AVRational

video_out()#
Returns

The name of the video output queue

Return type

str

audio_out()#
Returns

The name of the audio output queue

Return type

str

set_video_out(str name)#
Parameters

name (str) – The video output queue name

set_audio_out(str name)#
Parameters

name (str) – The audio output queue name

vpq_max_size#

This attribute sets the maximum size of the reader video queue. For local files, the default value of 1 is a setting that produces good results. Streams arriving over the network may require a larger buffer to accomodate structural issues or timing delays in order to preserve the data without dropping frames. The attribute is set by the user.

Type

int

Value

1

apq_max_size#

This attribute sets the maximum size of the reader audio queue. For local files, the default value of 1 is a setting that produces good results. Streams arriving over the network may require a larger buffer to accomodate structural issues or timing delays in order to preserve the data without dropping frames. The attribute is set by the user if needed.

Type

int

Value

1

show_video_pkts#

This attribute may be used to observe the characteristics of video packets in the media. This may be useful when troubleshooting applications. This attribute is set by the user if desired.

Type

bool

Value

False

show_audio_pkts#

This attribute may be used to observe the characteristics of audio packets in the media. This may be useful when troubleshooting applications. This attribute is set by the user if desired.

Type

bool

Value

False

Decoder#

Decoder unpacks the raw data and decompresses it into readable form.

avio.Decoder(avio.Reader reader, avio.AVMediaType media_type, avio.AVHWDeviceType
hw_device_type = AV_HWDEVICE_TYPE_NONE)

Initializes the decoder. A Reader is required to feed packets to the decoder. The user is required to specify the media type for the decoder. An option for a hardware device is available for use if the host machine is equipped with the appropriate hardware and drivers.

Note that the Decoder object is a dual purpose entity that provides functionality for both video and audio streams. The corresponding methods described apply only to the relevant media type being decoded.

Parameters
  • reader (avio.Reader) – Required media reader.

  • media_type (avio.AVMediaType) – Required media type for decoding

  • hw_device_type (avio.AVHWDeviceType) – Optional hardware device type

video_in()#
Returns

The name of the video input queue

Return type

str

audio_in()#
Returns

The name of the audio input queue

Return type

str

video_out()#
Returns

The name of the video output queue

Return type

str

audio_out()#
Returns

The name of the audio output queue

Return type

str

set_video_in(str name)#
Parameters

name (str) – The video input queue name

set_audio_in(str name)#
Parameters

name (str) – The audio input queue name

set_video_out(str name)#
Parameters

name (str) – The video output queue name

set_audio_out(str name)#
Parameters

name (str) – The audio output queue name

show_frames#

This attribute may be used to observe the characteristics of frames produced by the decoder. This may be useful when troubleshooting applications. This attribute is set by the user if desired.

Type

bool

Value

False

Filter#

Filter performs the initial processing on the decompressed data.

class avio.Filter(avio.Decoder decoder, str desc)#

Initializes the Filter. A Decoder is required to feed frames to the filter.

Note that the Filter object is a dual purpose entity that provides functionality for both video and audio streams. The corresponding methods described apply only to the relevant media type being filtered.

Parameters
  • decoder (avio.Decoder) – Required decoder.

  • desc (str) – Required string description of the filter operation

width()#
Returns

The width of the frame in pixels

Return type

int

height()#
Returns

The height of the frame in pixels

Return type

int

pix_fmt()#
Returns

The pixel format of the frame

Return type

avio.AVPixelFormat

sample_rate()#
Returns

The sample rate of the stream

Return type

int

channels()#
Returns

The number of channels of the stream

Return type

int

channel_layout()#
Returns

The channel layout of the stream

Return type

int

sample_format()#
Returns

The sample format of the stream

Return type

avio.AVSampleFormat

frame_size()#
Returns

The frame size the stream

Return type

int

video_in()#
Returns

The name of the video input queue

Return type

str

audio_in()#
Returns

The name of the audio input queue

Return type

str

video_out()#
Returns

The name of the video output queue

Return type

str

audio_out()#
Returns

The name of the audio output queue

Return type

str

set_video_in(str name)#
Parameters

name (str) – The video input queue name

set_audio_in(str name)#
Parameters

name (str) – The audio input queue name

set_video_out(str name)#
Parameters

name (str) – The video output queue name

set_audio_out(str name)#
Parameters

name (str) – The audio output queue name

Display#

Display handles the presentation of the media. It also hosts the python interface.

class avio.Display(avio.Reader reader)#

Initializes the display.

Parameters

reader – Required application reader

sample_rate#
Type

int

channels#
Type

int

channel_layout#
Type

int

frame_size#
Type

int

sample_format#
Type

avio.AVSampleFormat

audio_playback_format#
Type

avio.AVSampleFormat

disable_audio#

This attribute can be used to ignore audio streams. Useful if the audio subsystem is not working for some reason

Type

bool

fix_audio_pop#

During seek operations, the user may experience a pop sound on audio. Using this attribute set to true will reduce that noise.

Type

bool

hud_enabled#

If set to False, the application will not show the HUD

Type

bool

ignore_video_pts#

This attribute will cause the application to ignore time stamps on video frames. Useful for IP cameras

Type

bool

recent_q_size#

The application will maintain a queue of recent video frames, for single step review in reverse time order. Default size is 200

Type

int

prepend_recent_write#

If this attribute is set to True, the application will include the recent queue when writing. Note that audio should be disabled if using this option as the queue does not retain audio frames and the output will be distorted if audio is not disabled.

Type

bool

font_file#

The application will attempt to find this attribute automatically, which will enable the HUD. In the event that does not work, the user may assign the attribute manually.

Type

str

pin_hud(bool pinned)#

Setting with True will force the HUD to display at all times

video_in()#
Returns

The name of the video input queue

Return type

str

audio_in()#
Returns

The name of the audio input queue

Return type

str

video_out()#
Returns

The name of the video output queue

Return type

str

audio_out()#
Returns

The name of the audio output queue

Return type

str

set_video_in(str name)#
Parameters

name (str) – The video input queue name

set_audio_in(str name)#
Parameters

name (str) – The audio input queue name

set_video_out(str name)#
Parameters

name (str) – The video output queue name

set_audio_out(str name)#
Parameters

name (str) – The audio output queue name

Writer#

Writer will compress the data and write it to stream or file.

class avio.Writer(str desc)#

Initializes the writer. A description string for the type of file to be written is required. The string that specifies the file type corresponds to the file extension of the type. Typical cases include “mp4”, “webm”, “mkv”, “mov”, “avi”.

Parameters

desc (str) – Required description.

write_dir#

This attribute can be used to instruct the writer to write to a directory other than the system default video directory. This attribute is set by the user if desired.

Type

str

Value

A str representing the default video directory

filename#

This attribute can be used to set the filename of the writer output. If not set, the system will automatically use a datetime string representing the time when the writing process began.

Type

str

Value”

A str representing the current datetime

show_video_pkts#

This attribute may be used to observe the characteristics of video packets in the media. This may be useful when troubleshooting applications. This attribute is set by the user if desired.

Type

bool

Value

False

show_audio_pkts#

This attribute may be used to observe the characteristics of audio packets in the media. This may be useful when troubleshooting applications. This attribute is set by the user if desired.

Type

bool

Value

False

Encoder#

Encoder is the codec manager for compressing data.

class avio.Encoder(avio.Writer writer, avio.AVMediaType media_type)#

Initializes the Encoder. A Writer is required to manage the encoder and media.

Note that the Encoder object is a dual purpose entity that provides functionality for both video and audio streams. The corresponding methods described apply only to the relevant media type being filtered.

Parameters
  • writer (avio.Writer) – Required writer.

  • media_type (avio.AVMediaType) – The media type handled by the Encoder

pix_fmt#
Type

avio.AVPixelFormat

width#
Type

int

height#
Type

int

video_bit_rate#
Type

int

frame_rate#
Type

int

gop_size#
Type

int

video_time_base#
Type

avio.AVRational

profile#
Type

str

hw_video_codec_name#
Type

str

hw_device_type#
Type

avio.AVHWDeviceType

hw_pix_fmt#
Type

avio.AVPixelFormat

sw_pix_fmt#
Type

avio.AVPixelFormat

sample_fmt#
Type

avio.AVSampleFormat

channel_layout#
Type

int

audio_bit_rate#
Type

int

sample_rate#
Type

int

nb_samples#
Type

int

channels#
Type

int

audio_time_base#
Type

avio.AVRational

set_channel_layout_mono()#
Return type

void

set_channel_layout_stereo()#
Return type

void

frame_q_max_size#

This attribute can be used to set the maximum size of the input queue to the encoder. This is used when the preprend_recent_write flag is set and the encoder causes the video to stutter. A good value to try is to match the value of the recent_q_size.

Type:

int

video_in()#
Returns

The name of the video input queue

Return type

str

audio_in()#
Returns

The name of the audio input queue

Return type

str

video_out()#
Returns

The name of the video output queue

Return type

str

audio_out()#
Returns

The name of the audio output queue

Return type

str

set_video_in(str name)#
Parameters

name (str) – The video input queue name

set_audio_in(str name)#
Parameters

name (str) – The audio input queue name

set_video_out(str name)#
Parameters

name (str) – The video output queue name

set_audio_out(str name)#
Parameters

name (str) – The audio output queue name

Process#

class avio.Process#

Process is the main object of an avio application. It bundles the selected component objects into a threaded framework and launches with the run command. The order in which objects are added is not critical, but it may be helpful to view the system in terms of data flow through the components. Additionally, the process object may be used to trim and merge media files.

add_reader(avio.Reader reader)#
Parameters

reader (avio.Reader) – The application reader, one per application

add_decoder(avio.Decoder decoder)#
Parameters

decoder (avio.Decoder) – Decodes the reader output, one for each media type

add_filter(avio.Filter filter)#
Parameters

filter (avio.Filter) – Filters the decoded frames, one for each decoder (optional)

add_display(avio.Display display)#
Parameters

display (avio.Display) – Displays the video and audio frames, one per application

set_python(avio.Display, str file_path, str python_class)#
Parameters
  • display (avio.Display) – The application display

  • file_path (str) – The name of the python file used to process video frames

  • python_class (str) – The name of the python Class

set_python_init_arg(Display, str arg)#
Parameters

arg (str) – The string used to initialized the python Class

add_encoder(avio.Encoder encoder)#
Parameters

encoder (avio.Encoder) – The encoder for the media, one for each type

add_frame_drain(str frame_q_name)#
Parameters

frame_q_name (str) – The name of the queue to be drained

add_packet_drain(str pkt_q_name)#
Parameters

pkt_q_name (str) – The name of the queue to be drained

run()#

This method takes no arguments

trim(str in_filename, str out_filename, int start, int end)#
Parameters
  • in_filename (str) – The name of the media file to be trimmed

  • out_filename (str) – The name of the output file

  • start (int) – The starting point of the trim, in milliseconds

  • end (int) – The end point of the trim, in milliseconds

merge(str out_filename)#

Note that input file names are assigned to the merge_filenames varaible prior to invocation of this command

Parameters

out_filename (str) – The name of the output file.

merge_filenames(tuple[str] filenames)#
Parameters

filenames (tuple[str]) – The media files to be merged

AVRational#

AVRational is used to define ratios.

class avio.AVRational#

This class is used by ffmpeg to define ratios referenced in media streams, mostly related to time factors. There are several function calls that return this type, and Encoders may require this as an input parameter. It has only two fields, num and den, which are the numerator and denominator of the fraction repsectively. When assigning a value of this type, the variable is instantiated with no arguments. This results in zero values for num and den, which can subsequently be assigned to their desired values. If the decimal equivalent of the number is required, convert num and den to floats and then divide.

num#

The numerator of the ratio fraction

Type

int

Value

0

den#

The denominator of the ratio fraction

Type

int

Value

0

Constants#

Various constants used to define media characteristics.

enum avio.AVMediaType#

AVMEDIA_TYPE_VIDEO

AVMEDIA_TYPE_AUDIO

AVMEDIA_TYPE_UNKNOWN

enum avio.AVPixelFormat#

AV_PIX_FMT_YUV420P

AV_PIX_FMT_BGR24

AV_PIX_FMT_RGB24

AV_PIX_FMT_NV12

AV_PIX_FMT_NV21

AV_PIX_FMT_RGBA

AV_PIX_FMT_BGRA

AV_PIX_FMT_VAAPI

AV_PIX_FMT_CUDA

AV_PIX_FMT_QSV

AV_PIX_FMT_D3D11VA_VLD

AV_PIX_FMT_VDPAU

AV_PIX_FMT_D3D11

AV_PIX_FMT_OPENCL

AV_PIX_FMT_GRAY8

AV_PIX_FMT_NONE

enum avio.AVHWDeviceType#

AV_HWDEVICE_TYPE_CUDA

AV_HWDEVICE_TYPE_VDPAU

AV_HWDEVICE_TYPE_VAAPI

AV_HWDEVICE_TYPE_DXVA2

AV_HWDEVICE_TYPE_QSV

AV_HWDEVICE_TYPE_VIDEOTOOLBOX

AV_HWDEVICE_TYPE_D3D11VA

AV_HWDEVICE_TYPE_DRM

AV_HWDEVICE_TYPE_OPENCL

AV_HWDEVICE_TYPE_MEDIACODEC

AV_HWDEVICE_TYPE_NONE

enum avio.AVSampleFormat#

AV_SAMPLE_FMT_U8

AV_SAMPLE_FMT_S16

AV_SAMPLE_FMT_S32

AV_SAMPLE_FMT_FLT

AV_SAMPLE_FMT_DBL

AV_SAMPLE_FMT_U8P

AV_SAMPLE_FMT_S16P

AV_SAMPLE_FMT_S32P

AV_SAMPLE_FMT_FLTP

AV_SAMPLE_FMT_DBLP

AV_SAMPLE_FMT_S64

AV_SAMPLE_FMT_S64P

AV_SAMPLE_FMT_NB

AV_SAMPLE_FMT_NONE