A Python library for streaming audio/video files using FFmpeg with automatic resampling and channel mixing
Project description
ffmpeg-audio
A Python library for streaming 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
- Automatic resampling: Automatically resamples audio to 16kHz (configurable)
- Channel mixing: Automatically converts to mono channel
- Segment reading: Read specific time segments from audio files
- Format support: Supports all audio/video formats that FFmpeg supports (MP3, WAV, FLAC, Opus, MP4, etc.)
Installation
pip install ffmpeg-audio
Note: This package requires FFmpeg to be installed on your system. Make sure FFmpeg is available in your PATH.
Quick Start
Streaming Audio
from ffmpeg_audio import AudioStreamer
import numpy as np
# Stream audio file in chunks
for chunk in AudioStreamer.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}")
Reading Audio Segments
from ffmpeg_audio import AudioSegmentReader
# Read a specific time segment (from 10s to 15s)
audio_data = AudioSegmentReader.read(
file_path="audio.mp3",
start_ms=10000, # 10 seconds
duration_ms=5000, # 5 seconds
)
# audio_data is a numpy array (float32, range -1.0 ~ 1.0, 16kHz mono)
print(f"Audio shape: {audio_data.shape}, sample rate: {AudioSegmentReader.SAMPLE_RATE} Hz")
API Reference
AudioStreamer
Main class for streaming audio/video files.
AudioStreamer.stream(file_path, chunk_duration_sec=None)
Stream audio file in chunks.
Parameters:
file_path(str): Path to the audio/video filechunk_duration_sec(int, optional): Duration of each chunk in seconds. Defaults to 1200 (20 minutes).
Yields:
np.ndarray: Audio chunk as float32 numpy array (range -1.0 ~ 1.0, 16kHz mono)
Raises:
TypeError: Ifchunk_duration_secis not an int or NoneValueError: Iffile_pathis empty or invalidFFmpegNotFoundError: If FFmpeg is not installed or not in PATHFileNotFoundError: If the audio file does not existPermissionError: If permission is denied accessing the fileUnsupportedFormatError: If the file format is not supported or invalidFFmpegAudioError: If FFmpeg process fails (for other errors)
Constants:
AudioStreamer.SAMPLE_RATE = 16000: Output sample rate (Hz)AudioStreamer.AUDIO_CHANNELS = 1: Output channel count (mono)AudioStreamer.STREAM_CHUNK_DURATION_SEC = 1200: Default chunk duration (seconds)
AudioSegmentReader
Class for reading specific time segments from audio files.
AudioSegmentReader.read(file_path, start_ms, duration_ms)
Read a specific time segment from an audio file.
Parameters:
file_path(str): Path to the audio/video filestart_ms(int): Start time in milliseconds (must be >= 0)duration_ms(int): Duration in milliseconds (must be > 0)
Returns:
np.ndarray: Audio data as float32 numpy array (range -1.0 ~ 1.0, 16kHz mono)
Raises:
FileNotFoundError: If file doesn't existValueError: If time parameters are invalidFFmpegNotFoundError: If FFmpeg is not installed or not in PATHFFmpegAudioError: If FFmpeg processing fails
Constants:
AudioSegmentReader.SAMPLE_RATE = 16000: Output sample rate (Hz)
Exceptions
FFmpegNotFoundError
Raised when FFmpeg is not installed or not available in PATH.
Attributes:
message: Error message
FFmpegAudioError
Raised when FFmpeg audio processing fails.
Attributes:
message: Error messagefile_path: Path to the file that caused the error (optional)returncode: FFmpeg process return code (optional)stderr: FFmpeg standard error output (optional)
UnsupportedFormatError
Raised when the audio file format is not supported or contains invalid data.
Attributes:
message: Error messagefile_path: Path to the file that caused the error (optional)returncode: FFmpeg process return code (optional)stderr: FFmpeg standard error output (optional)
Requirements
- Python >= 3.10
- FFmpeg (must be installed separately)
- numpy >= 1.26.4
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
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-0.1.0.tar.gz.
File metadata
- Download URL: ffmpeg_audio-0.1.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.19
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5f072bd719ad42ec63f98f9d9748d31a08f4f2563a0da8f3c786bb2eacf10356
|
|
| MD5 |
1a97bd76b7b8dc278fb33feb80a591e9
|
|
| BLAKE2b-256 |
567ca80cbebc94c458ee8bc3bd589b864b937a5eee70a248746aae86870c5523
|
File details
Details for the file ffmpeg_audio-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ffmpeg_audio-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.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 |
4aed4ba25f659053c6a085222fe3f792c36d8b840bc251ad86af9db6da54de93
|
|
| MD5 |
d108f71981dc1466eed3bd38d8856fcc
|
|
| BLAKE2b-256 |
6f6f98d482b0bf96b031ecb74048b5fa6c53e7c5ba3d1537498d17b1938692e9
|