Last modified: Apr 16, 2026 By Alexander Williams
Get Audio Duration in Python: Easy Guide
Working with audio files is common. You often need to know their length. Python makes this task simple. This guide shows you how.
We will use popular libraries. You will learn to extract duration from MP3, WAV, and other formats. Let's get started.
Why You Need Audio Duration
Knowing an audio file's length is useful. It helps in media players, editing software, and data analysis. You can manage playlists or process batches of files.
Python offers several tools for this. We will explore the best ones. Each has its strengths.
Prerequisites and Installation
You need Python installed. Basic knowledge helps. We will install necessary libraries using pip.
Open your terminal or command prompt. Run the following commands.
pip install pydub
pip install librosa
The pydub library is great for high-level operations. The librosa library is perfect for audio analysis. The built-in wave module handles WAV files.
For a broader look at tools, see our Python Audio Libraries: Play, Record, Process guide.
Method 1: Using pydub for Easy Extraction
pydub is very user-friendly. It works with many audio formats. You need FFmpeg installed on your system.
First, import the AudioSegment class. Then, load your audio file. The duration is in milliseconds.
from pydub import AudioSegment
# Load an audio file (supports MP3, WAV, etc.)
audio = AudioSegment.from_file("my_song.mp3")
# Get duration in milliseconds
duration_ms = len(audio)
# Convert to seconds
duration_sec = duration_ms / 1000
print(f"Duration: {duration_sec:.2f} seconds")
print(f"Duration: {duration_ms} milliseconds")
Duration: 192.54 seconds
Duration: 192540 milliseconds
The from_file method detects the format. The len() function returns the length. This method is straightforward.
Method 2: Using librosa for Advanced Analysis
librosa is a powerhouse for music and audio analysis. It gives you the duration directly in seconds.
Use the librosa.get_duration() function. It's efficient for large files.
import librosa
# Load audio file and get its sample rate and data
audio_path = "my_song.wav"
y, sr = librosa.load(audio_path, sr=None) # sr=None preserves original sample rate
# Get the duration in seconds
duration = librosa.get_duration(y=y, sr=sr)
print(f"Audio Duration: {duration:.2f} seconds")
Audio Duration: 192.54 seconds
The librosa.load() function returns the audio time series and sample rate. The get_duration function uses these to calculate length.
This is ideal for machine learning projects. For more foundational knowledge, check out our Python Audio Processing Guide for Beginners.
Method 3: Using the Built-in wave Module
For standard WAV files, use Python's built-in wave module. No extra installation is needed.
Duration is calculated from the number of frames and the frame rate.
import wave
# Open the WAV file in read mode
with wave.open("sound_effect.wav", 'rb') as wav_file:
# Get the number of frames and the frame rate (samples per second)
n_frames = wav_file.getnframes()
frame_rate = wav_file.getframerate()
# Calculate duration: frames / frames per second
duration = n_frames / float(frame_rate)
print(f"WAV File Duration: {duration:.3f} seconds")
WAV File Duration: 5.125 seconds
The getnframes() method gets the total audio frames. The getframerate() method gets the sampling rate. This method is lightweight and fast for WAV files.
Choosing the Right Method
Which method should you use? It depends on your needs.
Use pydub for simplicity and multiple formats. Use librosa if you plan deeper audio analysis. Use the wave module for quick WAV file checks.
All methods are reliable. Your choice depends on your project's scope.
Common Issues and Solutions
You might encounter some problems. Here are quick fixes.
File Not Found Error: Double-check your file path. Use absolute paths if needed.
Codec Not Supported (pydub): Ensure FFmpeg is installed and accessible in your system's PATH.
Long Loading Time (librosa): Use librosa.get_duration(path='file.mp3') to load only the metadata, which is much faster.
Conclusion
Getting audio duration in Python is easy. You learned three effective methods.
Use pydub for general use. Use librosa for analysis. Use the wave module for WAV files.
These skills are a great start for any audio processing project. Now you can manage and analyze your audio files with confidence.