sintonia/analyzer/libretime_analyzer/ffmpeg.py
jo ceab19271d feat(analyzer): analyze replaygain using ffmpeg
- remove pycairo pip install
- fix py36 compatibility
- reraise when executable was not found

BREAKING CHANGE: The analyzer requires 'ffmpeg'. The 'rgain3' python package and it's system dependencies can be removed.
2022-01-24 12:54:31 +02:00

56 lines
1.2 KiB
Python

import re
from pathlib import Path
from typing import Optional
from .utils import run_
def _ffmpeg(*args, **kwargs):
return run_(
"ffmpeg",
*args,
"-f",
"null",
"/dev/null",
"-hide_banner",
"-nostats",
**kwargs,
)
def _ffprobe(*args, **kwargs):
return run_("ffprobe", *args, **kwargs)
_PROBE_REPLAYGAIN_RE = re.compile(
r".*REPLAYGAIN_TRACK_GAIN: ([-+]?[0-9]+\.[0-9]+) dB.*",
)
def probe_replaygain(filepath: Path) -> Optional[float]:
"""
Probe replaygain will probe the given audio file and return the replaygain if available.
"""
cmd = _ffprobe("-i", filepath)
track_gain_match = _PROBE_REPLAYGAIN_RE.search(cmd.stderr)
if track_gain_match:
return float(track_gain_match.group(1))
_COMPUTE_REPLAYGAIN_RE = re.compile(
r".* track_gain = ([-+]?[0-9]+\.[0-9]+) dB.*",
)
def compute_replaygain(filepath: Path) -> Optional[float]:
"""
Compute replaygain will analyse the given audio file and return the replaygain if available.
"""
cmd = _ffmpeg("-i", filepath, "-vn", "-filter", "replaygain")
track_gain_match = _COMPUTE_REPLAYGAIN_RE.search(cmd.stderr)
if track_gain_match:
return float(track_gain_match.group(1))