Skip to content

The open-source AI voice studio. Clone, dictate, create.

refactor(export): share ffmpeg lookup/encode in utils/ffmpeg.py

jamiepinebf4d00c451f8
diff --git a/backend/services/stories.py b/backend/services/stories.py
index dadfb0d9..cb1eb6f4 100644
--- a/backend/services/stories.py
+++ b/backend/services/stories.py
@@ -8,8 +8,6 @@ from datetime import UTC, datetime
import asyncio
import os
import re
-import shutil
-import subprocess
import uuid
import tempfile
from pathlib import Path
@@ -17,6 +15,7 @@ from sqlalchemy.orm import Session
from sqlalchemy import func
from .. import config
+from ..utils.ffmpeg import encode_audio
from ..models import (
StoryCreate,
StoryResponse,
@@ -939,38 +938,12 @@ def _ffmpeg_encode(
Raises RuntimeError on missing ffmpeg or non-zero exit.
"""
- if shutil.which("ffmpeg") is None:
- raise RuntimeError(
- "ffmpeg is required for m4b/mp3 story export — install it or request format=wav"
- )
-
- cmd: List[str] = ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y", "-i", str(wav_path)]
meta_path: Optional[Path] = None
try:
if chapters:
meta_path = wav_path.with_suffix(".chapters.txt")
_write_ffmetadata(chapters, meta_path)
- cmd.extend(["-i", str(meta_path), "-map_metadata", "1", "-map_chapters", "1"])
-
- if fmt == "m4b":
- # The 'ipod' muxer is ffmpeg's name for the .m4b container.
- cmd.extend(["-map", "0:a", "-c:a", "aac", "-b:a", "128k", "-f", "ipod"])
- elif fmt == "mp3":
- cmd.extend(["-map", "0:a", "-c:a", "libmp3lame", "-b:a", "192k", "-f", "mp3"])
- else:
- raise ValueError(f"Unsupported export format: {fmt}")
-
- cmd.append(str(out_path))
- try:
- # Hard ceiling — a stuck ffmpeg must not pin server resources
- # indefinitely. 10 minutes is generous enough for a multi-hour
- # audiobook on slow hardware but still bounded.
- result = subprocess.run(cmd, capture_output=True, check=False, timeout=600)
- except subprocess.TimeoutExpired as exc:
- raise RuntimeError("ffmpeg timed out during story export") from exc
- if result.returncode != 0:
- stderr = result.stderr.decode("utf-8", errors="replace").strip()
- raise RuntimeError(f"ffmpeg exited {result.returncode}: {stderr}")
+ encode_audio(wav_path, out_path, fmt, metadata_path=meta_path)
finally:
if meta_path is not None:
meta_path.unlink(missing_ok=True)
diff --git a/backend/utils/ffmpeg.py b/backend/utils/ffmpeg.py
new file mode 100644
index 00000000..149e89bc
--- /dev/null
+++ b/backend/utils/ffmpeg.py
@@ -0,0 +1,92 @@
+"""Locate ffmpeg and transcode WAV exports into compressed containers.
+
+Voicebox does not bundle ffmpeg. Export paths that need it (MP3, M4B) shell out
+to whatever the user has installed and raise a clear ``RuntimeError`` when it
+is missing so the API can turn that into an actionable 503.
+"""
+
+import os
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+from typing import List, Optional
+
+# GUI-launched apps on macOS get a minimal PATH (/usr/bin:/bin:/usr/sbin:/sbin),
+# so a Homebrew or MacPorts ffmpeg is invisible to shutil.which. Windows package
+# managers likewise drop shims in per-user dirs the sidecar may not see.
+_EXTRA_DIRS = [
+ "/opt/homebrew/bin",
+ "/usr/local/bin",
+ "/opt/local/bin",
+]
+if sys.platform == "win32":
+ _local = os.environ.get("LOCALAPPDATA")
+ if _local:
+ _EXTRA_DIRS.append(str(Path(_local) / "Microsoft" / "WinGet" / "Links"))
+ _EXTRA_DIRS.append(r"C:\ffmpeg\bin")
+
+MISSING_MESSAGE = (
+ "ffmpeg is required for MP3/M4B export but was not found. Install it "
+ "(macOS: brew install ffmpeg; Windows: winget install Gyan.FFmpeg; "
+ "Linux: apt install ffmpeg) and restart Voicebox, or export as WAV."
+)
+
+ENCODERS = {
+ # The 'ipod' muxer is ffmpeg's name for the .m4b container.
+ "m4b": ["-c:a", "aac", "-b:a", "128k", "-f", "ipod"],
+ "mp3": ["-c:a", "libmp3lame", "-b:a", "192k", "-f", "mp3"],
+}
+
+
+def find_ffmpeg(name: str = "ffmpeg") -> Optional[str]:
+ """Return the path to ``name`` (ffmpeg/ffprobe) or None when not installed."""
+ override = os.environ.get("VOICEBOX_FFMPEG_DIR")
+ if override:
+ candidate = Path(override) / (f"{name}.exe" if sys.platform == "win32" else name)
+ if candidate.is_file():
+ return str(candidate)
+ found = shutil.which(name)
+ if found:
+ return found
+ for d in _EXTRA_DIRS:
+ found = shutil.which(name, path=d)
+ if found:
+ return found
+ return None
+
+
+def encode_audio(
+ wav_path: Path,
+ out_path: Path,
+ fmt: str,
+ metadata_path: Optional[Path] = None,
+ timeout: int = 600,
+) -> None:
+ """Transcode ``wav_path`` to ``fmt`` ("mp3" or "m4b") at ``out_path``.
+
+ ``metadata_path`` is an optional FFMETADATA1 file whose tags and chapters
+ are copied into the output. Raises ``ValueError`` for an unknown ``fmt``
+ and ``RuntimeError`` when ffmpeg is missing, times out, or exits non-zero.
+ """
+ if fmt not in ENCODERS:
+ raise ValueError(f"Unsupported export format: {fmt}")
+
+ ffmpeg = find_ffmpeg()
+ if ffmpeg is None:
+ raise RuntimeError(MISSING_MESSAGE)
+
+ cmd: List[str] = [ffmpeg, "-hide_banner", "-loglevel", "error", "-y", "-i", str(wav_path)]
+ if metadata_path is not None:
+ cmd.extend(["-i", str(metadata_path), "-map_metadata", "1", "-map_chapters", "1"])
+ cmd.extend(["-map", "0:a", *ENCODERS[fmt], str(out_path)])
+
+ try:
+ # Hard ceiling so a stuck ffmpeg can't pin server resources forever;
+ # ten minutes covers a multi-hour audiobook on slow hardware.
+ result = subprocess.run(cmd, capture_output=True, check=False, timeout=timeout)
+ except subprocess.TimeoutExpired as exc:
+ raise RuntimeError("ffmpeg timed out during audio export") from exc
+ if result.returncode != 0:
+ stderr = result.stderr.decode("utf-8", errors="replace").strip()
+ raise RuntimeError(f"ffmpeg exited {result.returncode}: {stderr}")