36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
"""Kleiner Wrapper um ffprobe zur Längenermittlung von Audiodateien."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def probe_duration_seconds(audio_path: Path) -> float:
|
|
"""Ermittelt die Länge einer Audiodatei in Sekunden via ffprobe.
|
|
|
|
Gibt 0.0 zurück, wenn ffprobe die Datei nicht auswerten kann, statt
|
|
abzustürzen - die Länge ist reine Anzeige-Information und darf das
|
|
Hinzufügen einer Datei nicht verhindern.
|
|
"""
|
|
try:
|
|
result = subprocess.run(
|
|
[
|
|
"ffprobe",
|
|
"-v", "error",
|
|
"-show_entries", "format=duration",
|
|
"-of", "default=noprint_wrappers=1:nokey=1",
|
|
str(audio_path),
|
|
],
|
|
capture_output=True,
|
|
text=True,
|
|
check=True,
|
|
)
|
|
return float(result.stdout.strip())
|
|
except (subprocess.CalledProcessError, ValueError, FileNotFoundError):
|
|
logger.exception("ffprobe konnte die Länge von %s nicht ermitteln", audio_path)
|
|
return 0.0
|