AudioFile
AudioFile extends File and provides additional methods for working with audio files.
AudioFile instances are created when a DataChain is initialized from storage with the type="audio" parameter:
There are additional models for working with audio files:
AudioFragment- represents a fragment of an audio file.
These are virtual models that do not create physical files.
Instead, they are used to represent the data in the AudioFile these models are referring to.
If you need to save the data, you can use the save method of these models,
allowing you to save data locally or upload it to a storage service.
For a complete example of audio processing with DataChain, see
Audio-to-Text with Whisper -
a speech recognition pipeline that uses AudioFile, AudioFragment, and Audio
to chunk audio files and transcribe them.
AudioFile
Bases: File
A data model for handling audio files.
This model inherits from the File model and provides additional functionality
for reading audio files, extracting audio fragments, and splitting audio into
fragments.
Source code in datachain/lib/file.py
get_fragment
get_fragment(start: float, end: float) -> AudioFragment
Returns an audio fragment from the specified time range. It does not download the file, neither it actually extracts the fragment. It returns a Model representing the audio fragment, which can be used to read or save it later.
Parameters:
-
start(float) βThe start time of the fragment in seconds.
-
end(float) βThe end time of the fragment in seconds.
Returns:
-
AudioFragment(AudioFragment) βA Model representing the audio fragment.
Source code in datachain/lib/file.py
get_fragments
get_fragments(
duration: float,
start: float = 0,
end: float | None = None,
) -> Iterator[AudioFragment]
Splits the audio into multiple fragments of a specified duration.
Parameters:
-
duration(float) βThe duration of each audio fragment in seconds.
-
start(float, default:0) βThe starting time in seconds (default: 0).
-
end(float, default:None) βThe ending time in seconds. If None, the entire remaining audio is processed (default: None).
Returns:
-
Iterator[AudioFragment]βIterator[AudioFragment]: An iterator yielding audio fragments.
Note
If end is not specified, number of samples will be taken from the audio file, this means audio metadata needs to be read.
Source code in datachain/lib/file.py
get_info
get_info() -> Audio
Retrieves metadata and information about the audio file.
Metadata is read through File.open(), so it can stream when caching
is disabled. When caching is enabled, opening the file may populate the
local cache first. For UDFs that only need audio metadata, it can be
useful to disable caching and prefetching.
Returns:
-
Audio(Audio) βA Model containing audio metadata such as duration, sample rate, channels, and codec details.
Source code in datachain/lib/file.py
save
save(
destination: str,
format: str | None = None,
start: float = 0,
end: float | None = None,
client_config: dict | None = None,
*,
content_type: str | None = None,
content_disposition: str | None = None,
cache_control: str | None = None,
content_encoding: str | None = None,
metadata: dict[str, str] | None = None,
write_options: dict[str, Any] | None = None
) -> AudioFile
Save audio file or extract fragment to specified format.
If destination is a remote path, the audio file will be uploaded
to remote storage.
Parameters:
-
destination(str) βOutput directory path or URI (e.g.
s3://β¦,gs://β¦). -
format(str | None, default:None) βOutput format ('wav', 'mp3', etc). Defaults to source format.
-
start(float, default:0) βStart time in seconds (>= 0). Defaults to 0.
-
end(float | None, default:None) βEnd time in seconds. If None, extracts to end of file.
-
client_config(dict | None, default:None) βOptional client configuration.
-
content_type(str | None, default:None) βContent-Typeto set on the written object. -
content_disposition(str | None, default:None) βContent-Dispositionto set on the object. -
cache_control(str | None, default:None) βCache-Controlto set on the object. -
content_encoding(str | None, default:None) βContent-Encodingto set on the object. -
metadata(dict[str, str] | None, default:None) βCustom key/value metadata to attach to the object.
-
write_options(dict[str, Any] | None, default:None) βRaw, backend-native write kwargs merged after the normalized fields above.
Returns:
-
AudioFile(AudioFile) βNew audio file with format conversion/extraction applied.
Examples:
audio.save("/path", "mp3") # Entire file to MP3 audio.save("s3://bucket/path", "wav", start=2.5) # From 2.5s to end as WAV audio.save("/path", "flac", start=1, end=3) # 1-3s fragment as FLAC
Source code in datachain/lib/file.py
1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 | |
AudioFragment
Bases: DataModel
A data model for representing an audio fragment.
This model represents a specific fragment within an audio file with defined start and end times. It allows access to individual fragments and provides functionality for reading and saving audio fragments as separate audio files.
Attributes:
-
audio(AudioFile) βThe audio file containing the audio fragment.
-
start(float) βThe starting time of the audio fragment in seconds.
-
end(float) βThe ending time of the audio fragment in seconds.
get_np
Returns the audio fragment as a NumPy array with sample rate.
Returns:
-
tuple[ndarray, int]βtuple[ndarray, int]: A tuple containing the audio data as a NumPy array and the sample rate.
Source code in datachain/lib/file.py
read_bytes
Returns the audio fragment as audio bytes.
Parameters:
-
format(str, default:'wav') βThe desired audio format (e.g., 'wav', 'mp3'). Defaults to 'wav'.
Returns:
-
bytes(bytes) βThe encoded audio fragment as bytes.
Source code in datachain/lib/file.py
save
save(
destination: str,
format: str | None = None,
client_config: dict | None = None,
*,
content_type: str | None = None,
content_disposition: str | None = None,
cache_control: str | None = None,
content_encoding: str | None = None,
metadata: dict[str, str] | None = None,
write_options: dict[str, Any] | None = None
) -> AudioFile
Saves the audio fragment as a new audio file.
If destination is a remote path, the audio file will be uploaded
to remote storage.
Parameters:
-
destination(str) βOutput directory path or URI (e.g.
s3://β¦,gs://β¦). -
format(str | None, default:None) βOutput audio format (e.g., 'wav', 'mp3'). If None, inferred from the file extension.
-
client_config(dict | None, default:None) βOptional client configuration (e.g. credentials).
-
content_type(str | None, default:None) βContent-Typeto set on the written object. -
content_disposition(str | None, default:None) βContent-Dispositionto set on the object. -
cache_control(str | None, default:None) βCache-Controlto set on the object. -
content_encoding(str | None, default:None) βContent-Encodingto set on the object. -
metadata(dict[str, str] | None, default:None) βCustom key/value metadata to attach to the object.
-
write_options(dict[str, Any] | None, default:None) βRaw, backend-native write kwargs merged after the normalized fields above.
Returns:
-
AudioFile(AudioFile) βA Model representing the saved audio file.
Source code in datachain/lib/file.py
Audio
Bases: DataModel
A data model representing metadata for an audio file.
Attributes:
-
sample_rate(int) βThe sample rate of the audio (samples per second). Defaults to -1 if unknown.
-
channels(int) βThe number of audio channels. Defaults to -1 if unknown.
-
duration(float) βThe total duration of the audio in seconds. Defaults to -1.0 if unknown.
-
samples(int) βThe total number of samples in the audio. Defaults to -1 if unknown.
-
format(str) βThe format of the audio file (e.g., 'wav', 'mp3'). Defaults to an empty string.
-
codec(str) βThe codec used for encoding the audio. Defaults to an empty string.
-
bit_rate(int) βThe bit rate of the audio in bits per second. Defaults to -1 if unknown.
get_channel_name
staticmethod
Map channel index to meaningful name based on common audio formats