refactor(audio): replace ffmpeg-based SNR calculation with librosa
Refactoring Request Template
Title (semantic)
refactor(audio): replace ffmpeg-based SNR calculation with librosa
Description
This merge request refactors the SNR (Signal-to-Noise Ratio) calculation logic by replacing the previous FFmpeg-based implementation with a cleaner, pure Python approach using librosa and numpy.
Previously, SNR calculation relied on multiple FFmpeg subprocess executions, stdout parsing, regex extraction, and fallback chains to estimate audio quality metrics. This created a fragile implementation tightly coupled to FFmpeg output formatting and difficult to maintain or test.
The new implementation introduces a simplified and maintainable architecture using librosa RMS analysis for signal/noise estimation.
Refactor Summary
- Replaced FFmpeg subprocess-based SNR calculation with
librosa - Removed regex parsing and stdout-dependent logic
- Removed multi-level fallback estimation methods
- Reduced complexity from ~473 lines to a compact implementation
- Added safer validation for:
- empty audio
- invalid duration
- empty RMS output
- invalid signal/noise power values
- Improved logging and error handling
- Preserved existing public function signature for backward compatibility
Technical Changes
Updated File
app/utils/snr_frequency.py
Removed
- FFmpeg subprocess execution
- ffprobe-based audio probing
- temporary audio extraction for video processing
- regex parsing of FFmpeg logs
- heuristic fallback SNR calculations
Added
-
librosa.load()for media loading - RMS feature extraction using
librosa.feature.rms() - Percentile-based signal/noise estimation:
- 95th percentile → signal power
- 5th percentile → noise power
- Runtime-safe validation checks
Integration Validation
Reviewed issue-referenced callers:
-
app/tasks/data_analysis.py→ no SNR usage found -
app/tasks/file_processing.py→ no SNR usage found
Confirmed active compatibility with:
app/api/v1/endpoints/records.pybackfill/snr_frequency.py
No breaking changes introduced to existing callers.
Checklist
- Code has been refactored for clarity, maintainability, or performance
-
No functional changes have been introduced -
Existing behavior remains backward compatible -
All existing tests are passing -
Code adheres to project coding standards -
Removed FFmpeg parsing dependency from SNR logic -
Improved error handling and logging -
Reduced technical debt and code complexity
-
Related Issue(s)
Closes #100