Audio decoding, encoding, and transformation

Updated at:

This topic describes the audio decoding, encoding, format conversion, standardization, resampling, and waveform concatenation operators in the Python DataFrame API.

Limits

Operator list

Category

Operator

Description

Decoding

audio_decode

Decodes DataType.binary(), a URI, or AUDIO_CLIP_REF_TYPE into a float32 PCM AUDIO_WAVEFORM_TYPE. Suitable for standardizing input before resampling, channel conversion, speech detection, segmentation, or recognition. For example, you can convert an MP3, WAV, or FLAC file into sample data that downstream waveform operators can process directly.

audio_encode

Encodes a float32 PCM AUDIO_WAVEFORM_TYPE into WAV, FLAC, AIFF, MP3, or OGG bytes. Suitable for persisting or transferring a waveform after transformation, concatenation, or segmentation, or for passing it to a system that accepts only encoded files. For example, you can save a standardized 16 kHz mono waveform as WAV.

Format conversion

audio_convert_format

Decodes the input and then re-encodes it into new bytes in the target format. Suitable when downstream systems require a specific container or encoding format. For example, you can normalize audio from different sources into formats such as WAV and FLAC.

audio_standardize

Converts channels and then resamples to standardize an audio waveform to the specified sample rate and number of channels. Suitable for normalizing input from different sources before ASR, VAD, or model inference.

audio_resample

Changes the sample rate of an audio waveform by using soxr HQ while retaining the original number of channels. Suitable when downstream systems require a specific sample rate but the original channel layout must be preserved. For example, you can convert a 48 kHz stereo recording to 16 kHz stereo.

audio_concat

Concatenates multiple audio waveforms that have the same sample rate, number of channels, and sample format, in array order. Suitable for reassembling segmented clips or merging the results of segmented processing. For example, you can restore consecutive clips that were denoised in parallel into a single audio stream.

Common runtime parameters

Operator signatures retain the runtime parameters actually supported by each operator. To avoid repetition, operator parameter tables describe only business parameters. Runtime parameters are described below.

Parameter

Type

Default

Applies to

Description

concurrency

Optional[int]

None

All operators on this page

The UDF concurrency. None uses the framework default.

Audio decoding and encoding

audio_decode

Decodes encoded audio or an audio segment reference into a float32 PCM waveform.

Input type: DataType.binary() (encoded audio bytes column), DataType.string() (audio URI column), or AUDIO_CLIP_REF_TYPE (audio segment reference column).

Signature:

audio_decode(
    *columns,
    on_error="raise",
    max_decoded_bytes=1024 * 1024 * 1024,
    concurrency=None
)

Parameter

Type

Default

Description

on_error

str

"raise"

"raise" throws decoding exceptions. "null" returns null for supported media inputs that fail to decode. The default value in Python differs from SQL.

max_decoded_bytes

int

1073741824

The maximum number of decoded bytes for a single row. The value must be greater than 0 and cannot exceed 2147483647.

A null input returns None. Inaccessible URIs, unsupported input types, and invalid segment references still throw exceptions.

Return type: AUDIO_WAVEFORM_TYPE.

from pyflink.dataframe import col
from pyflink.multimodal.operators import audio_decode

result = df.with_column(
    "waveform",
    audio_decode(
        col("audio_bytes"),
        on_error="null",
        max_decoded_bytes=512 * 1024 * 1024
    )
)

audio_encode

Encodes an audio waveform into bytes in the specified format.

Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.

Signature:

audio_encode(
    *columns,
    format="wav",
    concurrency=None
)

Parameter

Type

Default

Description

format

str

"wav"

The target audio format.

The supported formats, aliases, and default encoding subtypes are as follows:

Format

Available names

Default encoding subtype

WAV

"wav", "wave"

PCM_16

FLAC

"flac"

PCM_16

OGG

"ogg", "oga"

VORBIS

AIFF

"aiff", "aif"

PCM_16

MP3

"mp3", "mpeg"

MPEG_LAYER_III

If the current libsndfile does not support the target format or default encoding subtype, the operator throws an exception.

A null input returns None. An exception is thrown if the input is not valid audio, or if the waveform contains NaN or Inf values.

Return type: DataType.binary().

from pyflink.multimodal.operators import audio_encode

result = df.with_column(
    "wav_bytes",
    audio_encode(
        col("waveform"),
        format="wav"
    )
)

audio_convert_format

Converts encoded audio into another encoding format. The operator decodes the audio first and then re-encodes it in the target format.

Input type: DataType.binary() or DataType.string(), an encoded audio bytes column or an audio URI column.

Signature:

audio_convert_format(
    *columns,
    format="wav",
    on_error="raise",
    max_decoded_bytes=1024 * 1024 * 1024,
    concurrency=None
)

Parameter

Type

Default

Description

format

str

"wav"

The target audio format.

on_error

str

"raise"

"raise" throws decoding exceptions. "null" returns null when a supported input fails to decode. The default value in Python differs from SQL.

max_decoded_bytes

int

1073741824

The maximum number of intermediate PCM bytes allowed during the conversion.

URI access errors, encoding errors, unsupported formats, and unavailable target encoding subtypes still fail the job. A null input returns None.

Return type: DataType.binary().

from pyflink.multimodal.operators import audio_convert_format

result = df.with_column(
    "wav_bytes",
    audio_convert_format(
        col("audio_uri"),
        format="wav",
        on_error="null"
    )
)

Audio waveform transformation

audio_standardize

Standardizes an audio waveform to the specified sample rate and number of channels.

Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.

Signature:

audio_standardize(
    *columns,
    sample_rate=16000,
    channels=1,
    concurrency=None
)

Parameter

Type

Default

Description

sample_rate

int

16000

The target sample rate, in Hz. The value must be greater than 0.

channels

int

1

The target number of channels. The value must be greater than 0.

The operator converts channels first and then resamples by using soxr HQ. When converting multi-channel audio to mono, the channels are averaged. When converting mono to multi-channel, the mono channel is duplicated. When converting between different multi-channel counts, the audio is first mixed down to mono and then duplicated to the target number of channels. The output retains the source URI and clip time fields.

Return type: AUDIO_WAVEFORM_TYPE.

from pyflink.multimodal.operators import audio_standardize

result = df.with_column(
    "speech_waveform",
    audio_standardize(
        col("waveform"),
        sample_rate=16000,
        channels=1
    )
)

audio_resample

Changes only the sample rate of an audio waveform and retains the number of channels.

Input type: AUDIO_WAVEFORM_TYPE, a decoded audio waveform column.

Signature:

audio_resample(
    *columns,
    sample_rate,
    concurrency=None
)

Parameter

Type

Default

Description

sample_rate

int

Required

The target sample rate, in Hz. The value must be greater than 0.

The operator resamples by using soxr HQ and retains the number of channels, the source URI, and the clip time fields. The number of output frames is:

ceil(input_frames * target_sample_rate / input_sample_rate)

The operator truncates or pads the result if necessary to match this frame count. If the target sample rate equals the input sample rate, the operator returns the original waveform after input validation.

Return type: AUDIO_WAVEFORM_TYPE.

from pyflink.multimodal.operators import audio_resample

result = df.with_column(
    "resampled",
    audio_resample(
        col("waveform"),
        sample_rate=8000,
    ),
)

audio_concat

Concatenates multiple audio waveforms in array order.

Input type: DataType.list(AUDIO_WAVEFORM_TYPE), a non-empty array column of audio waveforms.

Signature:

audio_concat(
    *columns,
    concurrency=None,
)
  • If the input array is None, the operator returns None. An empty array throws an exception.

  • All waveforms must have the same sample rate, number of channels, and sample format, and must meet the float32 interleaved waveform constraints.

  • When there is only one input, its source URI and start and end times are retained as is, including cases where only some source fields are set.

  • For multiple inputs, the merged source and start and end times are retained only when the source_uri values are all identical, every segment has complete start and end times, and the start of each subsequent segment equals the end of the previous one. Otherwise, all three source fields are set to None.

Return type: AUDIO_WAVEFORM_TYPE.

from pyflink.multimodal.operators import audio_concat

result = df.with_column(
    "joined",
    audio_concat(col("clips")),
)