A simple REST API for synchronizing two audio files using cross-correlation analysis.
- Sync two audio files by finding their time offset using file paths
- Support for multiple audio formats (WAV, MP3, MP4, FLAC, M4A, AAC)
- Modular architecture with separate processing logic
- Dockerized for easy deployment using UV package manager
- Simple REST API interface with JSON payloads
- Automatic file format handling with FFmpeg
audio_sync.py
: Core audio processing module withAudioSyncProcessor
classapp.py
: Thin Flask wrapper providing REST API endpointspyproject.toml
: UV-based dependency management
- Build the Docker image:
docker build -t audio-sync-api .
- Run the container:
docker run -p 5000:5000 audio-sync-api
- The API will be available at
http://localhost:5000
Synchronize two audio files using local file paths.
Request: JSON payload:
{
"reference_file": "/path/to/reference.wav",
"target_file": "/path/to/target.wav",
"output_dir": "/path/to/output" // optional
}
Response:
{
"success": true,
"offset_seconds": -2.345,
"reference_file": "/path/to/reference.wav",
"target_file": "/path/to/target.wav",
"output_file": "/path/to/output/target_synced.wav",
"command": "ffmpeg -i target.wav -ss 2.345 -c copy -y output.wav",
"message": "Files synced successfully. Offset: -2.345s"
}
Get information about an audio file.
Request: JSON payload:
{
"file_path": "/path/to/audio.wav"
}
Response:
{
"file_path": "/path/to/audio.wav",
"duration_seconds": 180.5,
"exists": true,
"format": ".wav"
}
Health check endpoint.
API documentation and info.
# Sync two audio files
curl -X POST \
-H "Content-Type: application/json" \
-d '{"reference_file": "/data/reference.wav", "target_file": "/data/target.wav"}' \
http://localhost:5000/sync
# Get file information
curl -X POST \
-H "Content-Type: application/json" \
-d '{"file_path": "/data/audio.wav"}' \
http://localhost:5000/info
- Install UV (if not already installed):
curl -LsSf https://astral.sh/uv/install.sh | sh
- Install dependencies:
uv sync
- Run the Flask app:
uv run python app.py
You can also use the audio sync functionality directly:
from audio_sync import sync_audio_files, AudioSyncProcessor
# Simple function usage
result = sync_audio_files('/path/to/reference.wav', '/path/to/target.wav')
# Class-based usage
processor = AudioSyncProcessor()
result = processor.sync_files('/path/to/reference.wav', '/path/to/target.wav')
info = processor.get_file_info('/path/to/audio.wav')
- Loads both audio files using librosa
- Resamples to match sample rates if needed
- Performs cross-correlation to find the optimal time offset
- Uses FFmpeg to apply the offset:
- If target file starts later: adds silence at the beginning
- If target file starts earlier: cuts from the beginning
- If files are aligned: copies the file
- WAV
- MP3
- MP4
- FLAC
- M4A
- AAC
- Python 3.9+
- FFmpeg (for audio processing)
- UV package manager