ClipQuery / clipper.py
maguid28's picture
initial commit
45b9636
raw
history blame contribute delete
811 Bytes
import subprocess, uuid, os
from logging_config import logger
def clip(source_path: str, start: float, end: float, out_dir: str = "/tmp") -> str:
"""Extract an audio clip from source_path between start and end seconds.
Returns path to generated mp3 file inside out_dir.
"""
out = os.path.join(out_dir, f"{uuid.uuid4()}.mp3")
cmd = [
"ffmpeg",
"-hide_banner",
"-loglevel",
"error",
"-ss",
str(start),
"-to",
str(end),
"-i",
source_path,
"-vn", # no video
"-acodec",
"libmp3lame",
"-ar",
"44100", # sample rate
"-b:a",
"96k", # bitrate
"-y",
out,
]
logger.info(" ".join(cmd))
subprocess.run(cmd, check=True)
return out