fix(music-lab): ffmpeg 인코딩 가속 + 타임아웃 확장 (저성능 CPU 대응)

- preset fast → ultrafast (5-10x 가속, J4025 같은 저성능 CPU에서 5분 내 완료)
- tune stillimage 추가 (정적 배경 + 파형 오버레이에 최적)
- threads 0 — 모든 CPU 코어 활용
- VIDEO_TIMEOUT_S 300 → 600 (안전 마진)
- subprocess.TimeoutExpired 캐치하여 명확한 에러 메시지
This commit is contained in:
2026-05-09 01:01:03 +09:00
parent 97b15cb985
commit 47e5315487

View File

@@ -7,7 +7,7 @@ from . import storage
logger = logging.getLogger("music-lab.video") logger = logging.getLogger("music-lab.video")
VIDEO_TIMEOUT_S = 300 # 5분 VIDEO_TIMEOUT_S = 600 # 10분 — Celeron J4025 같은 저성능 CPU 여유
class VideoGenerationError(Exception): class VideoGenerationError(Exception):
@@ -28,7 +28,10 @@ def generate(*, pipeline_id: int, audio_path: str, cover_path: str,
cmd = _build_visualizer_cmd(audio_path, cover_path, out_path, w, h) cmd = _build_visualizer_cmd(audio_path, cover_path, out_path, w, h)
logger.info("ffmpeg 실행: %s", " ".join(cmd)) logger.info("ffmpeg 실행: %s", " ".join(cmd))
result = subprocess.run(cmd, capture_output=True, text=True, timeout=VIDEO_TIMEOUT_S) try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=VIDEO_TIMEOUT_S)
except subprocess.TimeoutExpired:
raise VideoGenerationError(f"ffmpeg 타임아웃 ({VIDEO_TIMEOUT_S}s) — CPU 부하 또는 입력 파일 문제")
if result.returncode != 0: if result.returncode != 0:
raise VideoGenerationError(f"ffmpeg 실패: {result.stderr[-800:]}") raise VideoGenerationError(f"ffmpeg 실패: {result.stderr[-800:]}")
@@ -49,7 +52,11 @@ def _build_visualizer_cmd(audio: str, bg: str, out: str, w: str, h: str) -> list
f"[1:a]showwaves=s={w}x200:mode=cline:colors=0xFF4444@0.8[wave];" f"[1:a]showwaves=s={w}x200:mode=cline:colors=0xFF4444@0.8[wave];"
f"[bg][wave]overlay=0:({h}-200)[out]", f"[bg][wave]overlay=0:({h}-200)[out]",
"-map", "[out]", "-map", "1:a", "-map", "[out]", "-map", "1:a",
"-c:v", "libx264", "-preset", "fast", "-crf", "23", "-c:v", "libx264",
"-preset", "ultrafast", # was "fast" — ultrafast is 5-10x faster on weak CPUs
"-tune", "stillimage", # 정적 배경 + 파형 오버레이는 stillimage 튜닝이 적합
"-threads", "0", # use all available CPU cores
"-crf", "23",
"-c:a", "aac", "-b:a", "192k", "-c:a", "aac", "-b:a", "192k",
"-shortest", out, "-shortest", out,
] ]