Audio

Audio Streams

class av.audio.stream.AudioStream

Bases: Stream

decode(Packet packet=None)

Decode a Packet and return a list of AudioFrame.

Return type:

list[AudioFrame]

See also

This is a passthrough to CodecContext.decode().

encode(AudioFrame frame=None)

Encode an AudioFrame and return a list of Packet.

Return type:

list[Packet]

See also

This is mostly a passthrough to CodecContext.encode().

Audio Context

class av.audio.codeccontext.AudioCodecContext

Bases: CodecContext

format

The audio sample format.

Type:

AudioFormat

frame_size

Number of samples per channel in an audio frame.

Type:

int

layout

The audio channel layout.

Type:

AudioLayout

rate

Another name for sample_rate.

sample_rate

Sample rate of the audio data, in samples per second.

Type:

int

Audio Formats

class av.audio.format.AudioFormat

Bases: object

Descriptor of audio formats.

bits

Number of bits per sample.

>>> SampleFormat('s16p').bits
16
bytes

Number of bytes per sample.

>>> SampleFormat('s16p').bytes
2
container_name

The name of a ContainerFormat which directly accepts this data.

Raises:

ValueError – when planar, since there are no such containers.

is_packed

Is this a planar format?

Strictly opposite of is_planar.

is_planar

Is this a planar format?

Strictly opposite of is_packed.

name

Canonical name of the sample format.

>>> SampleFormat('s16p').name
's16p'
packed

The packed variant of this format.

Is itself when packed:

>>> fmt = Format('s16')
>>> fmt.packed is fmt
True
planar

The planar variant of this format.

Is itself when planar:

>>> from av import AudioFormat as Format
>>> fmt = Format('s16p')
>>> fmt.planar is fmt
True

Audio Layouts

class av.audio.layout.AudioLayout(layout)

Bases: object

channels

A tuple of AudioChannel objects.

Type:

tuple

name

The canonical name of the audio layout.

class av.audio.layout.AudioChannel

Bases: object

description

A human description of the audio channel.

name

The canonical name of the audio channel.

Audio Frames

class av.audio.frame.AudioFrame

Bases: Frame

A frame of audio.

format

The audio sample format.

Type:

AudioFormat

static from_ndarray(array, format='s16', layout='stereo')

Construct a frame from a numpy array.

layout

The audio channel layout.

Type:

AudioLayout

planes

A tuple of AudioPlane.

Type:

tuple

rate

Another name for sample_rate.

sample_rate

Sample rate of the audio data, in samples per second.

Type:

int

samples

Number of audio samples (per channel).

Type:

int

to_ndarray(**kwargs)

Get a numpy array of this frame.

Note

Numpy must be installed.

Audio FIFOs

class av.audio.fifo.AudioFifo

Bases: object

A simple audio sample FIFO (First In First Out) buffer.

write(frame)

Push a frame of samples into the queue.

Parameters:

frame (AudioFrame) – The frame of samples to push.

The FIFO will remember the attributes from the first frame, and use those to populate all output frames.

If there is a pts and time_base and sample_rate, then the FIFO will assert that the incoming timestamps are continuous.

read(samples=0, partial=False)

Read samples from the queue.

Parameters:
  • samples (int) – The number of samples to pull; 0 gets all.

  • partial (bool) – Allow returning less than requested.

Returns:

New AudioFrame or None (if empty).

If the incoming frames had valid a time_base, sample_rate and pts, the returned frames will have accurate timing.

read_many(samples, partial=False)

Read as many frames as we can.

Parameters:
  • samples (int) – How large for the frames to be.

  • partial (bool) – If we should return a partial frame.

Returns:

A list of AudioFrame.

format

The AudioFormat of this FIFO.

layout

The AudioLayout of this FIFO.

pts_per_sample
sample_rate
samples

Number of audio samples (per channel) in the buffer.

samples_read
samples_written

Audio Resamplers

class av.audio.resampler.AudioResampler(format=None, layout=None, rate=None)

Bases: object

Parameters:
  • format (AudioFormat) – The target format, or string that parses to one (e.g. "s16").

  • layout (AudioLayout) – The target layout, or an int/string that parses to one (e.g. "stereo").

  • rate (int) – The target sample rate.

resample(frame)

Convert the sample_rate, channel_layout and/or format of a AudioFrame.

Parameters:

frame (AudioFrame) – The frame to convert or None to flush.

Returns:

A list of AudioFrame in new parameters. If the nothing is to be done return the same frame as a single element list.

format
frame_size
is_passthrough
layout
rate