A Python library for audio processing with FFmpeg, designed for streaming and segment reading
Project description
ffmpeg-audio
A Python library for processing audio/video files using FFmpeg with automatic resampling and channel mixing.
Features
- Streaming audio reading: Stream large audio/video files in chunks without loading everything into memory
- Segment reading: Read specific time segments from audio files in one operation
- Automatic resampling: Automatically resamples audio to 16kHz (fixed)
- Channel mixing: Automatically converts to mono channel
- Format support: Supports all audio/video formats that FFmpeg supports (MP3, WAV, FLAC, Opus, MP4, etc.)
- Time range support: Both streaming and reading support start time and duration parameters
Installation
pip install ffmpeg-audio
Note: This package requires FFmpeg to be installed on your system. Make sure FFmpeg is available in your PATH.
Configuration
Environment Variables
The library supports configuration through environment variables:
-
FFMPEG_STREAM_CHUNK_DURATION_SEC: Default chunk duration (in seconds) for streaming operations. If not set or invalid, defaults to 1200 seconds (20 minutes). The value must be a positive integer. Non-standard values (empty strings, non-numeric strings, negative numbers, or zero) will fall back to the default value. -
FFMPEG_TIMEOUT_MS: Default timeout (in milliseconds) for read operations. If not set or invalid, defaults to 300000 milliseconds (5 minutes). The value must be a positive integer. Non-standard values will fall back to the default value.
Example:
# Set default chunk duration to 5 minutes (300 seconds)
export FFMPEG_STREAM_CHUNK_DURATION_SEC=300
# Use in Python
from ffmpeg_audio import FFmpegAudio
# Will use 300 seconds as default chunk duration
for chunk in FFmpegAudio.stream("audio.mp3"):
# Process chunk
pass
Quick Start
Streaming Audio
from ffmpeg_audio import FFmpegAudio
import numpy as np
# Stream entire audio file in chunks
for chunk in FFmpegAudio.stream("audio.mp3"):
# chunk is a numpy array (float32, range -1.0 ~ 1.0)
# Process chunk here
print(f"Chunk shape: {chunk.shape}, dtype: {chunk.dtype}")
# Stream specific time range (from 10s, duration 5s)
for chunk in FFmpegAudio.stream("audio.mp3", start_ms=10000, duration_ms=5000):
# Process chunk
pass
# Stream with custom chunk size (1 minute chunks)
for chunk in FFmpegAudio.stream("audio.mp3", chunk_duration_sec=60):
# Process chunk
pass
Reading Audio Segments
from ffmpeg_audio import FFmpegAudio
# Read a specific time segment (from 10s to 15s)
audio_data = FFmpegAudio.read(
file_path="audio.mp3",
start_ms=10000, # 10 seconds
duration_ms=5000, # 5 seconds
)
# Read from beginning (5 seconds from start)
audio_data = FFmpegAudio.read(
file_path="audio.mp3",
duration_ms=5000, # 5 seconds from start
)
# Read entire file (no start_ms or duration_ms specified)
audio_data = FFmpegAudio.read(file_path="audio.mp3")
# audio_data is a numpy array (float32, range -1.0 ~ 1.0, 16kHz mono)
print(f"Audio shape: {audio_data.shape}, sample rate: {FFmpegAudio.SAMPLE_RATE} Hz")
API Reference
FFmpegAudio
Main class for processing audio/video files. All methods are static.
Constants:
FFmpegAudio.SAMPLE_RATE = 16000: Output sample rate (Hz)FFmpegAudio.AUDIO_CHANNELS = 1: Output channel count (mono)
FFmpegAudio.stream(file_path, start_ms=None, duration_ms=None, chunk_duration_sec=<default>)
Stream audio file in chunks, yielding numpy arrays.
This method reads audio in chunks to minimize memory usage for large files. Each chunk is a numpy array of float32 samples in the range [-1.0, 1.0]. The generator continues until the file ends or the specified duration is reached.
Parameters:
file_path(str): Path to the audio/video file (supports all FFmpeg formats)start_ms(int, optional): Start position in milliseconds. None means from file beginning. If < 0, will be auto-corrected to None with a warning.duration_ms(int, optional): Total duration to read in milliseconds. None means read until end. If <= 0, will be auto-corrected to None with a warning.chunk_duration_sec(int | None): Duration of each chunk in seconds. Defaults to 1200s (20 minutes) and can be configured viaFFMPEG_STREAM_CHUNK_DURATION_SEC. PassingNone(or omitting the argument) silently uses the default value, while values <= 0 keep triggering the warning-and-reset behavior.
Yields:
np.ndarray: Audio chunk as float32 array with shape(n_samples,). Values are normalized to [-1.0, 1.0] range.
Raises:
TypeError: If parameter types are invalidValueError: Iffile_pathis empty or parameter values are invalid (after auto-correction):start_ms < 0(auto-corrected to None)duration_ms <= 0(auto-corrected to None)
FFmpegNotFoundError: If FFmpeg executable is not found in PATHFileNotFoundError: If the input file does not existPermissionError: If file access is deniedUnsupportedFormatError: If file format is not supported or corruptedFFmpegAudioError: For other FFmpeg processing errors
FFmpegAudio.read(file_path, start_ms=None, duration_ms=None, timeout_ms=<default>)
Read audio data from a file in one operation.
This method reads audio data into memory at once. If both start_ms and duration_ms are None, it reads the entire file. For large files or streaming use cases, consider using stream() instead.
The output format (16kHz mono float32) is optimized for speech processing and energy detection algorithms.
Parameters:
file_path(str): Path to audio/video file (supports all FFmpeg formats)start_ms(int, optional): Start position in milliseconds. None means from beginning. If < 0, will be auto-corrected to None with a warning. If specified butduration_msis None, reads fromstart_msto end of file.duration_ms(int, optional): Segment duration in milliseconds. None means read until end of file. If <= 0, will be auto-corrected to None with a warning. Ifstart_msis provided butduration_msis None, reads fromstart_msto end of file. If both are None, reads the entire file.timeout_ms(int): Maximum processing time in milliseconds. Defaults to 300000ms (5 minutes), configurable viaFFMPEG_TIMEOUT_MSenvironment variable. If <= 0, will be auto-corrected to default with a warning.
Returns:
np.ndarray: Audio data as float32 array with shape(n_samples,)wheren_samplesdepends on the audio duration- dtype: float32
- value range: [-1.0, 1.0]
- sample rate: SAMPLE_RATE (16000 Hz)
Raises:
TypeError: If parameter types are invalidValueError: If parameter values are invalid (after auto-correction):start_ms < 0(auto-corrected to None)duration_ms <= 0(auto-corrected to None)timeout_ms <= 0(auto-corrected to default timeout)
FileNotFoundError: If the input file does not existFFmpegNotFoundError: If FFmpeg executable is not found in PATHFFmpegAudioError: If FFmpeg processing fails or timeout is exceeded
Exceptions
FFmpegNotFoundError
Raised when FFmpeg executable is not found in system PATH.
This exception indicates that FFmpeg is either not installed or not accessible from the current environment. Users should install FFmpeg and ensure it's in PATH.
Attributes:
message: Human-readable error message describing the issue
FFmpegAudioError
General FFmpeg audio processing error.
Raised when FFmpeg fails for reasons other than file not found, permission denied, or unsupported format. Contains process return code and stderr for debugging.
Attributes:
message: Primary error message (required)file_path: Path to the file that caused the error (optional)returncode: FFmpeg process exit code (optional)stderr: FFmpeg stderr output for debugging (optional)
UnsupportedFormatError
Raised when audio file format is unsupported or corrupted.
This exception indicates that FFmpeg cannot decode the file, either because the format is not supported or the file is corrupted/invalid.
Attributes:
message: Primary error message (required)file_path: Path to the file that caused the error (optional)returncode: FFmpeg process exit code (optional)stderr: FFmpeg stderr output for debugging (optional)
Requirements
- Python >= 3.10
- FFmpeg (must be installed separately)
- numpy >= 1.26.4
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ffmpeg_audio-1.0.1.tar.gz.
File metadata
- Download URL: ffmpeg_audio-1.0.1.tar.gz
- Upload date:
- Size: 10.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd2739b33a88b10a9d393ffb9b8f0c41b4196254ba74eea521002d76f3c4625d
|
|
| MD5 |
723fb029e7c2588bfdc78a729ca98d5c
|
|
| BLAKE2b-256 |
acf06008205bb08f97c5174f89061f1c16327bb53ede64de704394ecaf18cfb2
|
File details
Details for the file ffmpeg_audio-1.0.1-py3-none-any.whl.
File metadata
- Download URL: ffmpeg_audio-1.0.1-py3-none-any.whl
- Upload date:
- Size: 11.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d1cbd3a76ecc11511147cac322b985f6ed5893338ab8797257c62d6d0459a83
|
|
| MD5 |
7159bcdcc213071709345e23a065ef33
|
|
| BLAKE2b-256 |
55b6d974c1c5992476051eb5926975730bda6b887dabe7e5f2ed6b8c18ad338a
|