librosa.get_duration
- librosa.get_duration(*, y=None, sr=22050, S=None, n_fft=2048, hop_length=512, center=True, path=None, filename=<DEPRECATED parameter>)[source]
Compute the duration (in seconds) of an audio time series, feature matrix, or filename.
- Parameters:
- ynp.ndarray [shape=(…, n)] or None
audio time series. Multi-channel is supported.
- srnumber > 0 [scalar]
audio sampling rate of
y
- Snp.ndarray [shape=(…, d, t)] or None
STFT matrix, or any STFT-derived matrix (e.g., chromagram or mel spectrogram). Durations calculated from spectrogram inputs are only accurate up to the frame resolution. If high precision is required, it is better to use the audio time series directly.
- n_fftint > 0 [scalar]
FFT window size for
S
- hop_lengthint > 0 [ scalar]
number of audio samples between columns of
S
- centerboolean
If
True
,S[:, t]
is centered aty[t * hop_length]
If
False
, thenS[:, t]
begins aty[t * hop_length]
- pathstr, path, or file-like
If provided, all other parameters are ignored, and the duration is calculated directly from the audio file. Note that this avoids loading the contents into memory, and is therefore useful for querying the duration of long files.
As in
load
, this can also be an integer or open file-handle that can be processed bysoundfile
.- filenameDeprecated
Equivalent to
path
Warning
This parameter has been renamed to
path
in 0.10. Support forfilename=
will be removed in 1.0.
- Returns:
- dfloat >= 0
Duration (in seconds) of the input time series or spectrogram.
- Raises:
- ParameterError
if none of
y
,S
, orpath
are provided.
Notes
get_duration
can be applied to a file (path
), a spectrogram (S
), or audio buffer (y, sr
). Only one of these three options should be provided. If you do provide multiple options (e.g.,path
andS
), thenpath
takes precedence overS
, andS
takes precedence over(y, sr)
.Examples
>>> # Load an example audio file >>> y, sr = librosa.load(librosa.ex('trumpet')) >>> librosa.get_duration(y=y, sr=sr) 5.333378684807256
>>> # Or directly from an audio file >>> librosa.get_duration(filename=librosa.ex('trumpet')) 5.333378684807256
>>> # Or compute duration from an STFT matrix >>> y, sr = librosa.load(librosa.ex('trumpet')) >>> S = librosa.stft(y) >>> librosa.get_duration(S=S, sr=sr) 5.317369614512471
>>> # Or a non-centered STFT matrix >>> S_left = librosa.stft(y, center=False) >>> librosa.get_duration(S=S_left, sr=sr) 5.224489795918367