Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to get the duration of audio in Python?
Getting the duration of audio files is a common task in audio processing applications. Python offers several libraries to accomplish this, each with its own advantages for different use cases.
The audio duration refers to the length of time an audio file plays, typically measured in seconds. This value depends on the audio file's properties like sample rate, number of frames, and channels.
Algorithm
The general process for calculating audio duration involves ?
Import the appropriate audio processing library
Load the audio file
Extract audio properties (sample rate, frames, channels)
Calculate duration using:
duration = frames / sample_rate
Using the wave Library
The wave library is built into Python and works specifically with WAV files ?
import wave
def get_duration_wave(file_path):
with wave.open(file_path, 'r') as audio_file:
frame_rate = audio_file.getframerate()
n_frames = audio_file.getnframes()
duration = n_frames / float(frame_rate)
return duration
# Simulate reading a 10-second WAV file
print("Using wave library:")
print("Frame rate: 44100 Hz")
print("Number of frames: 441000")
duration = 441000 / 44100.0
print(f"Duration: {duration:.2f} seconds")
Using wave library: Frame rate: 44100 Hz Number of frames: 441000 Duration: 10.00 seconds
Using the pydub Library
Pydub supports multiple audio formats and provides a simple interface. Install with pip install pydub ?
from pydub import AudioSegment
def get_duration_pydub(file_path):
audio_file = AudioSegment.from_file(file_path)
duration = audio_file.duration_seconds
return duration
file_path = 'example.wav'
duration = get_duration_pydub(file_path)
print(f"Duration: {duration:.2f} seconds")
Duration: 10.00 seconds
Using the librosa Library
Librosa is powerful for music and audio analysis. Install with pip install librosa ?
import librosa
def get_duration_librosa(file_path):
audio_data, sample_rate = librosa.load(file_path)
duration = librosa.get_duration(y=audio_data, sr=sample_rate)
return duration
file_path = 'example.wav'
duration = get_duration_librosa(file_path)
print(f"Duration: {duration:.2f} seconds")
Duration: 10.00 seconds
Using ffmpeg-python
FFmpeg handles virtually any audio/video format. Install with pip install ffmpeg-python ?
import ffmpeg
def get_duration_ffmpeg(file_path):
probe = ffmpeg.probe(file_path)
stream = next((stream for stream in probe['streams']
if stream['codec_type'] == 'audio'), None)
duration = float(stream['duration'])
return duration
file_path = 'example.wav'
duration = get_duration_ffmpeg(file_path)
print(f"Duration: {duration:.2f} seconds")
Duration: 10.00 seconds
Comparison
| Library | Supported Formats | Installation | Best For |
|---|---|---|---|
wave |
WAV only | Built-in | Simple WAV processing |
pydub |
Many formats | pip install | General audio manipulation |
librosa |
Many formats | pip install | Music/audio analysis |
ffmpeg-python |
All formats | pip install | Professional audio/video |
Conclusion
Use wave for simple WAV files, pydub for general audio tasks, librosa for music analysis, or ffmpeg-python for maximum format support. Each library provides reliable methods to calculate audio duration in Python.
