Files
bornetime/backend/app/services/video_utils.py
T
oval 6851b26923 Initial commit: Børnetime app scaffold
FastAPI + Celery + Svelte app for English→Danish video translation
with voice cloning. Includes diarization, STT, translation, TTS,
and audio mixing pipeline.
2026-07-10 22:42:23 +02:00

48 lines
1.2 KiB
Python

import subprocess
import os
from typing import Optional
from ..config import settings
def get_video_duration(path: str) -> Optional[float]:
cmd = [
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0 and result.stdout.strip():
return float(result.stdout.strip())
return None
def extract_audio(video_path: str, output_path: str, sample_rate: int = 16000) -> str:
cmd = [
"ffmpeg",
"-y",
"-i",
video_path,
"-vn",
"-acodec",
"pcm_s16le",
"-ar",
str(sample_rate),
"-ac",
"1",
output_path,
]
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode != 0:
raise RuntimeError(f"Audio extraction failed: {result.stderr}")
return output_path
def build_output_path(video_path: str) -> str:
base, ext = os.path.splitext(os.path.basename(video_path))
return f"{base}_en_da_audio{ext}"