70 lines
1.7 KiB
Python
70 lines
1.7 KiB
Python
import shutil
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
from .file_utils import resolve_path
|
|
|
|
|
|
def check_ffmpeg():
|
|
ffmpeg = shutil.which("ffmpeg") or shutil.which("ffmpeg.exe")
|
|
if ffmpeg is None:
|
|
raise RuntimeError("ffmpeg not found. Please install ffmpeg and add it to PATH.")
|
|
return ffmpeg
|
|
|
|
|
|
def run_ffmpeg(args, error_label):
|
|
ffmpeg = check_ffmpeg()
|
|
command = [ffmpeg] + [str(arg) for arg in args]
|
|
result = subprocess.run(command, capture_output=True, text=True)
|
|
if result.returncode != 0:
|
|
stderr = result.stderr.strip()
|
|
raise RuntimeError(f"{error_label} failed with exit code {result.returncode}: {stderr}")
|
|
|
|
|
|
def convert_video_to_25fps(input_path, output_path):
|
|
input_file = resolve_path(input_path)
|
|
output_file = resolve_path(output_path)
|
|
output_file.parent.mkdir(parents=True, exist_ok=True)
|
|
run_ffmpeg(
|
|
[
|
|
"-y",
|
|
"-i",
|
|
input_file,
|
|
"-r",
|
|
"25",
|
|
"-an",
|
|
"-c:v",
|
|
"libx264",
|
|
"-pix_fmt",
|
|
"yuv420p",
|
|
"-crf",
|
|
"18",
|
|
output_file,
|
|
],
|
|
"25fps video conversion",
|
|
)
|
|
return output_file
|
|
|
|
|
|
def convert_audio_to_16k_wav(input_path, output_path):
|
|
input_file = resolve_path(input_path)
|
|
output_file = resolve_path(output_path)
|
|
output_file.parent.mkdir(parents=True, exist_ok=True)
|
|
run_ffmpeg(
|
|
[
|
|
"-y",
|
|
"-i",
|
|
input_file,
|
|
"-vn",
|
|
"-ac",
|
|
"1",
|
|
"-ar",
|
|
"16000",
|
|
"-acodec",
|
|
"pcm_s16le",
|
|
output_file,
|
|
],
|
|
"16kHz audio conversion",
|
|
)
|
|
return output_file
|