audio
pixeltable.functions.audio
Pixeltable UDFs for AudioType.
encode_audio
encode_audio(
audio_data: Array[Float],
*,
input_sample_rate: Int,
format: String,
output_sample_rate: Int | None = None
) -> Audio
Encodes an audio clip represented as an array into a specified audio format.
Parameters:
-
audio_data(Array[Float]) –An array of sampled amplitudes. The accepted array shapes are
(N,)or(1, N)for mono audio or(2, N)for stereo. -
input_sample_rate(Int) –The sample rate of the input audio data.
-
format(String) –The desired output audio format. The supported formats are 'wav', 'mp3', 'flac', and 'mp4'.
-
output_sample_rate(Int | None, default:None) –The desired sample rate for the output audio. Defaults to the input sample rate if unspecified.
Examples:
Add a computed column with encoded FLAC audio files to a table with audio data (as arrays of floats) and sample rates:
t.add_computed_column(
audio_file=encode_audio(
t.audio_data, input_sample_rate=t.sample_rate, format='flac'
)
)
get_metadata
get_metadata(audio: Audio) -> Json
Gets various metadata associated with an audio file and returns it as a dictionary.
Parameters:
-
audio(Audio) –The audio to get metadata for.
Returns:
-
Json–A
dictsuch as the following:{ 'size': 2568827, 'streams': [ { 'type': 'audio', 'frames': 0, 'duration': 2646000, 'metadata': {}, 'time_base': 2.2675736961451248e-05, 'codec_context': { 'name': 'flac', 'profile': None, 'channels': 1, 'codec_tag': '\x00\x00\x00\x00', }, 'duration_seconds': 60.0, } ], 'bit_rate': 342510, 'metadata': {'encoder': 'Lavf61.1.100'}, 'bit_exact': False, }
Examples:
Extract metadata for files in the audio_col column of the table tbl:
>>> tbl.select(tbl.audio_col.get_metadata()).collect()