Рабочий пайплайн: YouTube -> транскрипция -> выжимка -> Postgres
- новый api/: /pipeline/process (yt-dlp, субтитры или Vosk, LLM/экстрактивная суммаризация), /videos, /llm; старый код перенесён в api_legacy/ - web/: Flet UI (flet 0.28.3 + flet-web, порт 8550) - Dockerfile.api: ffmpeg слоем из mwader/static-ffmpeg, pip с кэш-маунтом - requirements/pyproject: убраны moviepy, imageio-ffmpeg, битый asyncio - README с инструкцией запуска и загрузкой Vosk-модели Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
97
api_legacy/route/moviepy.py
Normal file
97
api_legacy/route/moviepy.py
Normal file
@@ -0,0 +1,97 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from fastapi.responses import FileResponse
|
||||
from pathlib import Path
|
||||
import asyncio
|
||||
|
||||
router = APIRouter(prefix="/moviepy", tags=["moviepy"])
|
||||
|
||||
|
||||
class VideoToMp3Request(BaseModel):
|
||||
filename: str
|
||||
|
||||
|
||||
class VideoToWavRequest(BaseModel):
|
||||
filename: str
|
||||
|
||||
|
||||
def _run_moviepy(src: Path, dest: Path):
|
||||
from moviepy import VideoFileClip
|
||||
|
||||
clip = VideoFileClip(str(src))
|
||||
try:
|
||||
if clip.audio is None:
|
||||
raise RuntimeError("Source file has no audio stream")
|
||||
clip.audio.write_audiofile(str(dest), codec="libmp3lame", bitrate="192k")
|
||||
finally:
|
||||
clip.close()
|
||||
|
||||
|
||||
def _run_moviepy_to_wav(src: Path, dest: Path):
|
||||
from moviepy import VideoFileClip
|
||||
|
||||
clip = VideoFileClip(str(src))
|
||||
try:
|
||||
if clip.audio is None:
|
||||
raise RuntimeError("Source file has no audio stream")
|
||||
clip.audio.write_audiofile(str(dest), fps=16000, nbytes=2)
|
||||
finally:
|
||||
clip.close()
|
||||
|
||||
|
||||
@router.post("/video-to-mp3")
|
||||
async def video_to_mp3(req: VideoToMp3Request):
|
||||
try:
|
||||
import moviepy # noqa: F401
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="moviepy is not installed in the runtime. Install moviepy/imageio-ffmpeg")
|
||||
|
||||
project_root = Path(__file__).resolve().parent.parent.parent
|
||||
data_dir = project_root / "data"
|
||||
src = (data_dir / req.filename).resolve()
|
||||
try:
|
||||
src.relative_to(data_dir)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=400, detail="Invalid filename")
|
||||
if not src.exists() or not src.is_file():
|
||||
raise HTTPException(status_code=404, detail="Source file not found")
|
||||
|
||||
out_dir = data_dir / "audio"
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
dest = out_dir / f"{src.stem}.mp3"
|
||||
|
||||
try:
|
||||
await asyncio.get_running_loop().run_in_executor(None, _run_moviepy, src, dest)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"conversion failed: {e}")
|
||||
|
||||
return FileResponse(str(dest), filename=dest.name, media_type="audio/mpeg")
|
||||
|
||||
|
||||
@router.post("/video-to-wav")
|
||||
async def video_to_wav(req: VideoToWavRequest):
|
||||
try:
|
||||
import moviepy # noqa: F401
|
||||
except Exception:
|
||||
raise HTTPException(status_code=503, detail="moviepy is not installed in the runtime. Install moviepy/imageio-ffmpeg")
|
||||
|
||||
project_root = Path(__file__).resolve().parent.parent.parent
|
||||
data_dir = project_root / "data"
|
||||
src = (data_dir / req.filename).resolve()
|
||||
try:
|
||||
src.relative_to(data_dir)
|
||||
except Exception:
|
||||
raise HTTPException(status_code=400, detail="Invalid filename")
|
||||
if not src.exists() or not src.is_file():
|
||||
raise HTTPException(status_code=404, detail="Source file not found")
|
||||
|
||||
out_dir = data_dir / "audio"
|
||||
out_dir.mkdir(parents=True, exist_ok=True)
|
||||
dest = out_dir / f"{src.stem}.wav"
|
||||
|
||||
try:
|
||||
await asyncio.get_running_loop().run_in_executor(None, _run_moviepy_to_wav, src, dest)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=f"conversion failed: {e}")
|
||||
|
||||
return FileResponse(str(dest), filename=dest.name, media_type="audio/wav")
|
||||
Reference in New Issue
Block a user