AudioDecoder¶
- class torchcodec.decoders.AudioDecoder(source: Union[str, Path, RawIOBase, BufferedReader, bytes, Tensor], *, stream_index: Optional[int] = None, sample_rate: Optional[int] = None)[source]¶
A single-stream audio decoder.
This can be used to decode audio from pure audio files (e.g. mp3, wav, etc.), or from videos that contain audio streams (e.g. mp4 videos).
Returned samples are float samples normalized in [-1, 1]
- Parameters:
source (str,
Pathlib.path
, bytes,torch.Tensor
or file-like object) –The source of the video:
If
str
: a local path or a URL to a video or audio file.If
Pathlib.path
: a path to a local video or audio file.If
bytes
object ortorch.Tensor
: the raw encoded audio data.If file-like object: we read video data from the object on demand. The object must expose the methods read(self, size: int) -> bytes and seek(self, offset: int, whence: int) -> bytes. Read more in: Streaming data through file-like support.
stream_index (int, optional) – Specifies which stream in the file to decode samples from. Note that this index is absolute across all media types. If left unspecified, then the best stream is used.
sample_rate (int, optional) – The desired output sample rate of the decoded samples. By default, the samples are returned in their original sample rate.
- Variables:
metadata (AudioStreamMetadata) – Metadata of the audio stream.
stream_index (int) – The stream index that this decoder is retrieving samples from. If a stream index was provided at initialization, this is the same value. If it was left unspecified, this is the best stream.
Examples using
AudioDecoder
:- get_all_samples() AudioSamples [source]¶
Returns all the audio samples from the source.
To decode samples in a specific range, use
get_samples_played_in_range()
.- Returns:
The samples within the file.
- Return type:
- get_samples_played_in_range(start_seconds: float = 0.0, stop_seconds: Optional[float] = None) AudioSamples [source]¶
Returns audio samples in the given range.
Samples are in the half open range [start_seconds, stop_seconds).
To decode all the samples from beginning to end, you can call this method while leaving
start_seconds
andstop_seconds
to their default values, or useget_all_samples()
as a more convenient alias.- Parameters:
- Returns:
The samples within the specified range.
- Return type: