duration()

audiofile.duration(file, sloppy=False)[source]

Duration in seconds of audio file.

The default behavior (sloppy=False) ensures the duration in seconds matches the one in samples. To achieve this it first decodes files to WAV if needed, e.g. MP4 files. If you have different decoders on different machines, results might differ.

The case sloppy=True returns the duration as reported in the header of the audio file. This is faster, but might still return different results on different machines as it depends on the installed software. If no duration information is provided in the header it will fall back to sloppy=False.

Parameters
  • file (str) – file name of input audio file

  • sloppy – if True report duration as stored in the header

Return type

float

Returns

duration in seconds of audio file

Raises
  • FileNotFoundError – if ffmpeg or mediainfo binary is needed, but cannot be found

  • RuntimeError – if file is missing, broken or format is not supported

Examples

>>> duration("stereo.wav")
1.5