preprocess module handles audio extraction from video files using FFmpeg. It prepares audio tracks for synchronization analysis by converting them to a standard format.
extract_audio_from_videos
Extracts audio tracks from all video files in a directory using FFmpeg. Produces mono WAV files at a specified sample rate.Parameters
Directory containing input video files (
.mp4, .mov)Output directory for extracted WAV files. Created if it doesn’t exist.
Target sample rate in Hz for the output audio files. Common values:
16000- Default, good balance of quality and processing speed44100- CD quality48000- Professional video standard
Returns
No return value. Audio files are written to disk with the same base filename as the video:video1.mp4→video1.wavcamera_A.mov→camera_A.wav
Raises
Raised if FFmpeg is not found in system PATH
Raised if FFmpeg fails to extract audio from a video file
Output Format
All extracted audio files are:- Format: WAV (uncompressed)
- Channels: Mono (1 channel) - stereo tracks are mixed to mono
- Sample rate: As specified by
target_srparameter - Bit depth: 16-bit PCM
- Naming: Same basename as input video file with
.wavextension
Implementation Details
The function:- Scans
video_dirfor video files with extensions.mp4or.mov - For each video, runs FFmpeg with the following conversion:
-ac 1- Convert to mono-ar {target_sr}- Resample to target sample rate-vn- Discard video stream (audio only)
- Writes mono WAV files to
audio_dir
Usage Example
Integration with Sync Workflow
This function is typically called before audio-based synchronization:The Flask UI automatically handles audio extraction when audio sync method is selected. This function is primarily useful for programmatic/batch processing workflows.
Requirements
Performance
Audio extraction performance depends on:- Video duration and codec
- Target sample rate (lower rates process faster)
- Disk I/O speed
- 5-minute video: ~5-10 seconds extraction time
- 30-minute video: ~20-30 seconds extraction time
See Also
audio_sync
Use extracted audio for GCC-PHAT synchronization
Configuration
Configure audio and video directory paths