55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
import logging
|
|
import shlex
|
|
import subprocess
|
|
from pathlib import Path
|
|
from typing import Sequence
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def _run_ffmpeg(args: Sequence[str]) -> None:
|
|
cmd = ["ffmpeg", "-hide_banner", "-loglevel", "error", *args]
|
|
logger.debug("Executando ffmpeg: %s", " ".join(shlex.quote(part) for part in cmd))
|
|
completed = subprocess.run(cmd, check=False)
|
|
if completed.returncode != 0:
|
|
raise RuntimeError(f"ffmpeg falhou com exit code {completed.returncode}")
|
|
|
|
|
|
def extract_audio_to_wav(input_video: Path, output_wav: Path) -> Path:
|
|
_run_ffmpeg(
|
|
[
|
|
"-y",
|
|
"-i",
|
|
str(input_video),
|
|
"-ac",
|
|
"1",
|
|
"-ar",
|
|
"16000",
|
|
"-vn",
|
|
str(output_wav),
|
|
]
|
|
)
|
|
return output_wav
|
|
|
|
|
|
def create_video_segment(input_video: Path, start: float, end: float, output_path: Path) -> Path:
|
|
duration = max(0.01, end - start)
|
|
_run_ffmpeg(
|
|
[
|
|
"-y",
|
|
"-i",
|
|
str(input_video),
|
|
"-ss",
|
|
f"{start:.3f}",
|
|
"-t",
|
|
f"{duration:.3f}",
|
|
"-c",
|
|
"copy",
|
|
str(output_path),
|
|
]
|
|
)
|
|
return output_path
|
|
|