Spaces:
Running
Running
refactor: merge app/ vào backend/app/, xoá app/ gốc
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- Dockerfile +1 -2
- app/api/__init__.py +0 -1
- app/api/common.py +0 -61
- app/api/cut.py +0 -203
- app/api/download.py +0 -113
- app/api/dub.py +0 -174
- app/api/shuffle.py +0 -188
- app/config.py +0 -70
- app/core/files.py +0 -86
- app/core/jobs.py +0 -151
- app/main.py +0 -83
- app/services/cutter/__init__.py +0 -0
- app/services/cutter/cut.py +0 -85
- app/services/cutter/modes.py +0 -265
- app/services/cutter/probe.py +0 -63
- app/services/downloader/__init__.py +0 -0
- app/services/downloader/ydl.py +0 -168
- app/services/dubber/__init__.py +0 -0
- app/services/dubber/asr.py +0 -86
- app/services/dubber/audio.py +0 -114
- app/services/dubber/mux.py +0 -127
- app/services/dubber/pipeline.py +0 -214
- app/services/dubber/probe_dur.py +0 -13
- app/services/dubber/translate.py +0 -88
- app/services/dubber/tts.py +0 -144
- app/services/listing/__init__.py +0 -0
- app/services/shuffler/__init__.py +0 -0
- app/services/shuffler/engine.py +0 -204
- app/services/shuffler/render.py +0 -81
- app/services/videogen/__init__.py +0 -0
- {app → backend}/__init__.py +0 -0
- {app → backend/app}/api/auth.py +0 -0
- backend/app/api/common.py +2 -5
- backend/app/api/cut.py +33 -63
- backend/app/api/download.py +24 -36
- backend/app/api/dub.py +5 -8
- {app → backend/app}/api/listing.py +0 -0
- backend/app/api/shuffle.py +30 -50
- {app → backend/app}/api/video.py +0 -0
- backend/app/config.py +20 -6
- backend/app/core/files.py +0 -6
- backend/app/core/jobs.py +1 -6
- backend/app/main.py +29 -6
- {app/core → backend/app/services/auth}/__init__.py +0 -0
- {app → backend/app}/services/auth/manager.py +0 -0
- {app → backend/app}/services/auth/storage.py +0 -0
- backend/app/services/cutter/modes.py +117 -301
- backend/app/services/downloader/ydl.py +23 -82
- backend/app/services/dubber/asr.py +9 -37
- backend/app/services/dubber/mux.py +2 -7
Dockerfile
CHANGED
|
@@ -15,9 +15,8 @@ ENV HF_HOME=/tmp/hf_cache
|
|
| 15 |
WORKDIR /app
|
| 16 |
COPY backend/requirements.txt .
|
| 17 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 18 |
-
COPY ./app ./app
|
| 19 |
COPY ./backend ./backend
|
| 20 |
COPY --from=frontend /frontend/dist ./frontend/dist
|
| 21 |
ENV PORT=7860
|
| 22 |
EXPOSE 7860
|
| 23 |
-
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 15 |
WORKDIR /app
|
| 16 |
COPY backend/requirements.txt .
|
| 17 |
RUN pip install --no-cache-dir -r requirements.txt
|
|
|
|
| 18 |
COPY ./backend ./backend
|
| 19 |
COPY --from=frontend /frontend/dist ./frontend/dist
|
| 20 |
ENV PORT=7860
|
| 21 |
EXPOSE 7860
|
| 22 |
+
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
app/api/__init__.py
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
"""Gói router FastAPI cho các tool."""
|
|
|
|
|
|
app/api/common.py
DELETED
|
@@ -1,61 +0,0 @@
|
|
| 1 |
-
"""Endpoint dùng chung: trạng thái job, tải file kết quả."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
|
| 7 |
-
from fastapi import APIRouter, HTTPException
|
| 8 |
-
from fastapi.responses import FileResponse
|
| 9 |
-
|
| 10 |
-
from ..core.files import session_dir, delete_session
|
| 11 |
-
from ..core.jobs import jobs
|
| 12 |
-
|
| 13 |
-
router = APIRouter(prefix="/api", tags=["common"])
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
@router.delete("/sessions/{sid}")
|
| 17 |
-
def drop_session(sid: str):
|
| 18 |
-
"""Xoá phiên tạm sau khi user đã tải kết quả về máy (không bắt buộc)."""
|
| 19 |
-
return {"deleted": delete_session(sid), "session": sid}
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@router.get("/jobs/{job_id}")
|
| 23 |
-
def job_status(job_id: str):
|
| 24 |
-
job = jobs.get(job_id)
|
| 25 |
-
if not job:
|
| 26 |
-
raise HTTPException(404, "job không tồn tại")
|
| 27 |
-
return job.to_dict()
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
@router.post("/jobs/{job_id}/cancel")
|
| 31 |
-
def job_cancel(job_id: str):
|
| 32 |
-
jobs.cancel(job_id)
|
| 33 |
-
return {"ok": True}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
@router.get("/sessions/{sid}/inputs")
|
| 37 |
-
def list_inputs(sid: str):
|
| 38 |
-
"""Liệt kê video trong thư mục inputs của phiên (dùng khi đẩy từ Down list)."""
|
| 39 |
-
d = os.path.join(session_dir(sid), "inputs")
|
| 40 |
-
if not os.path.isdir(d):
|
| 41 |
-
return {"session": sid, "files": []}
|
| 42 |
-
out = []
|
| 43 |
-
for name in sorted(os.listdir(d)):
|
| 44 |
-
full = os.path.join(d, name)
|
| 45 |
-
if os.path.isfile(full):
|
| 46 |
-
out.append({
|
| 47 |
-
"file": f"inputs/{name}",
|
| 48 |
-
"name": name,
|
| 49 |
-
"size": os.path.getsize(full),
|
| 50 |
-
"url": f"/api/files/{sid}/inputs/{name}",
|
| 51 |
-
})
|
| 52 |
-
return {"session": sid, "files": out}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
@router.get("/files/{sid}/{path:path}")
|
| 56 |
-
def get_file(sid: str, path: str):
|
| 57 |
-
base = os.path.abspath(session_dir(sid))
|
| 58 |
-
full = os.path.abspath(os.path.join(base, path))
|
| 59 |
-
if not full.startswith(base) or not os.path.exists(full):
|
| 60 |
-
raise HTTPException(404, "file không tồn tại")
|
| 61 |
-
return FileResponse(full, filename=os.path.basename(full))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/cut.py
DELETED
|
@@ -1,203 +0,0 @@
|
|
| 1 |
-
"""API tool: Cut video.
|
| 2 |
-
|
| 3 |
-
Luồng FE:
|
| 4 |
-
1) upload video -> /upload (trả session + info)
|
| 5 |
-
2) /preview -> sinh khúc theo mode + thumbnail strip để xem trước/kéo điểm cắt
|
| 6 |
-
3) /execute -> cắt theo các điểm cắt (đã chỉnh tay) -> zip
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
from __future__ import annotations
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
|
| 13 |
-
from fastapi import APIRouter, File, Form, UploadFile
|
| 14 |
-
from pydantic import BaseModel
|
| 15 |
-
|
| 16 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 17 |
-
from ..core.jobs import jobs
|
| 18 |
-
from ..services.cutter import cut as cutter
|
| 19 |
-
from ..services.cutter import modes
|
| 20 |
-
from ..services.cutter.probe import probe
|
| 21 |
-
|
| 22 |
-
router = APIRouter(prefix="/api/cut", tags=["cut"])
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
@router.post("/upload")
|
| 26 |
-
async def upload(file: UploadFile = File(...), session: str = Form(None)):
|
| 27 |
-
sid = session or new_session("cut")
|
| 28 |
-
path = save_upload(sid, file.filename, await file.read(), folder="inputs")
|
| 29 |
-
info = probe(path)
|
| 30 |
-
return {
|
| 31 |
-
"session": sid,
|
| 32 |
-
"file": os.path.relpath(path, session_dir(sid)),
|
| 33 |
-
"info": {"duration": info.duration, "fps": info.fps,
|
| 34 |
-
"nb_frames": info.nb_frames,
|
| 35 |
-
"width": info.width, "height": info.height},
|
| 36 |
-
"auto_count": modes.auto_segment_count(info),
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
class PreviewReq(BaseModel):
|
| 41 |
-
session: str
|
| 42 |
-
file: str
|
| 43 |
-
mode: str = "even" # even | scene | highlight | smart
|
| 44 |
-
count: int | None = None # số khúc (even) — None để máy tự chia
|
| 45 |
-
min_len: float = 1.5
|
| 46 |
-
sensitivity: float = 0.5
|
| 47 |
-
top_ratio: float = 0.4
|
| 48 |
-
thumbnails: int = 40
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
@router.post("/preview")
|
| 52 |
-
def preview(req: PreviewReq):
|
| 53 |
-
src = os.path.join(session_dir(req.session), req.file)
|
| 54 |
-
info = probe(src)
|
| 55 |
-
segs = modes.build_segments(
|
| 56 |
-
info, req.mode, n=req.count, min_len=req.min_len,
|
| 57 |
-
sensitivity=req.sensitivity, top_ratio=req.top_ratio)
|
| 58 |
-
thumbs = cutter.thumbnail_strip(
|
| 59 |
-
info, sub_dir(req.session, "thumbs"), count=req.thumbnails)
|
| 60 |
-
rel_thumbs = [{"time": t["time"],
|
| 61 |
-
"url": f"/api/files/{req.session}/thumbs/{os.path.basename(t['path'])}"}
|
| 62 |
-
for t in thumbs]
|
| 63 |
-
return {
|
| 64 |
-
"info": {"duration": info.duration, "fps": info.fps,
|
| 65 |
-
"nb_frames": info.nb_frames},
|
| 66 |
-
"segments": [s.to_dict() for s in segs],
|
| 67 |
-
"thumbnails": rel_thumbs,
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
class CutPoint(BaseModel):
|
| 72 |
-
start_frame: int
|
| 73 |
-
end_frame: int
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
class ExecuteReq(BaseModel):
|
| 77 |
-
session: str
|
| 78 |
-
file: str
|
| 79 |
-
# điểm cắt cuối cùng (đã chỉnh tay / thêm khúc trong UI). Nếu rỗng -> dùng mode.
|
| 80 |
-
segments: list[CutPoint] | None = None
|
| 81 |
-
mode: str = "even"
|
| 82 |
-
count: int | None = None
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
@router.post("/execute")
|
| 86 |
-
def execute(req: ExecuteReq):
|
| 87 |
-
src = os.path.join(session_dir(req.session), req.file)
|
| 88 |
-
info = probe(src)
|
| 89 |
-
if req.segments:
|
| 90 |
-
segs = [modes.Segment(i, s.start_frame, s.end_frame, info.fps)
|
| 91 |
-
for i, s in enumerate(req.segments)]
|
| 92 |
-
else:
|
| 93 |
-
segs = modes.build_segments(info, req.mode, n=req.count)
|
| 94 |
-
|
| 95 |
-
job = jobs.create("cut")
|
| 96 |
-
|
| 97 |
-
def work(h):
|
| 98 |
-
out_dir = sub_dir(req.session, "clips")
|
| 99 |
-
results = []
|
| 100 |
-
n = len(segs)
|
| 101 |
-
for i, seg in enumerate(segs):
|
| 102 |
-
it = h.add_item(f"clip_{i+1:03d}")
|
| 103 |
-
try:
|
| 104 |
-
name = f"clip_{seg.index + 1:03d}.mp4"
|
| 105 |
-
r = cutter.cut_segment(src, seg, os.path.join(out_dir, name))
|
| 106 |
-
results.append(r)
|
| 107 |
-
h.item_status(it, "done", detail=f"{r.duration}s", result={
|
| 108 |
-
"name": name, "duration": r.duration, "size": r.size,
|
| 109 |
-
"url": f"/api/files/{req.session}/clips/{name}"})
|
| 110 |
-
except Exception as e:
|
| 111 |
-
h.item_status(it, "error", error=str(e))
|
| 112 |
-
h.set_progress((i + 1) / max(1, n))
|
| 113 |
-
zp = os.path.join(session_dir(req.session), "clips.zip")
|
| 114 |
-
cutter.zip_clips(results, zp)
|
| 115 |
-
return {"session": req.session, "clips": len(results),
|
| 116 |
-
"zip": "clips.zip"}
|
| 117 |
-
|
| 118 |
-
jobs.submit(job, work)
|
| 119 |
-
return {"job_id": job.id, "session": req.session,
|
| 120 |
-
"segments": [s.to_dict() for s in segs]}
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
class BatchItem(BaseModel):
|
| 124 |
-
file: str
|
| 125 |
-
segments: list[CutPoint] | None = None
|
| 126 |
-
mode: str = "even"
|
| 127 |
-
count: int | None = None
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
class ExecuteBatchReq(BaseModel):
|
| 131 |
-
session: str
|
| 132 |
-
videos: list[BatchItem]
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
@router.post("/execute-batch")
|
| 136 |
-
def execute_batch(req: ExecuteBatchReq):
|
| 137 |
-
"""Cắt NHIỀU video cùng lúc trong một phiên -> 1 danh sách clip gộp.
|
| 138 |
-
|
| 139 |
-
Tên clip có tiền tố theo video để không đè nhau: v{vi}_clip_{i}.mp4.
|
| 140 |
-
"""
|
| 141 |
-
plans = []
|
| 142 |
-
for vi, item in enumerate(req.videos):
|
| 143 |
-
src = os.path.join(session_dir(req.session), item.file)
|
| 144 |
-
info = probe(src)
|
| 145 |
-
if item.segments:
|
| 146 |
-
segs = [modes.Segment(i, s.start_frame, s.end_frame, info.fps)
|
| 147 |
-
for i, s in enumerate(item.segments)]
|
| 148 |
-
else:
|
| 149 |
-
segs = modes.build_segments(info, item.mode, n=item.count)
|
| 150 |
-
plans.append((vi, item.file, src, segs))
|
| 151 |
-
|
| 152 |
-
job = jobs.create("cut")
|
| 153 |
-
|
| 154 |
-
def work(h):
|
| 155 |
-
out_dir = sub_dir(req.session, "clips")
|
| 156 |
-
results = []
|
| 157 |
-
total = sum(len(p[3]) for p in plans) or 1
|
| 158 |
-
done = 0
|
| 159 |
-
for vi, fname, src, segs in plans:
|
| 160 |
-
base = os.path.splitext(os.path.basename(fname))[0]
|
| 161 |
-
for i, seg in enumerate(segs):
|
| 162 |
-
it = h.add_item(f"v{vi+1}_clip_{i+1:03d}")
|
| 163 |
-
try:
|
| 164 |
-
name = f"v{vi+1}_clip_{i+1:03d}.mp4"
|
| 165 |
-
r = cutter.cut_segment(src, seg, os.path.join(out_dir, name))
|
| 166 |
-
results.append({"name": name, "duration": r.duration,
|
| 167 |
-
"size": r.size, "video": base,
|
| 168 |
-
"url": f"/api/files/{req.session}/clips/{name}"})
|
| 169 |
-
h.item_status(it, "done", detail=f"{r.duration}s", result=results[-1])
|
| 170 |
-
except Exception as e:
|
| 171 |
-
h.item_status(it, "error", error=str(e))
|
| 172 |
-
done += 1
|
| 173 |
-
h.set_progress(done / total)
|
| 174 |
-
return {"session": req.session, "clips": len(results)}
|
| 175 |
-
|
| 176 |
-
jobs.submit(job, work)
|
| 177 |
-
return {"job_id": job.id, "session": req.session}
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
@router.post("/push-shuffle")
|
| 181 |
-
def push_shuffle(req: PushShuffleReq):
|
| 182 |
-
"""Đẩy các khúc đã cắt sang tool Xào: mỗi khúc thành 1 segment (seg_i).
|
| 183 |
-
|
| 184 |
-
FE mở Xào với session này; mỗi segment đã có sẵn 1 clip theo đúng vị trí,
|
| 185 |
-
người dùng có thể thêm clip vào từng segment sau.
|
| 186 |
-
"""
|
| 187 |
-
import shutil
|
| 188 |
-
new_sid = new_session("shuffle")
|
| 189 |
-
segments = []
|
| 190 |
-
for i, rel in enumerate(req.clips):
|
| 191 |
-
src = os.path.join(session_dir(req.session), rel)
|
| 192 |
-
if not os.path.exists(src):
|
| 193 |
-
continue
|
| 194 |
-
folder = sub_dir(new_sid, f"seg_{i}")
|
| 195 |
-
dst = os.path.join(folder, os.path.basename(src))
|
| 196 |
-
shutil.copy(src, dst)
|
| 197 |
-
seg_rel = os.path.relpath(dst, session_dir(new_sid)).replace(os.sep, "/")
|
| 198 |
-
segments.append({
|
| 199 |
-
"files": [seg_rel],
|
| 200 |
-
"name": os.path.basename(src),
|
| 201 |
-
"url": f"/api/files/{new_sid}/{seg_rel}",
|
| 202 |
-
})
|
| 203 |
-
return {"session": new_sid, "segments": segments}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/download.py
DELETED
|
@@ -1,113 +0,0 @@
|
|
| 1 |
-
"""API tool: Down list video."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
|
| 8 |
-
from fastapi import APIRouter
|
| 9 |
-
from pydantic import BaseModel
|
| 10 |
-
|
| 11 |
-
from ..config import settings
|
| 12 |
-
from ..core.files import new_session, session_dir, sub_dir, zip_paths
|
| 13 |
-
from ..core.jobs import DONE, ERROR, RUNNING, jobs
|
| 14 |
-
from ..services.downloader.ydl import detect_source, download_one, parse_links
|
| 15 |
-
|
| 16 |
-
router = APIRouter(prefix="/api/download", tags=["download"])
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
class ParseReq(BaseModel):
|
| 20 |
-
raw: str
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
class StartReq(BaseModel):
|
| 24 |
-
urls: list[str]
|
| 25 |
-
zip_output: bool = True
|
| 26 |
-
cookies_file: str | None = None
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
@router.post("/parse")
|
| 30 |
-
def parse(req: ParseReq):
|
| 31 |
-
urls = parse_links(req.raw, settings.MAX_LINKS)
|
| 32 |
-
return {
|
| 33 |
-
"count": len(urls),
|
| 34 |
-
"items": [{"url": u, "source": detect_source(u), "status": "ready"}
|
| 35 |
-
for u in urls],
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
@router.post("/start")
|
| 40 |
-
def start(req: StartReq):
|
| 41 |
-
sid = new_session("download")
|
| 42 |
-
out_dir = sub_dir(sid, "videos")
|
| 43 |
-
job = jobs.create("download")
|
| 44 |
-
|
| 45 |
-
def work(h):
|
| 46 |
-
items = [h.add_item(u, item_id=str(i)) for i, u in enumerate(req.urls)]
|
| 47 |
-
done_paths, failed = [], []
|
| 48 |
-
n = len(req.urls)
|
| 49 |
-
for i, url in enumerate(req.urls):
|
| 50 |
-
if h.cancelled():
|
| 51 |
-
break
|
| 52 |
-
it = items[i]
|
| 53 |
-
h.item_status(it, RUNNING, detail="đang tải")
|
| 54 |
-
h.log(f"[{i+1}/{n}] tải {url}")
|
| 55 |
-
res = download_one(url, out_dir, req.cookies_file)
|
| 56 |
-
if res.ok:
|
| 57 |
-
done_paths.append(res.path)
|
| 58 |
-
h.item_status(it, DONE, detail=res.title, result={
|
| 59 |
-
"path": os.path.relpath(res.path, session_dir(sid)),
|
| 60 |
-
"url": f"/api/files/{sid}/" + os.path.relpath(
|
| 61 |
-
res.path, session_dir(sid)).replace(os.sep, "/"),
|
| 62 |
-
"title": res.title, "ext": res.ext, "size": res.size,
|
| 63 |
-
"duration": res.duration, "thumbnail": res.thumbnail,
|
| 64 |
-
"source": detect_source(url),
|
| 65 |
-
})
|
| 66 |
-
h.log(f" ✔ {res.title} ({res.size} bytes)")
|
| 67 |
-
else:
|
| 68 |
-
failed.append({"url": url, "error": res.error})
|
| 69 |
-
h.item_status(it, ERROR, error=res.error)
|
| 70 |
-
h.log(f" ✗ LỖI: {res.error}", level="error")
|
| 71 |
-
h.set_progress((i + 1) / max(1, n))
|
| 72 |
-
|
| 73 |
-
zip_rel = None
|
| 74 |
-
if req.zip_output and done_paths:
|
| 75 |
-
zp = os.path.join(session_dir(sid), "downloads.zip")
|
| 76 |
-
zip_paths(done_paths, zp)
|
| 77 |
-
zip_rel = "downloads.zip"
|
| 78 |
-
|
| 79 |
-
return {
|
| 80 |
-
"session": sid,
|
| 81 |
-
"success": len(done_paths),
|
| 82 |
-
"failed": failed,
|
| 83 |
-
"zip": zip_rel,
|
| 84 |
-
}
|
| 85 |
-
|
| 86 |
-
jobs.submit(job, work)
|
| 87 |
-
return {"job_id": job.id, "session": sid}
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
class PushReq(BaseModel):
|
| 91 |
-
session: str
|
| 92 |
-
paths: list[str] # đường dẫn tương đối các video đã tải
|
| 93 |
-
target_tool: str # cut | shuffle | dub
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
@router.post("/push")
|
| 97 |
-
def push_to_tool(req: PushReq):
|
| 98 |
-
"""Đẩy các video đã tải sang tool khác (cut / shuffle / dub).
|
| 99 |
-
|
| 100 |
-
Copy file sang session mới của tool đích, trả session id để FE mở thẳng.
|
| 101 |
-
"""
|
| 102 |
-
if req.target_tool not in {"cut", "shuffle", "dub"}:
|
| 103 |
-
return {"error": "target_tool không hợp lệ"}
|
| 104 |
-
new_sid = new_session(req.target_tool)
|
| 105 |
-
dst = sub_dir(new_sid, "inputs")
|
| 106 |
-
copied = []
|
| 107 |
-
for rel in req.paths:
|
| 108 |
-
src = os.path.join(session_dir(req.session), rel)
|
| 109 |
-
if os.path.exists(src):
|
| 110 |
-
d = os.path.join(dst, os.path.basename(src))
|
| 111 |
-
shutil.copy(src, d)
|
| 112 |
-
copied.append(os.path.relpath(d, session_dir(new_sid)))
|
| 113 |
-
return {"session": new_sid, "target_tool": req.target_tool, "files": copied}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/dub.py
DELETED
|
@@ -1,174 +0,0 @@
|
|
| 1 |
-
"""API tool: Dịch / đổi giọng video.
|
| 2 |
-
|
| 3 |
-
Luồng FE:
|
| 4 |
-
1) /upload -> tải 1+ video nguồn (gọi nhiều lần) + (tuỳ chọn) mẫu giọng
|
| 5 |
-
2) /voice-sample -> tải file giọng mẫu user tự up (clone)
|
| 6 |
-
3) /start -> chạy pipeline lồng tiếng cho từng video -> trả video+audio+srt
|
| 7 |
-
|
| 8 |
-
Mỗi video là một item trong job để FE theo dõi tiến độ riêng.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import os
|
| 14 |
-
|
| 15 |
-
from fastapi import APIRouter, File, Form, UploadFile
|
| 16 |
-
from pydantic import BaseModel
|
| 17 |
-
|
| 18 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 19 |
-
from ..core.jobs import jobs
|
| 20 |
-
from ..services.dubber import pipeline
|
| 21 |
-
|
| 22 |
-
router = APIRouter(prefix="/api/dub", tags=["dub"])
|
| 23 |
-
|
| 24 |
-
LANGS = {"en", "vi", "fil"}
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# ----------------------------------------------------------------- upload ---
|
| 28 |
-
|
| 29 |
-
@router.post("/upload")
|
| 30 |
-
async def upload(file: UploadFile = File(...), session: str = Form(None)):
|
| 31 |
-
sid = session or new_session("dub")
|
| 32 |
-
path = save_upload(sid, file.filename, await file.read(), folder="inputs")
|
| 33 |
-
rel = os.path.relpath(path, session_dir(sid))
|
| 34 |
-
return {"session": sid, "file": rel, "name": os.path.basename(path),
|
| 35 |
-
"size": os.path.getsize(path)}
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
@router.post("/voice-sample")
|
| 39 |
-
async def voice_sample(file: UploadFile = File(...), session: str = Form(None)):
|
| 40 |
-
"""File giọng mẫu (WAV/MP3) user tự up để clone. Tạo phiên nếu chưa có."""
|
| 41 |
-
sid = session or new_session("dub")
|
| 42 |
-
path = save_upload(sid, file.filename, await file.read(), folder="voice")
|
| 43 |
-
rel = os.path.relpath(path, session_dir(sid))
|
| 44 |
-
return {"session": sid, "file": rel, "name": os.path.basename(path)}
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
# ------------------------------------------------------------------ start ---
|
| 48 |
-
|
| 49 |
-
class DubReq(BaseModel):
|
| 50 |
-
session: str
|
| 51 |
-
files: list[str] # danh sách video tương đối cần dịch
|
| 52 |
-
target_lang: str = "vi" # en | vi | fil
|
| 53 |
-
bg_mode: str = "duck" # none | duck | demucs
|
| 54 |
-
do_clone: bool = True
|
| 55 |
-
voice_source: str = "video" # preset | video | upload
|
| 56 |
-
speaker_file: str = "" # rel path nếu voice_source=upload
|
| 57 |
-
preset: str = "" # tên preset nếu voice_source=preset
|
| 58 |
-
style: str = "natural"
|
| 59 |
-
engine: str = "auto"
|
| 60 |
-
multi_speaker: bool = False
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
@router.post("/start")
|
| 64 |
-
def start(req: DubReq):
|
| 65 |
-
target = req.target_lang if req.target_lang in LANGS else "vi"
|
| 66 |
-
|
| 67 |
-
speaker_wav = ""
|
| 68 |
-
if req.voice_source == "upload" and req.speaker_file:
|
| 69 |
-
speaker_wav = os.path.join(session_dir(req.session), req.speaker_file)
|
| 70 |
-
|
| 71 |
-
opt = pipeline.DubOptions(
|
| 72 |
-
target_lang=target,
|
| 73 |
-
bg_mode=req.bg_mode,
|
| 74 |
-
do_clone=req.do_clone,
|
| 75 |
-
voice_source=req.voice_source,
|
| 76 |
-
speaker_wav=speaker_wav,
|
| 77 |
-
preset=req.preset,
|
| 78 |
-
style=req.style,
|
| 79 |
-
engine=req.engine,
|
| 80 |
-
multi_speaker=req.multi_speaker,
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
job = jobs.create("dub")
|
| 84 |
-
|
| 85 |
-
def work(h):
|
| 86 |
-
n = len(req.files)
|
| 87 |
-
outputs = []
|
| 88 |
-
for idx, rel in enumerate(req.files):
|
| 89 |
-
src = os.path.join(session_dir(req.session), rel)
|
| 90 |
-
base = os.path.splitext(os.path.basename(rel))[0]
|
| 91 |
-
it = h.add_item(os.path.basename(rel))
|
| 92 |
-
out_dir = sub_dir(req.session, "out", base)
|
| 93 |
-
try:
|
| 94 |
-
def prog(p, m, _i=idx, _it=it):
|
| 95 |
-
# tiến độ tổng = (đã xong video + p video hiện tại) / n
|
| 96 |
-
h.set_progress((_i + p) / max(1, n))
|
| 97 |
-
h.item_status(_it, "running", detail=m)
|
| 98 |
-
|
| 99 |
-
res = pipeline.run_dub(src, out_dir, opt, prog)
|
| 100 |
-
|
| 101 |
-
def url(path):
|
| 102 |
-
return f"/api/files/{req.session}/" + os.path.relpath(
|
| 103 |
-
path, session_dir(req.session)).replace(os.sep, "/")
|
| 104 |
-
|
| 105 |
-
result = {
|
| 106 |
-
"name": base,
|
| 107 |
-
"language": res.language,
|
| 108 |
-
"n_lines": res.n_lines,
|
| 109 |
-
"video_url": url(res.video_out),
|
| 110 |
-
"audio_url": url(res.audio_out),
|
| 111 |
-
"srt_url": url(res.srt_out),
|
| 112 |
-
"note": res.note,
|
| 113 |
-
}
|
| 114 |
-
outputs.append(result)
|
| 115 |
-
h.item_status(it, "done",
|
| 116 |
-
detail=f"{res.n_lines} câu · {res.language}",
|
| 117 |
-
result=result)
|
| 118 |
-
except Exception as e:
|
| 119 |
-
h.item_status(it, "error", error=str(e))
|
| 120 |
-
h.set_progress((idx + 1) / max(1, n))
|
| 121 |
-
return {"session": req.session, "outputs": outputs}
|
| 122 |
-
|
| 123 |
-
jobs.submit(job, work)
|
| 124 |
-
return {"job_id": job.id, "session": req.session, "videos": len(req.files)}
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
# ------------------------------------------------------- TTS từ text ---
|
| 128 |
-
|
| 129 |
-
class TtsReq(BaseModel):
|
| 130 |
-
session: str = ""
|
| 131 |
-
text: str
|
| 132 |
-
target_lang: str = "vi" # en | vi | fil
|
| 133 |
-
voice_source: str = "preset" # preset | upload
|
| 134 |
-
speaker_file: str = "" # rel path nếu voice_source=upload (clone)
|
| 135 |
-
preset: str = "" # tên giọng có sẵn
|
| 136 |
-
style: str = "natural"
|
| 137 |
-
engine: str = "auto" # auto | vieneu | xtts | voxcpm
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
@router.post("/tts")
|
| 141 |
-
def tts_from_text(req: TtsReq):
|
| 142 |
-
"""Gen voice từ text: đầu vào text + giọng (clone/preset) -> đoạn voice."""
|
| 143 |
-
sid = req.session or new_session("tts")
|
| 144 |
-
target = req.target_lang if req.target_lang in LANGS else "vi"
|
| 145 |
-
|
| 146 |
-
speaker_wav = ""
|
| 147 |
-
if req.voice_source == "upload" and req.speaker_file:
|
| 148 |
-
speaker_wav = os.path.join(session_dir(sid), req.speaker_file)
|
| 149 |
-
|
| 150 |
-
opt = pipeline.TtsOptions(
|
| 151 |
-
text=req.text, target_lang=target, voice_source=req.voice_source,
|
| 152 |
-
speaker_wav=speaker_wav, preset=req.preset, style=req.style,
|
| 153 |
-
engine=req.engine)
|
| 154 |
-
|
| 155 |
-
job = jobs.create("tts")
|
| 156 |
-
|
| 157 |
-
def work(h):
|
| 158 |
-
it = h.add_item("voice")
|
| 159 |
-
out_dir = sub_dir(sid, "tts_out")
|
| 160 |
-
try:
|
| 161 |
-
def prog(p, m, _it=it):
|
| 162 |
-
h.set_progress(p)
|
| 163 |
-
h.item_status(_it, "running", detail=m)
|
| 164 |
-
wav = pipeline.run_tts(opt, out_dir, prog)
|
| 165 |
-
rel = os.path.relpath(wav, session_dir(sid)).replace(os.sep, "/")
|
| 166 |
-
result = {"name": "voice.wav", "audio_url": f"/api/files/{sid}/{rel}"}
|
| 167 |
-
h.item_status(it, "done", detail="đã tạo voice", result=result)
|
| 168 |
-
return {"session": sid, "output": result}
|
| 169 |
-
except Exception as e:
|
| 170 |
-
h.item_status(it, "error", error=str(e))
|
| 171 |
-
return {"session": sid, "error": str(e)}
|
| 172 |
-
|
| 173 |
-
jobs.submit(job, work)
|
| 174 |
-
return {"job_id": job.id, "session": sid}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/api/shuffle.py
DELETED
|
@@ -1,188 +0,0 @@
|
|
| 1 |
-
"""API tool: Xào video.
|
| 2 |
-
|
| 3 |
-
Luồng FE:
|
| 4 |
-
1) /upload -> nạp clip vào từng khúc (folder "seg_{i}")
|
| 5 |
-
2) /upload-audio -> (tùy chọn) nạp track âm thanh để lồng vào biến thể
|
| 6 |
-
3) /estimate -> tính số tổ hợp + số sau lọc trùng
|
| 7 |
-
4) /generate -> sinh tổ hợp + dedup + render (+ lồng audio) -> zip
|
| 8 |
-
|
| 9 |
-
Thứ tự đảo do order_mode của TỪNG khúc quyết định (lock|shuffle|drop),
|
| 10 |
-
không còn cờ allow_reorder toàn cục.
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
from __future__ import annotations
|
| 14 |
-
|
| 15 |
-
import os
|
| 16 |
-
|
| 17 |
-
from fastapi import APIRouter, File, Form, UploadFile
|
| 18 |
-
from pydantic import BaseModel
|
| 19 |
-
|
| 20 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 21 |
-
from ..core.jobs import jobs
|
| 22 |
-
from ..services.shuffler import engine
|
| 23 |
-
from ..services.shuffler import render as shuffle_render
|
| 24 |
-
|
| 25 |
-
router = APIRouter(prefix="/api/shuffle", tags=["shuffle"])
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
# ----------------------------------------------------------------- upload ---
|
| 29 |
-
|
| 30 |
-
@router.post("/upload")
|
| 31 |
-
async def upload(
|
| 32 |
-
file: UploadFile = File(...),
|
| 33 |
-
segment: int = Form(...),
|
| 34 |
-
session: str = Form(None),
|
| 35 |
-
):
|
| 36 |
-
"""Tải 1 clip vào một khúc. Hỗ trợ tải cả thư mục (FE gửi nhiều file)."""
|
| 37 |
-
sid = session or new_session("shuffle")
|
| 38 |
-
folder = f"seg_{segment}"
|
| 39 |
-
path = save_upload(sid, file.filename, await file.read(), folder=folder)
|
| 40 |
-
rel = os.path.relpath(path, session_dir(sid))
|
| 41 |
-
return {
|
| 42 |
-
"session": sid, "segment": segment, "file": rel,
|
| 43 |
-
"name": os.path.basename(path), "size": os.path.getsize(path),
|
| 44 |
-
"url": f"/api/files/{sid}/{rel}",
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
@router.post("/upload-audio")
|
| 49 |
-
async def upload_audio(
|
| 50 |
-
file: UploadFile = File(...),
|
| 51 |
-
session: str = Form(...),
|
| 52 |
-
):
|
| 53 |
-
"""Tải 1 track âm thanh dùng để lồng vào các biến thể."""
|
| 54 |
-
path = save_upload(session, file.filename, await file.read(), folder="audio")
|
| 55 |
-
rel = os.path.relpath(path, session_dir(session))
|
| 56 |
-
return {
|
| 57 |
-
"session": session, "file": rel,
|
| 58 |
-
"name": os.path.basename(path), "size": os.path.getsize(path),
|
| 59 |
-
"url": f"/api/files/{session}/{rel}",
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
@router.get("/segments/{sid}")
|
| 64 |
-
def list_segments(sid: str):
|
| 65 |
-
"""Liệt kê các khúc (seg_*) đã có sẵn trong phiên — dùng khi đẩy từ Cut."""
|
| 66 |
-
base = session_dir(sid)
|
| 67 |
-
segs = []
|
| 68 |
-
if os.path.isdir(base):
|
| 69 |
-
names = sorted(
|
| 70 |
-
[d for d in os.listdir(base) if d.startswith("seg_")],
|
| 71 |
-
key=lambda x: int(x.split("_")[1]) if x.split("_")[1].isdigit() else 0)
|
| 72 |
-
for d in names:
|
| 73 |
-
folder = os.path.join(base, d)
|
| 74 |
-
if not os.path.isdir(folder):
|
| 75 |
-
continue
|
| 76 |
-
files = []
|
| 77 |
-
for fn in sorted(os.listdir(folder)):
|
| 78 |
-
full = os.path.join(folder, fn)
|
| 79 |
-
if os.path.isfile(full):
|
| 80 |
-
rel = f"{d}/{fn}"
|
| 81 |
-
files.append({"file": rel, "name": fn,
|
| 82 |
-
"size": os.path.getsize(full),
|
| 83 |
-
"url": f"/api/files/{sid}/{rel}"})
|
| 84 |
-
segs.append({"files": files})
|
| 85 |
-
return {"session": sid, "segments": segs}
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
# ------------------------------------------------ build config từ payload ---
|
| 89 |
-
|
| 90 |
-
class SegmentIn(BaseModel):
|
| 91 |
-
files: list[str]
|
| 92 |
-
order_mode: str = "shuffle" # lock | shuffle | drop
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
class ShuffleReq(BaseModel):
|
| 96 |
-
session: str
|
| 97 |
-
segments: list[SegmentIn]
|
| 98 |
-
audio_files: list[str] = [] # track âm thanh (tùy chọn)
|
| 99 |
-
similarity_threshold: float = 0.80 # >=1.0 => tắt lọc trùng
|
| 100 |
-
random_ratio: float = 1.0 # >=1.0 => không lấy mẫu
|
| 101 |
-
max_outputs: int | None = None
|
| 102 |
-
seed: int | None = None
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
def _build(req: ShuffleReq):
|
| 106 |
-
clip_map: dict[int, str] = {}
|
| 107 |
-
seg_objs = []
|
| 108 |
-
cid = 0
|
| 109 |
-
for seg in req.segments:
|
| 110 |
-
ids = []
|
| 111 |
-
for rel in seg.files:
|
| 112 |
-
clip_map[cid] = os.path.join(session_dir(req.session), rel)
|
| 113 |
-
ids.append(cid)
|
| 114 |
-
cid += 1
|
| 115 |
-
seg_objs.append(engine.Segment(clips=ids, order_mode=seg.order_mode))
|
| 116 |
-
cfg = engine.ShuffleConfig(
|
| 117 |
-
segments=seg_objs,
|
| 118 |
-
similarity_threshold=req.similarity_threshold,
|
| 119 |
-
random_ratio=req.random_ratio,
|
| 120 |
-
max_outputs=req.max_outputs,
|
| 121 |
-
seed=req.seed,
|
| 122 |
-
)
|
| 123 |
-
return cfg, clip_map
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
def _audio_paths(req: ShuffleReq) -> list[str]:
|
| 127 |
-
return [os.path.join(session_dir(req.session), a) for a in req.audio_files]
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
# --------------------------------------------------------------- estimate ---
|
| 131 |
-
|
| 132 |
-
@router.post("/estimate")
|
| 133 |
-
def estimate(req: ShuffleReq):
|
| 134 |
-
cfg, _ = _build(req)
|
| 135 |
-
est = engine.estimate(cfg)
|
| 136 |
-
preview = engine.generate(cfg)
|
| 137 |
-
return {
|
| 138 |
-
"selection_count": est.selection_count,
|
| 139 |
-
"ordering_count": est.ordering_count,
|
| 140 |
-
"raw_total": est.raw_total,
|
| 141 |
-
"after_dedup": len(preview),
|
| 142 |
-
}
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# --------------------------------------------------------------- generate ---
|
| 146 |
-
|
| 147 |
-
@router.post("/generate")
|
| 148 |
-
def generate(req: ShuffleReq):
|
| 149 |
-
cfg, clip_map = _build(req)
|
| 150 |
-
sequences = engine.generate(cfg)
|
| 151 |
-
audio_paths = _audio_paths(req)
|
| 152 |
-
|
| 153 |
-
job = jobs.create("shuffle")
|
| 154 |
-
|
| 155 |
-
def work(h):
|
| 156 |
-
h.log(f"Sẽ render {len(sequences)} biến thể"
|
| 157 |
-
+ (f" + lồng {len(audio_paths)} track âm thanh" if audio_paths else ""))
|
| 158 |
-
out_dir = sub_dir(req.session, "variations")
|
| 159 |
-
results = []
|
| 160 |
-
n = len(sequences)
|
| 161 |
-
for i, seq in enumerate(sequences, 1):
|
| 162 |
-
it = h.add_item(f"Variation_{i:02d}")
|
| 163 |
-
try:
|
| 164 |
-
paths = [clip_map[c] for c in seq]
|
| 165 |
-
name = f"Variation_{i:02d}.mp4"
|
| 166 |
-
out = os.path.join(out_dir, name)
|
| 167 |
-
if audio_paths:
|
| 168 |
-
tmp = os.path.join(out_dir, f".tmp_{i:02d}.mp4")
|
| 169 |
-
shuffle_render._concat_reencode(paths, tmp)
|
| 170 |
-
track = audio_paths[(i - 1) % len(audio_paths)]
|
| 171 |
-
shuffle_render.attach_audio(tmp, track, out)
|
| 172 |
-
os.remove(tmp)
|
| 173 |
-
else:
|
| 174 |
-
shuffle_render._concat_reencode(paths, out)
|
| 175 |
-
size = os.path.getsize(out)
|
| 176 |
-
results.append({"name": name, "path": out, "sequence": seq, "size": size})
|
| 177 |
-
h.item_status(it, "done", detail=f"{size//1024} KB", result={
|
| 178 |
-
"name": name, "size": size, "sequence": seq,
|
| 179 |
-
"url": f"/api/files/{req.session}/variations/{name}"})
|
| 180 |
-
except Exception as e:
|
| 181 |
-
h.item_status(it, "error", error=str(e))
|
| 182 |
-
h.set_progress(i / max(1, n))
|
| 183 |
-
zp = os.path.join(session_dir(req.session), "variations.zip")
|
| 184 |
-
shuffle_render.zip_variations(results, zp)
|
| 185 |
-
return {"session": req.session, "count": len(results), "zip": "variations.zip"}
|
| 186 |
-
|
| 187 |
-
jobs.submit(job, work)
|
| 188 |
-
return {"job_id": job.id, "session": req.session, "planned": len(sequences)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/config.py
DELETED
|
@@ -1,70 +0,0 @@
|
|
| 1 |
-
"""Cấu hình ứng dụng. Đọc từ biến môi trường (xem .env.example)."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import tempfile
|
| 7 |
-
from dataclasses import dataclass, field
|
| 8 |
-
|
| 9 |
-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 10 |
-
BACKEND_DIR = BASE_DIR
|
| 11 |
-
try:
|
| 12 |
-
from dotenv import load_dotenv
|
| 13 |
-
load_dotenv(os.path.join(BACKEND_DIR, ".env"))
|
| 14 |
-
except Exception:
|
| 15 |
-
pass
|
| 16 |
-
|
| 17 |
-
def _env(key: str, default: str = "") -> str:
|
| 18 |
-
return os.environ.get(key, default)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
@dataclass
|
| 22 |
-
class Settings:
|
| 23 |
-
# Vùng LÀM VIỆC TẠM theo phiên (không phải kho lưu trữ): mặc định nằm trong
|
| 24 |
-
# thư mục temp của hệ điều hành để không đọng lại trong project. Data xử lý
|
| 25 |
-
# xong là trả cho user tải về rồi tự dọn.
|
| 26 |
-
STORAGE_DIR: str = field(
|
| 27 |
-
default_factory=lambda: _env(
|
| 28 |
-
"STORAGE_DIR", os.path.join(tempfile.gettempdir(), "tnt_work")
|
| 29 |
-
)
|
| 30 |
-
)
|
| 31 |
-
# API keys cho pipeline lồng tiếng
|
| 32 |
-
GROQ_API_KEY: str = field(default_factory=lambda: _env("GROQ_API_KEY"))
|
| 33 |
-
GOOGLE_API_KEY: str = field(default_factory=lambda: _env("GOOGLE_API_KEY"))
|
| 34 |
-
# API keys cho Listing ảnh
|
| 35 |
-
OPENAI_API_KEY: str = field(default_factory=lambda: _env("OPENAI_API_KEY"))
|
| 36 |
-
OPENAI_IMAGE_API_KEY: str = field(default_factory=lambda: _env("OPENAI_IMAGE_API_KEY"))
|
| 37 |
-
|
| 38 |
-
# Database cho user auth (để trống thì dùng JSON file)
|
| 39 |
-
DATABASE_URL: str = field(default_factory=lambda: _env("DATABASE_URL", ""))
|
| 40 |
-
|
| 41 |
-
# Danh sách user được dùng key chung (cách nhau bằng dấu phẩy)
|
| 42 |
-
AUTHORIZED_USERS: list[str] = field(
|
| 43 |
-
default_factory=lambda: [u.strip() for u in _env("AUTHORIZED_USERS", "").split(",") if u.strip()]
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# Video Gen (Gradio proxy)
|
| 47 |
-
VIDEO_ADMIN_TOKEN: str = field(default_factory=lambda: _env("VIDEO_ADMIN_TOKEN"))
|
| 48 |
-
|
| 49 |
-
# model TTS / config
|
| 50 |
-
DEMUCS_MODEL: str = field(default_factory=lambda: _env("DEMUCS_MODEL", "htdemucs"))
|
| 51 |
-
WHISPER_MODEL: str = field(default_factory=lambda: _env("WHISPER_MODEL", "whisper-large-v3"))
|
| 52 |
-
GEMINI_MODEL: str = field(default_factory=lambda: _env("GEMINI_MODEL", "gemini-2.0-flash"))
|
| 53 |
-
GROQ_LLM_FALLBACK: str = field(default_factory=lambda: _env("GROQ_LLM_FALLBACK", "openai/gpt-oss-20b"))
|
| 54 |
-
|
| 55 |
-
# giới hạn
|
| 56 |
-
MAX_LINKS: int = 200
|
| 57 |
-
MAX_UPLOAD_MB: int = 2048
|
| 58 |
-
# Phiên tạm tự dọn sau ngần này giờ (mặc định 6h — đủ để xử lý + tải về).
|
| 59 |
-
SESSION_TTL_HOURS: int = field(
|
| 60 |
-
default_factory=lambda: int(_env("SESSION_TTL_HOURS", "6")))
|
| 61 |
-
|
| 62 |
-
CORS_ORIGINS: list[str] = field(
|
| 63 |
-
default_factory=lambda: _env("CORS_ORIGINS", "http://localhost:5173").split(",")
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
def __post_init__(self):
|
| 67 |
-
os.makedirs(self.STORAGE_DIR, exist_ok=True)
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
settings = Settings()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/files.py
DELETED
|
@@ -1,86 +0,0 @@
|
|
| 1 |
-
"""Quản lý thư mục lưu trữ theo phiên (session) và đóng gói zip."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
import time
|
| 8 |
-
import uuid
|
| 9 |
-
import zipfile
|
| 10 |
-
|
| 11 |
-
from ..config import settings
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
_last_sweep = 0.0
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def new_session(tool: str) -> str:
|
| 18 |
-
"""Tạo session id + thư mục tạm cho 1 phiên làm việc. Tiện thể dọn phiên cũ."""
|
| 19 |
-
_maybe_sweep()
|
| 20 |
-
sid = f"{tool}_{int(time.time())}_{uuid.uuid4().hex[:6]}"
|
| 21 |
-
os.makedirs(session_dir(sid), exist_ok=True)
|
| 22 |
-
return sid
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def _maybe_sweep(min_interval_sec: float = 600.0):
|
| 26 |
-
"""Dọn phiên quá hạn, tối đa ~10 phút/lần để khỏi tốn tài nguyên."""
|
| 27 |
-
global _last_sweep
|
| 28 |
-
now = time.time()
|
| 29 |
-
if now - _last_sweep < min_interval_sec:
|
| 30 |
-
return
|
| 31 |
-
_last_sweep = now
|
| 32 |
-
try:
|
| 33 |
-
cleanup_old(settings.SESSION_TTL_HOURS)
|
| 34 |
-
except Exception:
|
| 35 |
-
pass
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def session_dir(sid: str) -> str:
|
| 39 |
-
return os.path.join(settings.STORAGE_DIR, sid)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def sub_dir(sid: str, *parts: str) -> str:
|
| 43 |
-
d = os.path.join(session_dir(sid), *parts)
|
| 44 |
-
os.makedirs(d, exist_ok=True)
|
| 45 |
-
return d
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:
|
| 49 |
-
d = sub_dir(sid, folder)
|
| 50 |
-
safe = os.path.basename(filename).replace("/", "_")
|
| 51 |
-
path = os.path.join(d, safe)
|
| 52 |
-
with open(path, "wb") as f:
|
| 53 |
-
f.write(data)
|
| 54 |
-
return path
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def zip_paths(paths: list[str], zip_path: str, arcnames: list[str] | None = None) -> str:
|
| 58 |
-
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 59 |
-
for i, p in enumerate(paths):
|
| 60 |
-
arc = arcnames[i] if arcnames else os.path.basename(p)
|
| 61 |
-
zf.write(p, arcname=arc)
|
| 62 |
-
return zip_path
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def delete_session(sid: str) -> bool:
|
| 66 |
-
"""Xoá hẳn một phiên (gọi sau khi user đã tải kết quả về máy)."""
|
| 67 |
-
p = session_dir(sid)
|
| 68 |
-
if os.path.isdir(p):
|
| 69 |
-
shutil.rmtree(p, ignore_errors=True)
|
| 70 |
-
return True
|
| 71 |
-
return False
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
def cleanup_old(max_age_hours: float = 6):
|
| 75 |
-
"""Dọn các phiên tạm quá hạn (mặc định 6 giờ)."""
|
| 76 |
-
now = time.time()
|
| 77 |
-
root = settings.STORAGE_DIR
|
| 78 |
-
if not os.path.isdir(root):
|
| 79 |
-
return
|
| 80 |
-
for name in os.listdir(root):
|
| 81 |
-
p = os.path.join(root, name)
|
| 82 |
-
try:
|
| 83 |
-
if now - os.path.getmtime(p) > max_age_hours * 3600:
|
| 84 |
-
shutil.rmtree(p, ignore_errors=True)
|
| 85 |
-
except OSError:
|
| 86 |
-
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/core/jobs.py
DELETED
|
@@ -1,151 +0,0 @@
|
|
| 1 |
-
"""Quản lý job nền: trạng thái, log, tiến độ, và trạng thái từng item con.
|
| 2 |
-
|
| 3 |
-
Không phụ thuộc FastAPI -> test độc lập được. Dùng ThreadPool để chạy nền.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
from __future__ import annotations
|
| 7 |
-
|
| 8 |
-
import threading
|
| 9 |
-
import time
|
| 10 |
-
import traceback
|
| 11 |
-
import uuid
|
| 12 |
-
from concurrent.futures import ThreadPoolExecutor
|
| 13 |
-
from dataclasses import asdict, dataclass, field
|
| 14 |
-
from typing import Any, Callable
|
| 15 |
-
|
| 16 |
-
QUEUED, RUNNING, DONE, ERROR, CANCELLED = "queued", "running", "done", "error", "cancelled"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
@dataclass
|
| 20 |
-
class LogLine:
|
| 21 |
-
t: float
|
| 22 |
-
level: str
|
| 23 |
-
msg: str
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
@dataclass
|
| 27 |
-
class JobItem:
|
| 28 |
-
"""Một item con (vd: 1 link tải, 1 video dịch)."""
|
| 29 |
-
id: str
|
| 30 |
-
label: str
|
| 31 |
-
status: str = QUEUED # queued|running|done|error
|
| 32 |
-
detail: str = ""
|
| 33 |
-
error: str = ""
|
| 34 |
-
result: dict | None = None
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
@dataclass
|
| 38 |
-
class Job:
|
| 39 |
-
id: str
|
| 40 |
-
kind: str
|
| 41 |
-
status: str = QUEUED
|
| 42 |
-
progress: float = 0.0
|
| 43 |
-
logs: list[LogLine] = field(default_factory=list)
|
| 44 |
-
items: list[JobItem] = field(default_factory=list)
|
| 45 |
-
result: dict | None = None
|
| 46 |
-
error: str = ""
|
| 47 |
-
created_at: float = field(default_factory=time.time)
|
| 48 |
-
|
| 49 |
-
def to_dict(self) -> dict:
|
| 50 |
-
return {
|
| 51 |
-
"id": self.id,
|
| 52 |
-
"kind": self.kind,
|
| 53 |
-
"status": self.status,
|
| 54 |
-
"progress": round(self.progress, 4),
|
| 55 |
-
"logs": [asdict(l) for l in self.logs[-500:]],
|
| 56 |
-
"items": [asdict(i) for i in self.items],
|
| 57 |
-
"result": self.result,
|
| 58 |
-
"error": self.error,
|
| 59 |
-
"created_at": self.created_at,
|
| 60 |
-
"failed_items": [
|
| 61 |
-
{"id": i.id, "label": i.label, "error": i.error}
|
| 62 |
-
for i in self.items if i.status == ERROR
|
| 63 |
-
],
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
class JobHandle:
|
| 68 |
-
"""Trao cho hàm worker để cập nhật job một cách thread-safe."""
|
| 69 |
-
|
| 70 |
-
def __init__(self, manager: "JobManager", job: Job):
|
| 71 |
-
self._m = manager
|
| 72 |
-
self.job = job
|
| 73 |
-
|
| 74 |
-
def log(self, msg: str, level: str = "info"):
|
| 75 |
-
with self._m._lock:
|
| 76 |
-
self.job.logs.append(LogLine(time.time(), level, msg))
|
| 77 |
-
|
| 78 |
-
def set_progress(self, p: float):
|
| 79 |
-
with self._m._lock:
|
| 80 |
-
self.job.progress = max(0.0, min(1.0, p))
|
| 81 |
-
|
| 82 |
-
def add_item(self, label: str, item_id: str | None = None) -> JobItem:
|
| 83 |
-
it = JobItem(id=item_id or uuid.uuid4().hex[:8], label=label)
|
| 84 |
-
with self._m._lock:
|
| 85 |
-
self.job.items.append(it)
|
| 86 |
-
return it
|
| 87 |
-
|
| 88 |
-
def item_status(self, it: JobItem, status: str, detail: str = "",
|
| 89 |
-
error: str = "", result: dict | None = None):
|
| 90 |
-
with self._m._lock:
|
| 91 |
-
it.status = status
|
| 92 |
-
if detail:
|
| 93 |
-
it.detail = detail
|
| 94 |
-
if error:
|
| 95 |
-
it.error = error
|
| 96 |
-
if result is not None:
|
| 97 |
-
it.result = result
|
| 98 |
-
|
| 99 |
-
def cancelled(self) -> bool:
|
| 100 |
-
return self._m._cancel.get(self.job.id, False)
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
class JobManager:
|
| 104 |
-
def __init__(self, max_workers: int = 4):
|
| 105 |
-
self._jobs: dict[str, Job] = {}
|
| 106 |
-
self._lock = threading.RLock()
|
| 107 |
-
self._pool = ThreadPoolExecutor(max_workers=max_workers)
|
| 108 |
-
self._cancel: dict[str, bool] = {}
|
| 109 |
-
|
| 110 |
-
def create(self, kind: str) -> Job:
|
| 111 |
-
job = Job(id=uuid.uuid4().hex[:12], kind=kind)
|
| 112 |
-
with self._lock:
|
| 113 |
-
self._jobs[job.id] = job
|
| 114 |
-
return job
|
| 115 |
-
|
| 116 |
-
def get(self, job_id: str) -> Job | None:
|
| 117 |
-
return self._jobs.get(job_id)
|
| 118 |
-
|
| 119 |
-
def cancel(self, job_id: str):
|
| 120 |
-
self._cancel[job_id] = True
|
| 121 |
-
job = self.get(job_id)
|
| 122 |
-
if job and job.status in (QUEUED, RUNNING):
|
| 123 |
-
with self._lock:
|
| 124 |
-
job.status = CANCELLED
|
| 125 |
-
|
| 126 |
-
def submit(self, job: Job, fn: Callable[[JobHandle], Any]):
|
| 127 |
-
"""Chạy fn(handle) ở thread nền; tự set status/result/error."""
|
| 128 |
-
handle = JobHandle(self, job)
|
| 129 |
-
|
| 130 |
-
def runner():
|
| 131 |
-
with self._lock:
|
| 132 |
-
job.status = RUNNING
|
| 133 |
-
try:
|
| 134 |
-
result = fn(handle)
|
| 135 |
-
with self._lock:
|
| 136 |
-
if job.status != CANCELLED:
|
| 137 |
-
job.status = DONE
|
| 138 |
-
job.progress = 1.0
|
| 139 |
-
if isinstance(result, dict):
|
| 140 |
-
job.result = result
|
| 141 |
-
except Exception as e: # pragma: no cover - lỗi runtime
|
| 142 |
-
with self._lock:
|
| 143 |
-
job.status = ERROR
|
| 144 |
-
job.error = f"{e}"
|
| 145 |
-
handle.log(traceback.format_exc(), level="error")
|
| 146 |
-
|
| 147 |
-
self._pool.submit(runner)
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
# instance dùng chung toàn app
|
| 151 |
-
jobs = JobManager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/main.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
"""TNT Group - AI Content Create — FastAPI entrypoint.
|
| 2 |
-
|
| 3 |
-
Chạy (từ thư mục gốc project):
|
| 4 |
-
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
| 5 |
-
|
| 6 |
-
Cần Python 3.10+ và ffmpeg/ffprobe trong PATH. Các thư viện nặng
|
| 7 |
-
(yt-dlp, groq, google-generativeai, demucs, TTS...) chỉ cần khi dùng
|
| 8 |
-
tool tương ứng — xem requirements.txt.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import os
|
| 14 |
-
import sys
|
| 15 |
-
|
| 16 |
-
from fastapi import FastAPI
|
| 17 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
-
from fastapi.responses import RedirectResponse, FileResponse
|
| 19 |
-
from fastapi.staticfiles import StaticFiles
|
| 20 |
-
|
| 21 |
-
from .config import settings
|
| 22 |
-
from .core.files import cleanup_old
|
| 23 |
-
_here = os.path.dirname(os.path.abspath(__file__))
|
| 24 |
-
_parent = os.path.dirname(_here)
|
| 25 |
-
sys.path.insert(0, _parent)
|
| 26 |
-
|
| 27 |
-
from backend.app.api import common, download, cut, shuffle, dub
|
| 28 |
-
from .api import listing, video, auth
|
| 29 |
-
|
| 30 |
-
app = FastAPI(title="TNT Group - AI Content Create", version="1.0.0")
|
| 31 |
-
|
| 32 |
-
app.add_middleware(
|
| 33 |
-
CORSMiddleware,
|
| 34 |
-
allow_origins=settings.CORS_ORIGINS or ["*"],
|
| 35 |
-
allow_credentials=True,
|
| 36 |
-
allow_methods=["*"],
|
| 37 |
-
allow_headers=["*"],
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
app.include_router(common.router)
|
| 41 |
-
app.include_router(download.router)
|
| 42 |
-
app.include_router(cut.router)
|
| 43 |
-
app.include_router(shuffle.router)
|
| 44 |
-
app.include_router(dub.router)
|
| 45 |
-
app.include_router(listing.router)
|
| 46 |
-
app.include_router(video.router)
|
| 47 |
-
app.include_router(auth.router)
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@app.get("/api/health")
|
| 51 |
-
def health():
|
| 52 |
-
return {"ok": True, "name": "tnt-content-ai", "version": "1.0.0"}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
@app.get("/logo.png")
|
| 56 |
-
def logo_png():
|
| 57 |
-
return RedirectResponse(url="/logo.svg")
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
@app.on_event("startup")
|
| 61 |
-
def _startup():
|
| 62 |
-
os.makedirs(settings.STORAGE_DIR, exist_ok=True)
|
| 63 |
-
try:
|
| 64 |
-
cleanup_old(settings.SESSION_TTL_HOURS)
|
| 65 |
-
except Exception:
|
| 66 |
-
pass
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# Phục vụ frontend đã build (frontend/dist) nếu có — gắn CUỐI để không nuốt /api.
|
| 70 |
-
_here = os.path.dirname(os.path.abspath(__file__))
|
| 71 |
-
_candidates = [
|
| 72 |
-
os.path.join(_here, "..", "..", "frontend", "dist"), # local dev
|
| 73 |
-
os.path.join(_here, "..", "frontend", "dist"), # HF Spaces
|
| 74 |
-
"/app/frontend/dist", # Docker
|
| 75 |
-
]
|
| 76 |
-
_dist = ""
|
| 77 |
-
for p in _candidates:
|
| 78 |
-
p = os.path.abspath(p)
|
| 79 |
-
if os.path.isdir(p):
|
| 80 |
-
_dist = p
|
| 81 |
-
break
|
| 82 |
-
if _dist and os.path.isdir(_dist):
|
| 83 |
-
app.mount("/", StaticFiles(directory=_dist, html=True), name="frontend")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/cutter/__init__.py
DELETED
|
File without changes
|
app/services/cutter/cut.py
DELETED
|
@@ -1,85 +0,0 @@
|
|
| 1 |
-
"""Thực thi cắt: ffmpeg frame-accurate, KHÔNG TIẾNG (-an), thumbnail strip, zip."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import os
|
| 6 |
-
import shutil
|
| 7 |
-
import subprocess
|
| 8 |
-
import zipfile
|
| 9 |
-
from dataclasses import dataclass
|
| 10 |
-
|
| 11 |
-
from .modes import Segment
|
| 12 |
-
from .probe import FFMPEG, VideoInfo, probe
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
@dataclass
|
| 16 |
-
class ClipResult:
|
| 17 |
-
index: int
|
| 18 |
-
path: str
|
| 19 |
-
start_time: float
|
| 20 |
-
end_time: float
|
| 21 |
-
duration: float
|
| 22 |
-
size: int
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
def cut_segment(src: str, seg: Segment, out_path: str) -> ClipResult:
|
| 26 |
-
"""Cắt 1 khúc frame-accurate, re-encode (đảm bảo đúng frame, không thừa/thiếu),
|
| 27 |
-
loại bỏ audio (-an)."""
|
| 28 |
-
start = seg.start_frame / seg.fps
|
| 29 |
-
# dùng số frame để chốt độ dài chính xác thay vì -to (tránh sai số float)
|
| 30 |
-
nframes = seg.end_frame - seg.start_frame
|
| 31 |
-
cmd = [
|
| 32 |
-
FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 33 |
-
"-ss", f"{start:.6f}", "-i", src,
|
| 34 |
-
"-frames:v", str(nframes),
|
| 35 |
-
"-an", # KHÔNG TIẾNG
|
| 36 |
-
"-c:v", "libx264", "-preset", "veryfast", "-crf", "20",
|
| 37 |
-
"-pix_fmt", "yuv420p",
|
| 38 |
-
"-vsync", "cfr",
|
| 39 |
-
out_path,
|
| 40 |
-
]
|
| 41 |
-
subprocess.run(cmd, check=True, capture_output=True)
|
| 42 |
-
return ClipResult(
|
| 43 |
-
index=seg.index,
|
| 44 |
-
path=out_path,
|
| 45 |
-
start_time=round(seg.start_time, 3),
|
| 46 |
-
end_time=round(seg.end_time, 3),
|
| 47 |
-
duration=round(seg.duration, 3),
|
| 48 |
-
size=os.path.getsize(out_path),
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def cut_all(src: str, segments: list[Segment], out_dir: str,
|
| 53 |
-
prefix: str = "clip") -> list[ClipResult]:
|
| 54 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 55 |
-
results = []
|
| 56 |
-
for seg in segments:
|
| 57 |
-
name = f"{prefix}_{seg.index + 1:03d}.mp4"
|
| 58 |
-
results.append(cut_segment(src, seg, os.path.join(out_dir, name)))
|
| 59 |
-
return results
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
def thumbnail_strip(info: VideoInfo, out_dir: str, count: int = 40) -> list[dict]:
|
| 63 |
-
"""Sinh dải thumbnail để hiển thị nền timeline (preview điểm cắt)."""
|
| 64 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 65 |
-
count = max(2, count)
|
| 66 |
-
thumbs = []
|
| 67 |
-
for i in range(count):
|
| 68 |
-
t = info.duration * i / count
|
| 69 |
-
name = f"thumb_{i:03d}.jpg"
|
| 70 |
-
path = os.path.join(out_dir, name)
|
| 71 |
-
cmd = [
|
| 72 |
-
FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 73 |
-
"-ss", f"{t:.3f}", "-i", info.path,
|
| 74 |
-
"-frames:v", "1", "-vf", "scale=160:-1", path,
|
| 75 |
-
]
|
| 76 |
-
subprocess.run(cmd, check=True, capture_output=True)
|
| 77 |
-
thumbs.append({"time": round(t, 3), "path": path})
|
| 78 |
-
return thumbs
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def zip_clips(clips: list[ClipResult], zip_path: str) -> str:
|
| 82 |
-
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 83 |
-
for c in clips:
|
| 84 |
-
zf.write(c.path, arcname=os.path.basename(c.path))
|
| 85 |
-
return zip_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/cutter/modes.py
DELETED
|
@@ -1,265 +0,0 @@
|
|
| 1 |
-
"""4 chế độ cắt -> trả về danh sách KHÚC (start_frame, end_frame) frame-accurate.
|
| 2 |
-
|
| 3 |
-
- even : chia N khúc bằng nhau.
|
| 4 |
-
- scene : nhận diện CHUYỂN CẢNH (opencv hist-diff), khúc quá ngắn gộp lại.
|
| 5 |
-
- highlight : tìm đoạn CHUYỂN ĐỘNG MẠNH (motion energy), tách thành khúc.
|
| 6 |
-
- smart : dùng model CLIP cắt theo ngữ nghĩa (cần torch + open_clip; có fallback).
|
| 7 |
-
|
| 8 |
-
Boundary = (start_frame, end_frame) — end_frame exclusive theo frame index.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
from dataclasses import dataclass
|
| 14 |
-
|
| 15 |
-
import numpy as np
|
| 16 |
-
|
| 17 |
-
from .probe import VideoInfo
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
@dataclass
|
| 21 |
-
class Segment:
|
| 22 |
-
index: int
|
| 23 |
-
start_frame: int
|
| 24 |
-
end_frame: int # exclusive
|
| 25 |
-
fps: float
|
| 26 |
-
|
| 27 |
-
@property
|
| 28 |
-
def start_time(self) -> float:
|
| 29 |
-
return self.start_frame / self.fps
|
| 30 |
-
|
| 31 |
-
@property
|
| 32 |
-
def end_time(self) -> float:
|
| 33 |
-
return self.end_frame / self.fps
|
| 34 |
-
|
| 35 |
-
@property
|
| 36 |
-
def duration(self) -> float:
|
| 37 |
-
return (self.end_frame - self.start_frame) / self.fps
|
| 38 |
-
|
| 39 |
-
def to_dict(self) -> dict:
|
| 40 |
-
return {
|
| 41 |
-
"index": self.index,
|
| 42 |
-
"start_frame": self.start_frame,
|
| 43 |
-
"end_frame": self.end_frame,
|
| 44 |
-
"start_time": round(self.start_time, 3),
|
| 45 |
-
"end_time": round(self.end_time, 3),
|
| 46 |
-
"duration": round(self.duration, 3),
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def _segments_from_cuts(cut_frames: list[int], total: int, fps: float) -> list[Segment]:
|
| 51 |
-
"""cut_frames = các điểm cắt (frame index). Bao gồm 0 và total ở 2 đầu."""
|
| 52 |
-
pts = sorted(set([0, *[c for c in cut_frames if 0 < c < total], total]))
|
| 53 |
-
segs = []
|
| 54 |
-
for i in range(len(pts) - 1):
|
| 55 |
-
segs.append(Segment(i, pts[i], pts[i + 1], fps))
|
| 56 |
-
return segs
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
# ----------------------------------------------------------------- even ------
|
| 60 |
-
|
| 61 |
-
def even_cut(info: VideoInfo, n: int) -> list[Segment]:
|
| 62 |
-
n = max(1, n)
|
| 63 |
-
total = info.nb_frames
|
| 64 |
-
# chia đều theo frame, phần dư rải vào các khúc đầu để KHÔNG thừa/thiếu frame nào
|
| 65 |
-
base, rem = divmod(total, n)
|
| 66 |
-
cuts, acc = [], 0
|
| 67 |
-
for i in range(n):
|
| 68 |
-
acc += base + (1 if i < rem else 0)
|
| 69 |
-
cuts.append(acc)
|
| 70 |
-
return _segments_from_cuts(cuts[:-1], total, info.fps)
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def auto_segment_count(info: VideoInfo, target_len: float = 6.0) -> int:
|
| 74 |
-
"""Máy tự đề xuất số khúc dựa trên độ dài mục tiêu mỗi khúc (giây)."""
|
| 75 |
-
if info.duration <= 0:
|
| 76 |
-
return 1
|
| 77 |
-
return max(1, round(info.duration / max(0.5, target_len)))
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
# -------------------------------------------------------- scene / highlight --
|
| 81 |
-
|
| 82 |
-
def _frame_signals(path: str, max_dim: int = 160):
|
| 83 |
-
"""Quét video 1 lần, trả (hist_diff[], motion[]) theo từng frame (chuẩn hóa)."""
|
| 84 |
-
import cv2
|
| 85 |
-
|
| 86 |
-
cap = cv2.VideoCapture(path)
|
| 87 |
-
prev_gray = None
|
| 88 |
-
prev_hist = None
|
| 89 |
-
hist_diff: list[float] = [0.0]
|
| 90 |
-
motion: list[float] = [0.0]
|
| 91 |
-
|
| 92 |
-
while True:
|
| 93 |
-
ok, frame = cap.read()
|
| 94 |
-
if not ok:
|
| 95 |
-
break
|
| 96 |
-
h, w = frame.shape[:2]
|
| 97 |
-
scale = max_dim / max(h, w)
|
| 98 |
-
if scale < 1:
|
| 99 |
-
frame = cv2.resize(frame, (int(w * scale), int(h * scale)))
|
| 100 |
-
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 101 |
-
hist = cv2.calcHist([frame], [0, 1, 2], None, [8, 8, 8],
|
| 102 |
-
[0, 256, 0, 256, 0, 256])
|
| 103 |
-
cv2.normalize(hist, hist)
|
| 104 |
-
|
| 105 |
-
if prev_hist is not None:
|
| 106 |
-
d = cv2.compareHist(prev_hist, hist, cv2.HISTCMP_BHATTACHARYYA)
|
| 107 |
-
hist_diff.append(float(d))
|
| 108 |
-
m = float(np.mean(cv2.absdiff(gray, prev_gray)))
|
| 109 |
-
motion.append(m)
|
| 110 |
-
prev_gray, prev_hist = gray, hist
|
| 111 |
-
|
| 112 |
-
cap.release()
|
| 113 |
-
return np.array(hist_diff), np.array(motion)
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
def scene_cut(info: VideoInfo, min_len: float = 1.5, sensitivity: float = 0.5) -> list[Segment]:
|
| 117 |
-
"""Cắt tại các điểm chuyển cảnh; khúc < min_len gộp với khúc liền kề."""
|
| 118 |
-
hist_diff, _ = _frame_signals(info.path)
|
| 119 |
-
if len(hist_diff) < 2:
|
| 120 |
-
return even_cut(info, 1)
|
| 121 |
-
|
| 122 |
-
# ngưỡng = mean + k*std, k tỉ lệ nghịch với sensitivity
|
| 123 |
-
k = 3.0 - 2.0 * float(np.clip(sensitivity, 0, 1)) # sensitivity 1 -> k=1, 0 -> k=3
|
| 124 |
-
thr = hist_diff.mean() + k * hist_diff.std()
|
| 125 |
-
raw_cuts = [int(i) for i in np.where(hist_diff > thr)[0] if i > 0]
|
| 126 |
-
|
| 127 |
-
segs = _segments_from_cuts(raw_cuts, info.nb_frames, info.fps)
|
| 128 |
-
return _merge_short(segs, info.fps, min_len)
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
def highlight_cut(info: VideoInfo, top_ratio: float = 0.4,
|
| 132 |
-
min_len: float = 2.0, pad: float = 0.3) -> list[Segment]:
|
| 133 |
-
"""Giữ các đoạn CHUYỂN ĐỘNG MẠNH (cao trào) thành khúc riêng."""
|
| 134 |
-
_, motion = _frame_signals(info.path)
|
| 135 |
-
if len(motion) < 2:
|
| 136 |
-
return even_cut(info, 1)
|
| 137 |
-
|
| 138 |
-
thr = np.quantile(motion, 1.0 - float(np.clip(top_ratio, 0.05, 0.95)))
|
| 139 |
-
hot = motion >= thr
|
| 140 |
-
|
| 141 |
-
fps = info.fps
|
| 142 |
-
pad_f = int(pad * fps)
|
| 143 |
-
min_f = int(min_len * fps)
|
| 144 |
-
total = info.nb_frames
|
| 145 |
-
|
| 146 |
-
# gom các frame "nóng" liền nhau thành đoạn, nới biên pad, lọc đoạn ngắn
|
| 147 |
-
spans: list[tuple[int, int]] = []
|
| 148 |
-
i = 0
|
| 149 |
-
while i < len(hot):
|
| 150 |
-
if hot[i]:
|
| 151 |
-
j = i
|
| 152 |
-
while j < len(hot) and hot[j]:
|
| 153 |
-
j += 1
|
| 154 |
-
s = max(0, i - pad_f)
|
| 155 |
-
e = min(total, j + pad_f)
|
| 156 |
-
if e - s >= min_f:
|
| 157 |
-
spans.append((s, e))
|
| 158 |
-
i = j
|
| 159 |
-
else:
|
| 160 |
-
i += 1
|
| 161 |
-
|
| 162 |
-
if not spans:
|
| 163 |
-
return even_cut(info, max(1, auto_segment_count(info)))
|
| 164 |
-
|
| 165 |
-
# merge spans chồng lấn
|
| 166 |
-
spans.sort()
|
| 167 |
-
merged = [list(spans[0])]
|
| 168 |
-
for s, e in spans[1:]:
|
| 169 |
-
if s <= merged[-1][1]:
|
| 170 |
-
merged[-1][1] = max(merged[-1][1], e)
|
| 171 |
-
else:
|
| 172 |
-
merged.append([s, e])
|
| 173 |
-
|
| 174 |
-
return [Segment(i, s, e, fps) for i, (s, e) in enumerate(merged)]
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
def smart_cut(info: VideoInfo, n: int | None = None) -> list[Segment]:
|
| 178 |
-
"""Cắt theo ngữ nghĩa bằng CLIP. Cần `torch` + `open_clip_torch`.
|
| 179 |
-
|
| 180 |
-
Lấy embedding CLIP cho frame mẫu, phát hiện điểm ngữ nghĩa đổi mạnh
|
| 181 |
-
(cosine distance giữa frame liền kề vượt ngưỡng) -> điểm cắt.
|
| 182 |
-
Nếu thiếu thư viện/model -> fallback sang scene_cut.
|
| 183 |
-
"""
|
| 184 |
-
try:
|
| 185 |
-
import torch # noqa: F401
|
| 186 |
-
import open_clip # noqa: F401
|
| 187 |
-
except Exception:
|
| 188 |
-
# Fallback an toàn để luồng không vỡ khi chưa cài model.
|
| 189 |
-
return scene_cut(info)
|
| 190 |
-
|
| 191 |
-
return _smart_cut_clip(info, n)
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
def _smart_cut_clip(info: VideoInfo, n: int | None) -> list[Segment]: # pragma: no cover
|
| 195 |
-
import cv2
|
| 196 |
-
import numpy as np
|
| 197 |
-
import open_clip
|
| 198 |
-
import torch
|
| 199 |
-
|
| 200 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 201 |
-
model, _, preprocess = open_clip.create_model_and_transforms(
|
| 202 |
-
"ViT-B-32", pretrained="laion2b_s34b_b79k"
|
| 203 |
-
)
|
| 204 |
-
model = model.to(device).eval()
|
| 205 |
-
|
| 206 |
-
cap = cv2.VideoCapture(info.path)
|
| 207 |
-
fps = info.fps
|
| 208 |
-
step = max(1, int(fps)) # 1 frame/giây
|
| 209 |
-
feats, frame_idx = [], []
|
| 210 |
-
idx = 0
|
| 211 |
-
from PIL import Image
|
| 212 |
-
while True:
|
| 213 |
-
ok, frame = cap.read()
|
| 214 |
-
if not ok:
|
| 215 |
-
break
|
| 216 |
-
if idx % step == 0:
|
| 217 |
-
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 218 |
-
with torch.no_grad():
|
| 219 |
-
t = preprocess(img).unsqueeze(0).to(device)
|
| 220 |
-
f = model.encode_image(t)
|
| 221 |
-
f = f / f.norm(dim=-1, keepdim=True)
|
| 222 |
-
feats.append(f.cpu().numpy()[0])
|
| 223 |
-
frame_idx.append(idx)
|
| 224 |
-
idx += 1
|
| 225 |
-
cap.release()
|
| 226 |
-
|
| 227 |
-
if len(feats) < 2:
|
| 228 |
-
return scene_cut(info)
|
| 229 |
-
feats = np.array(feats)
|
| 230 |
-
dist = 1.0 - np.sum(feats[1:] * feats[:-1], axis=1)
|
| 231 |
-
thr = dist.mean() + dist.std()
|
| 232 |
-
cuts = [frame_idx[i + 1] for i in np.where(dist > thr)[0]]
|
| 233 |
-
segs = _segments_from_cuts(cuts, info.nb_frames, info.fps)
|
| 234 |
-
return _merge_short(segs, info.fps, 1.5)
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
# ----------------------------------------------------------------- helpers ---
|
| 238 |
-
|
| 239 |
-
def _merge_short(segs: list[Segment], fps: float, min_len: float) -> list[Segment]:
|
| 240 |
-
if not segs:
|
| 241 |
-
return segs
|
| 242 |
-
min_f = int(min_len * fps)
|
| 243 |
-
merged: list[Segment] = []
|
| 244 |
-
for s in segs:
|
| 245 |
-
if merged and (s.end_frame - merged[-1].start_frame) and \
|
| 246 |
-
(s.end_frame - s.start_frame) < min_f:
|
| 247 |
-
merged[-1] = Segment(merged[-1].index, merged[-1].start_frame,
|
| 248 |
-
s.end_frame, fps)
|
| 249 |
-
else:
|
| 250 |
-
merged.append(Segment(len(merged), s.start_frame, s.end_frame, fps))
|
| 251 |
-
# đánh số lại
|
| 252 |
-
return [Segment(i, m.start_frame, m.end_frame, fps) for i, m in enumerate(merged)]
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
def build_segments(info: VideoInfo, mode: str, **kw) -> list[Segment]:
|
| 256 |
-
if mode == "even":
|
| 257 |
-
n = kw.get("n") or auto_segment_count(info)
|
| 258 |
-
return even_cut(info, int(n))
|
| 259 |
-
if mode == "scene":
|
| 260 |
-
return scene_cut(info, kw.get("min_len", 1.5), kw.get("sensitivity", 0.5))
|
| 261 |
-
if mode == "highlight":
|
| 262 |
-
return highlight_cut(info, kw.get("top_ratio", 0.4), kw.get("min_len", 2.0))
|
| 263 |
-
if mode == "smart":
|
| 264 |
-
return smart_cut(info, kw.get("n"))
|
| 265 |
-
raise ValueError(f"unknown mode: {mode}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/cutter/probe.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 1 |
-
"""Đọc metadata video bằng ffprobe để cắt frame-accurate."""
|
| 2 |
-
|
| 3 |
-
from __future__ import annotations
|
| 4 |
-
|
| 5 |
-
import json
|
| 6 |
-
import shutil
|
| 7 |
-
import subprocess
|
| 8 |
-
from dataclasses import dataclass
|
| 9 |
-
|
| 10 |
-
FFPROBE = shutil.which("ffprobe") or "ffprobe"
|
| 11 |
-
FFMPEG = shutil.which("ffmpeg") or "ffmpeg"
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
@dataclass
|
| 15 |
-
class VideoInfo:
|
| 16 |
-
path: str
|
| 17 |
-
duration: float # giây
|
| 18 |
-
fps: float # khung hình/giây (đã quy đổi từ r_frame_rate)
|
| 19 |
-
nb_frames: int # tổng số frame (ước lượng nếu thiếu)
|
| 20 |
-
width: int
|
| 21 |
-
height: int
|
| 22 |
-
|
| 23 |
-
@property
|
| 24 |
-
def frame_dur(self) -> float:
|
| 25 |
-
return 1.0 / self.fps if self.fps else 0.0
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
def _parse_rate(rate: str) -> float:
|
| 29 |
-
if not rate or rate == "0/0":
|
| 30 |
-
return 0.0
|
| 31 |
-
if "/" in rate:
|
| 32 |
-
num, den = rate.split("/")
|
| 33 |
-
den = float(den)
|
| 34 |
-
return float(num) / den if den else 0.0
|
| 35 |
-
return float(rate)
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
def probe(path: str) -> VideoInfo:
|
| 39 |
-
cmd = [
|
| 40 |
-
FFPROBE, "-v", "error", "-print_format", "json",
|
| 41 |
-
"-show_streams", "-show_format", path,
|
| 42 |
-
]
|
| 43 |
-
out = subprocess.run(cmd, capture_output=True, text=True, check=True).stdout
|
| 44 |
-
data = json.loads(out)
|
| 45 |
-
|
| 46 |
-
vstream = next(s for s in data["streams"] if s["codec_type"] == "video")
|
| 47 |
-
fps = _parse_rate(vstream.get("avg_frame_rate") or "") or _parse_rate(
|
| 48 |
-
vstream.get("r_frame_rate") or ""
|
| 49 |
-
)
|
| 50 |
-
duration = float(
|
| 51 |
-
vstream.get("duration") or data.get("format", {}).get("duration") or 0.0
|
| 52 |
-
)
|
| 53 |
-
nb = vstream.get("nb_frames")
|
| 54 |
-
nb_frames = int(nb) if nb and nb.isdigit() else int(round(duration * fps))
|
| 55 |
-
|
| 56 |
-
return VideoInfo(
|
| 57 |
-
path=path,
|
| 58 |
-
duration=duration,
|
| 59 |
-
fps=fps or 30.0,
|
| 60 |
-
nb_frames=nb_frames,
|
| 61 |
-
width=int(vstream.get("width", 0)),
|
| 62 |
-
height=int(vstream.get("height", 0)),
|
| 63 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/downloader/__init__.py
DELETED
|
File without changes
|
app/services/downloader/ydl.py
DELETED
|
@@ -1,168 +0,0 @@
|
|
| 1 |
-
"""Tải video hàng loạt từ nhiều nguồn bằng yt-dlp.
|
| 2 |
-
|
| 3 |
-
Hỗ trợ: YouTube, Facebook, TikTok, Douyin, Shopee, 1688, Taobao, ...
|
| 4 |
-
Tải đúng CHẤT LƯỢNG GỐC như trên link.
|
| 5 |
-
|
| 6 |
-
Lưu ý môi trường:
|
| 7 |
-
- Cần `pip install yt-dlp` và có mạng.
|
| 8 |
-
- Một số nguồn (Shopee/1688/Taobao) có thể cần cookies; truyền qua `cookies_file`.
|
| 9 |
-
"""
|
| 10 |
-
|
| 11 |
-
from __future__ import annotations
|
| 12 |
-
|
| 13 |
-
import os
|
| 14 |
-
import re
|
| 15 |
-
from dataclasses import dataclass
|
| 16 |
-
from typing import Callable
|
| 17 |
-
|
| 18 |
-
URL_RE = re.compile(r"https?://[^\s]+")
|
| 19 |
-
ANSI_RE = re.compile(r"\x1b\[[0-9;]*m")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@dataclass
|
| 23 |
-
class DownloadResult:
|
| 24 |
-
url: str
|
| 25 |
-
ok: bool
|
| 26 |
-
path: str = ""
|
| 27 |
-
title: str = ""
|
| 28 |
-
ext: str = ""
|
| 29 |
-
size: int = 0
|
| 30 |
-
duration: float = 0.0
|
| 31 |
-
thumbnail: str = ""
|
| 32 |
-
error: str = ""
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def clean_error(s) -> str:
|
| 36 |
-
"""Bỏ mã màu ANSI + tiền tố ERROR, rút gọn để hiển thị gọn gàng."""
|
| 37 |
-
s = ANSI_RE.sub("", str(s or ""))
|
| 38 |
-
s = s.replace("ERROR:", "").replace("\n", " ").strip()
|
| 39 |
-
return (s[:240] + "…") if len(s) > 240 else s
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def normalize_url(url: str) -> str:
|
| 43 |
-
"""Chuẩn hóa một số dạng link để yt-dlp tải được.
|
| 44 |
-
|
| 45 |
-
- Douyin 'jingxuan?modal_id=ID' / '?modal_id=ID' -> '/video/ID'
|
| 46 |
-
- Bỏ các tham số rác phía sau cho douyin video.
|
| 47 |
-
"""
|
| 48 |
-
u = (url or "").strip()
|
| 49 |
-
m = re.search(r"douyin\.com/.*?[?&]modal_id=(\d+)", u)
|
| 50 |
-
if m:
|
| 51 |
-
return f"https://www.douyin.com/video/{m.group(1)}"
|
| 52 |
-
m = re.search(r"douyin\.com/video/(\d+)", u)
|
| 53 |
-
if m:
|
| 54 |
-
return f"https://www.douyin.com/video/{m.group(1)}"
|
| 55 |
-
return u
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
def parse_links(raw: str, max_links: int = 200) -> list[str]:
|
| 59 |
-
"""Tách danh sách link: mỗi dòng 1 link, chuẩn hóa, bỏ trùng, giữ thứ tự."""
|
| 60 |
-
seen, out = set(), []
|
| 61 |
-
for line in raw.splitlines():
|
| 62 |
-
line = line.strip()
|
| 63 |
-
if not line:
|
| 64 |
-
continue
|
| 65 |
-
m = URL_RE.search(line)
|
| 66 |
-
url = m.group(0) if m else line
|
| 67 |
-
url = normalize_url(url)
|
| 68 |
-
if url not in seen:
|
| 69 |
-
seen.add(url)
|
| 70 |
-
out.append(url)
|
| 71 |
-
if len(out) >= max_links:
|
| 72 |
-
break
|
| 73 |
-
return out
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
def detect_source(url: str) -> str:
|
| 77 |
-
u = url.lower()
|
| 78 |
-
table = {
|
| 79 |
-
"youtube": ["youtube.com", "youtu.be"],
|
| 80 |
-
"tiktok": ["tiktok.com"],
|
| 81 |
-
"douyin": ["douyin.com", "iesdouyin.com"],
|
| 82 |
-
"facebook": ["facebook.com", "fb.watch", "fb.com"],
|
| 83 |
-
"shopee": ["shopee."],
|
| 84 |
-
"1688": ["1688.com"],
|
| 85 |
-
"taobao": ["taobao.com", "tmall.com"],
|
| 86 |
-
"drive": ["drive.google.com"],
|
| 87 |
-
"instagram": ["instagram.com"],
|
| 88 |
-
}
|
| 89 |
-
for name, keys in table.items():
|
| 90 |
-
if any(k in u for k in keys):
|
| 91 |
-
return name
|
| 92 |
-
return "other"
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
def _build_opts(out_dir: str, cookies_file: str | None, generic: bool = False) -> dict:
|
| 96 |
-
opts = {
|
| 97 |
-
"outtmpl": os.path.join(out_dir, "%(title).80s [%(id)s].%(ext)s"),
|
| 98 |
-
"format": "bv*+ba/b",
|
| 99 |
-
"merge_output_format": "mp4",
|
| 100 |
-
"noplaylist": True,
|
| 101 |
-
"quiet": True,
|
| 102 |
-
"no_warnings": True,
|
| 103 |
-
"retries": 3,
|
| 104 |
-
"concurrent_fragment_downloads": 4,
|
| 105 |
-
"writethumbnail": False,
|
| 106 |
-
}
|
| 107 |
-
if generic:
|
| 108 |
-
# thử quét video nhúng trong trang (Shopee/1688/Taobao... không có extractor riêng)
|
| 109 |
-
opts["force_generic_extractor"] = True
|
| 110 |
-
if cookies_file and os.path.exists(cookies_file):
|
| 111 |
-
opts["cookiefile"] = cookies_file
|
| 112 |
-
return opts
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
def _attempt(url: str, out_dir: str, opts: dict) -> DownloadResult:
|
| 116 |
-
import yt_dlp # type: ignore
|
| 117 |
-
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 118 |
-
info = ydl.extract_info(url, download=True)
|
| 119 |
-
path = ydl.prepare_filename(info)
|
| 120 |
-
base, _ = os.path.splitext(path)
|
| 121 |
-
if not os.path.exists(path) and os.path.exists(base + ".mp4"):
|
| 122 |
-
path = base + ".mp4"
|
| 123 |
-
size = os.path.getsize(path) if os.path.exists(path) else 0
|
| 124 |
-
return DownloadResult(
|
| 125 |
-
url=url, ok=True, path=path,
|
| 126 |
-
title=info.get("title", "") or os.path.basename(base),
|
| 127 |
-
ext=os.path.splitext(path)[1].lstrip("."),
|
| 128 |
-
size=size,
|
| 129 |
-
duration=float(info.get("duration") or 0),
|
| 130 |
-
thumbnail=info.get("thumbnail", "") or "",
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
def download_one(url: str, out_dir: str, cookies_file: str | None = None) -> DownloadResult:
|
| 135 |
-
try:
|
| 136 |
-
import yt_dlp # noqa: F401 # type: ignore
|
| 137 |
-
except Exception as e: # pragma: no cover
|
| 138 |
-
return DownloadResult(url=url, ok=False,
|
| 139 |
-
error=clean_error(f"yt-dlp chưa được cài đặt: {e}"))
|
| 140 |
-
|
| 141 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 142 |
-
url = normalize_url(url)
|
| 143 |
-
source = detect_source(url)
|
| 144 |
-
try:
|
| 145 |
-
return _attempt(url, out_dir, _build_opts(out_dir, cookies_file))
|
| 146 |
-
except Exception as e: # pragma: no cover - lỗi mạng/extractor
|
| 147 |
-
first_err = clean_error(e)
|
| 148 |
-
# Shopee/1688/Taobao: thử lại bằng generic extractor (quét video trong trang)
|
| 149 |
-
if source in ("shopee", "1688", "taobao"):
|
| 150 |
-
try:
|
| 151 |
-
return _attempt(url, out_dir, _build_opts(out_dir, cookies_file, generic=True))
|
| 152 |
-
except Exception as e2:
|
| 153 |
-
return DownloadResult(url=url, ok=False,
|
| 154 |
-
error=clean_error(f"{source}: {e2}"))
|
| 155 |
-
return DownloadResult(url=url, ok=False, error=first_err)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
def download_many(urls: list[str], out_dir: str,
|
| 159 |
-
cookies_file: str | None = None,
|
| 160 |
-
on_item: Callable[[int, DownloadResult], None] | None = None
|
| 161 |
-
) -> list[DownloadResult]:
|
| 162 |
-
results = []
|
| 163 |
-
for i, url in enumerate(urls):
|
| 164 |
-
res = download_one(url, out_dir, cookies_file)
|
| 165 |
-
results.append(res)
|
| 166 |
-
if on_item:
|
| 167 |
-
on_item(i, res)
|
| 168 |
-
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/__init__.py
DELETED
|
File without changes
|
app/services/dubber/asr.py
DELETED
|
@@ -1,86 +0,0 @@
|
|
| 1 |
-
"""Bước NHẬN DẠNG (ASR) — Whisper large-v3 qua API Groq.
|
| 2 |
-
|
| 3 |
-
- response_format=verbose_json, lấy segment kèm timestamp + no_speech_prob + avg_logprob.
|
| 4 |
-
- Lọc đoạn không phải giọng nói thật; nếu lọc sạch hết thì GIỮ NGUYÊN để không mất nội dung.
|
| 5 |
-
- Gộp các mảnh vụn thành CÂU trọn vẹn để ngữ điệu mượt & ngân sách thời gian có nghĩa.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from __future__ import annotations
|
| 9 |
-
|
| 10 |
-
import re
|
| 11 |
-
from dataclasses import dataclass
|
| 12 |
-
|
| 13 |
-
from ...config import settings
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
@dataclass
|
| 17 |
-
class Segment:
|
| 18 |
-
start: float
|
| 19 |
-
end: float
|
| 20 |
-
text: str
|
| 21 |
-
|
| 22 |
-
@property
|
| 23 |
-
def dur(self) -> float:
|
| 24 |
-
return max(0.0, self.end - self.start)
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def transcribe(asr_wav: str, model: str | None = None) -> tuple[list[Segment], str]:
|
| 28 |
-
"""Trả (segments, language). Cần GROQ_API_KEY và `pip install groq`."""
|
| 29 |
-
from groq import Groq # type: ignore
|
| 30 |
-
|
| 31 |
-
client = Groq(api_key=settings.GROQ_API_KEY)
|
| 32 |
-
with open(asr_wav, "rb") as f:
|
| 33 |
-
resp = client.audio.transcriptions.create(
|
| 34 |
-
file=(asr_wav, f.read()),
|
| 35 |
-
model=model or settings.WHISPER_MODEL,
|
| 36 |
-
response_format="verbose_json",
|
| 37 |
-
temperature=0.0,
|
| 38 |
-
)
|
| 39 |
-
data = resp.model_dump() if hasattr(resp, "model_dump") else resp
|
| 40 |
-
language = data.get("language", "")
|
| 41 |
-
raw = data.get("segments", []) or []
|
| 42 |
-
|
| 43 |
-
kept = _filter_speech(raw)
|
| 44 |
-
if not kept: # đừng để mất nội dung
|
| 45 |
-
kept = [Segment(s["start"], s["end"], s["text"].strip()) for s in raw]
|
| 46 |
-
sentences = merge_into_sentences(kept)
|
| 47 |
-
return sentences, language
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def _filter_speech(raw: list[dict],
|
| 51 |
-
no_speech_max: float = 0.6,
|
| 52 |
-
logprob_min: float = -1.0) -> list[Segment]:
|
| 53 |
-
out = []
|
| 54 |
-
for s in raw:
|
| 55 |
-
text = (s.get("text") or "").strip()
|
| 56 |
-
if not text:
|
| 57 |
-
continue
|
| 58 |
-
if s.get("no_speech_prob", 0.0) > no_speech_max:
|
| 59 |
-
continue
|
| 60 |
-
if s.get("avg_logprob", 0.0) < logprob_min:
|
| 61 |
-
continue
|
| 62 |
-
out.append(Segment(float(s["start"]), float(s["end"]), text))
|
| 63 |
-
return out
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
_SENT_END = re.compile(r"[.!?。!?…]['\"”’)]?\s*$")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
def merge_into_sentences(segs: list[Segment], max_gap: float = 0.6,
|
| 70 |
-
max_dur: float = 12.0) -> list[Segment]:
|
| 71 |
-
"""Gộp các mảnh Whisper thành câu trọn vẹn."""
|
| 72 |
-
if not segs:
|
| 73 |
-
return []
|
| 74 |
-
out: list[Segment] = []
|
| 75 |
-
cur = Segment(segs[0].start, segs[0].end, segs[0].text)
|
| 76 |
-
for nxt in segs[1:]:
|
| 77 |
-
gap = nxt.start - cur.end
|
| 78 |
-
ends_sentence = bool(_SENT_END.search(cur.text))
|
| 79 |
-
too_long = (nxt.end - cur.start) > max_dur
|
| 80 |
-
if ends_sentence or gap > max_gap or too_long:
|
| 81 |
-
out.append(cur)
|
| 82 |
-
cur = Segment(nxt.start, nxt.end, nxt.text)
|
| 83 |
-
else:
|
| 84 |
-
cur = Segment(cur.start, nxt.end, (cur.text + " " + nxt.text).strip())
|
| 85 |
-
out.append(cur)
|
| 86 |
-
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/audio.py
DELETED
|
@@ -1,114 +0,0 @@
|
|
| 1 |
-
"""Bước AUDIO của pipeline lồng tiếng.
|
| 2 |
-
|
| 3 |
-
- Tách audio ra 2 bản: 16kHz mono (cho ASR) và 44.1kHz stereo (giữ chất gốc làm nền).
|
| 4 |
-
- Xử lý nền nhạc 3 chế độ: none | duck | demucs.
|
| 5 |
-
- Cắt mẫu clone giọng (~10-20s) từ lúc bắt đầu nói (ưu tiên bản giọng tách sạch).
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from __future__ import annotations
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
import shutil
|
| 12 |
-
import subprocess
|
| 13 |
-
from dataclasses import dataclass
|
| 14 |
-
|
| 15 |
-
FFMPEG = shutil.which("ffmpeg") or "ffmpeg"
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
@dataclass
|
| 19 |
-
class AudioBundle:
|
| 20 |
-
asr_wav: str # 16k mono cho Whisper
|
| 21 |
-
full_wav: str # 44.1k stereo gốc
|
| 22 |
-
bg_wav: str # nền nhạc theo chế độ (none -> im lặng)
|
| 23 |
-
voice_wav: str # giọng đã tách (demucs) hoặc bản gốc
|
| 24 |
-
mode: str
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def _run(cmd: list[str]):
|
| 28 |
-
subprocess.run(cmd, check=True, capture_output=True)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def extract_tracks(video: str, out_dir: str) -> tuple[str, str]:
|
| 32 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 33 |
-
asr_wav = os.path.join(out_dir, "asr_16k_mono.wav")
|
| 34 |
-
full_wav = os.path.join(out_dir, "full_44k_stereo.wav")
|
| 35 |
-
_run([FFMPEG, "-y", "-hide_banner", "-loglevel", "error", "-i", video,
|
| 36 |
-
"-vn", "-ac", "1", "-ar", "16000", asr_wav])
|
| 37 |
-
_run([FFMPEG, "-y", "-hide_banner", "-loglevel", "error", "-i", video,
|
| 38 |
-
"-vn", "-ac", "2", "-ar", "44100", full_wav])
|
| 39 |
-
return asr_wav, full_wav
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def _silence_like(ref_wav: str, out: str):
|
| 43 |
-
"""Tạo track im lặng dài bằng ref (cho chế độ none)."""
|
| 44 |
-
dur = _duration(ref_wav)
|
| 45 |
-
_run([FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 46 |
-
"-f", "lavfi", "-i", f"anullsrc=r=44100:cl=stereo",
|
| 47 |
-
"-t", f"{dur:.3f}", out])
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def _duration(wav: str) -> float:
|
| 51 |
-
out = subprocess.run(
|
| 52 |
-
["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 53 |
-
"-of", "csv=p=0", wav], capture_output=True, text=True).stdout.strip()
|
| 54 |
-
try:
|
| 55 |
-
return float(out)
|
| 56 |
-
except ValueError:
|
| 57 |
-
return 0.0
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
def _demucs_separate(full_wav: str, out_dir: str, model: str) -> tuple[str, str]:
|
| 61 |
-
"""Tách nhạc & giọng bằng Demucs. Trả (bg_wav, voice_wav).
|
| 62 |
-
|
| 63 |
-
Cần `pip install demucs`. Nếu thiếu -> raise để pipeline fallback duck.
|
| 64 |
-
"""
|
| 65 |
-
import demucs.separate # type: ignore # noqa: F401
|
| 66 |
-
|
| 67 |
-
sep_dir = os.path.join(out_dir, "demucs")
|
| 68 |
-
os.makedirs(sep_dir, exist_ok=True)
|
| 69 |
-
subprocess.run(
|
| 70 |
-
["python", "-m", "demucs", "-n", model, "--two-stems", "vocals",
|
| 71 |
-
"-o", sep_dir, full_wav], check=True, capture_output=True)
|
| 72 |
-
stem_root = os.path.join(sep_dir, model,
|
| 73 |
-
os.path.splitext(os.path.basename(full_wav))[0])
|
| 74 |
-
vocals = os.path.join(stem_root, "vocals.wav")
|
| 75 |
-
no_vocals = os.path.join(stem_root, "no_vocals.wav")
|
| 76 |
-
return no_vocals, vocals
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def prepare_background(full_wav: str, out_dir: str, mode: str,
|
| 80 |
-
demucs_model: str = "htdemucs") -> tuple[str, str]:
|
| 81 |
-
"""Trả (bg_wav, voice_wav) theo chế độ nền."""
|
| 82 |
-
bg = os.path.join(out_dir, "bg.wav")
|
| 83 |
-
if mode == "none":
|
| 84 |
-
_silence_like(full_wav, bg)
|
| 85 |
-
return bg, full_wav
|
| 86 |
-
if mode == "duck":
|
| 87 |
-
# giảm tiếng gốc ~ -12dB làm nền mờ
|
| 88 |
-
_run([FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 89 |
-
"-i", full_wav, "-af", "volume=-12dB", bg])
|
| 90 |
-
return bg, full_wav
|
| 91 |
-
if mode == "demucs":
|
| 92 |
-
try:
|
| 93 |
-
return _demucs_separate(full_wav, out_dir, demucs_model)
|
| 94 |
-
except Exception:
|
| 95 |
-
# fallback an toàn -> duck
|
| 96 |
-
return prepare_background(full_wav, out_dir, "duck")
|
| 97 |
-
raise ValueError(f"bg mode không hợp lệ: {mode}")
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def cut_clone_sample(voice_wav: str, out_dir: str, start: float = 0.0,
|
| 101 |
-
length: float = 15.0) -> str:
|
| 102 |
-
"""Cắt ~10-20s từ lúc bắt đầu nói làm mẫu clone."""
|
| 103 |
-
sample = os.path.join(out_dir, "clone_sample.wav")
|
| 104 |
-
_run([FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 105 |
-
"-ss", f"{start:.3f}", "-i", voice_wav, "-t", f"{length:.3f}",
|
| 106 |
-
"-ac", "1", "-ar", "24000", sample])
|
| 107 |
-
return sample
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
def build_audio(video: str, out_dir: str, bg_mode: str,
|
| 111 |
-
demucs_model: str = "htdemucs") -> AudioBundle:
|
| 112 |
-
asr_wav, full_wav = extract_tracks(video, out_dir)
|
| 113 |
-
bg_wav, voice_wav = prepare_background(full_wav, out_dir, bg_mode, demucs_model)
|
| 114 |
-
return AudioBundle(asr_wav, full_wav, bg_wav, voice_wav, bg_mode)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/mux.py
DELETED
|
@@ -1,127 +0,0 @@
|
|
| 1 |
-
"""Bước GHÉP (mux).
|
| 2 |
-
|
| 3 |
-
- Neo mỗi câu vào ĐÚNG mốc thời gian gốc (không dồn-đẩy gây trôi tích lũy).
|
| 4 |
-
- Tinh chỉnh phần dư bằng atempo có KẸP TRẦN để không méo tiếng.
|
| 5 |
-
- Ép câu cuối nằm gọn trong phần video còn lại (không mất đuôi).
|
| 6 |
-
- Trộn các câu đã đọc LÊN TRÊN nhạc nền rồi mux vào luồng hình gốc.
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
from __future__ import annotations
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
import shutil
|
| 13 |
-
import subprocess
|
| 14 |
-
from dataclasses import dataclass
|
| 15 |
-
|
| 16 |
-
FFMPEG = shutil.which("ffmpeg") or "ffmpeg"
|
| 17 |
-
ATEMPO_MAX = 1.5 # trần tăng tốc (không méo)
|
| 18 |
-
ATEMPO_MIN = 0.7 # sàn (chậm lại)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
def wav_duration(path: str) -> float:
|
| 22 |
-
out = subprocess.run(
|
| 23 |
-
["ffprobe", "-v", "error", "-show_entries", "format=duration",
|
| 24 |
-
"-of", "csv=p=0", path], capture_output=True, text=True).stdout.strip()
|
| 25 |
-
try:
|
| 26 |
-
return float(out)
|
| 27 |
-
except ValueError:
|
| 28 |
-
return 0.0
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
def _atempo_chain(ratio: float) -> str:
|
| 32 |
-
"""ffmpeg atempo chỉ nhận 0.5..2.0; ghép nhiều bộ lọc nếu cần."""
|
| 33 |
-
ratio = max(0.25, min(4.0, ratio))
|
| 34 |
-
parts = []
|
| 35 |
-
while ratio > 2.0:
|
| 36 |
-
parts.append("atempo=2.0")
|
| 37 |
-
ratio /= 2.0
|
| 38 |
-
while ratio < 0.5:
|
| 39 |
-
parts.append("atempo=0.5")
|
| 40 |
-
ratio /= 0.5
|
| 41 |
-
parts.append(f"atempo={ratio:.4f}")
|
| 42 |
-
return ",".join(parts)
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
def fit_to_slot(wav: str, target_dur: float, out: str,
|
| 46 |
-
cap: float = ATEMPO_MAX, floor: float = ATEMPO_MIN) -> str:
|
| 47 |
-
"""Ép câu vào khe target_dur bằng atempo có kẹp trần/sàn.
|
| 48 |
-
|
| 49 |
-
- Nếu đọc dài hơn khe: tăng tốc (ratio = dur/target) nhưng không quá cap.
|
| 50 |
-
- Nếu ngắn hơn: giữ nguyên (sẽ chèn lặng khi assemble), không kéo dãn gây méo.
|
| 51 |
-
"""
|
| 52 |
-
dur = wav_duration(wav)
|
| 53 |
-
if target_dur <= 0 or dur <= 0:
|
| 54 |
-
shutil.copy(wav, out)
|
| 55 |
-
return out
|
| 56 |
-
ratio = dur / target_dur
|
| 57 |
-
if ratio <= 1.0:
|
| 58 |
-
shutil.copy(wav, out) # vừa hoặc dư khe -> giữ nguyên
|
| 59 |
-
return out
|
| 60 |
-
ratio = min(ratio, cap) # KẸP TRẦN
|
| 61 |
-
subprocess.run(
|
| 62 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error", "-i", wav,
|
| 63 |
-
"-filter:a", _atempo_chain(ratio), out], check=True, capture_output=True)
|
| 64 |
-
return out
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
@dataclass
|
| 68 |
-
class PlacedLine:
|
| 69 |
-
start: float
|
| 70 |
-
wav: str
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def assemble_voice(lines: list[PlacedLine], total_dur: float, out_wav: str) -> str:
|
| 74 |
-
"""Đặt từng câu đúng mốc start trên 1 track dài total_dur (anchor tuyệt đối)."""
|
| 75 |
-
if not lines:
|
| 76 |
-
subprocess.run(
|
| 77 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 78 |
-
"-f", "lavfi", "-i", "anullsrc=r=44100:cl=stereo",
|
| 79 |
-
"-t", f"{total_dur:.3f}", out_wav], check=True, capture_output=True)
|
| 80 |
-
return out_wav
|
| 81 |
-
|
| 82 |
-
# input 0 = track nền im lặng dài ĐÚNG total_dur làm đáy (đảm bảo đủ độ dài)
|
| 83 |
-
inputs = ["-f", "lavfi", "-i",
|
| 84 |
-
f"anullsrc=r=44100:cl=stereo:d={total_dur:.3f}"]
|
| 85 |
-
filters, amix_in = ["[0:a]aformat=channel_layouts=stereo[base]"], ["[base]"]
|
| 86 |
-
for i, ln in enumerate(lines):
|
| 87 |
-
idx = i + 1
|
| 88 |
-
inputs += ["-i", ln.wav]
|
| 89 |
-
delay_ms = int(max(0.0, ln.start) * 1000)
|
| 90 |
-
filters.append(
|
| 91 |
-
f"[{idx}:a]aresample=44100,aformat=channel_layouts=stereo,"
|
| 92 |
-
f"adelay={delay_ms}|{delay_ms}[a{idx}]")
|
| 93 |
-
amix_in.append(f"[a{idx}]")
|
| 94 |
-
|
| 95 |
-
n = len(lines) + 1
|
| 96 |
-
filtergraph = ";".join(filters) + ";" + "".join(amix_in) + \
|
| 97 |
-
f"amix=inputs={n}:normalize=0:duration=longest[vox]"
|
| 98 |
-
|
| 99 |
-
subprocess.run(
|
| 100 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error", *inputs,
|
| 101 |
-
"-filter_complex", filtergraph, "-map", "[vox]",
|
| 102 |
-
"-t", f"{total_dur:.3f}", out_wav], check=True, capture_output=True)
|
| 103 |
-
return out_wav
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
def mix_over_bg(voice_wav: str, bg_wav: str, out_wav: str) -> str:
|
| 107 |
-
"""Trộn giọng dịch lên trên nhạc nền."""
|
| 108 |
-
subprocess.run(
|
| 109 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 110 |
-
"-i", bg_wav, "-i", voice_wav,
|
| 111 |
-
"-filter_complex",
|
| 112 |
-
"[0:a]aresample=44100,aformat=channel_layouts=stereo[bg];"
|
| 113 |
-
"[1:a]aresample=44100,aformat=channel_layouts=stereo[vx];"
|
| 114 |
-
"[bg][vx]amix=inputs=2:normalize=0:duration=longest[mix]",
|
| 115 |
-
"-map", "[mix]", out_wav], check=True, capture_output=True)
|
| 116 |
-
return out_wav
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
def mux_to_video(video: str, audio_wav: str, out_mp4: str) -> str:
|
| 120 |
-
"""Ghép audio mới vào luồng hình gốc (giữ nguyên video)."""
|
| 121 |
-
subprocess.run(
|
| 122 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 123 |
-
"-i", video, "-i", audio_wav,
|
| 124 |
-
"-map", "0:v:0", "-map", "1:a:0",
|
| 125 |
-
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
|
| 126 |
-
"-shortest", out_mp4], check=True, capture_output=True)
|
| 127 |
-
return out_mp4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/pipeline.py
DELETED
|
@@ -1,214 +0,0 @@
|
|
| 1 |
-
"""Nối toàn bộ pipeline lồng tiếng cho 1 video.
|
| 2 |
-
|
| 3 |
-
Luồng: tách audio -> nền nhạc -> ASR (Whisper/Groq) -> dịch (Gemini/Groq) ->
|
| 4 |
-
[clone] -> TTS theo ngôn ngữ đích -> neo mốc + atempo -> trộn nền -> mux video.
|
| 5 |
-
Trả về đường dẫn: video đã dịch, audio giọng clone, subtitle SRT.
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
from __future__ import annotations
|
| 9 |
-
|
| 10 |
-
import os
|
| 11 |
-
from dataclasses import dataclass
|
| 12 |
-
from typing import Callable
|
| 13 |
-
|
| 14 |
-
from . import asr, audio, mux, translate, tts
|
| 15 |
-
from .probe_dur import video_duration
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
@dataclass
|
| 19 |
-
class DubOptions:
|
| 20 |
-
target_lang: str # en | vi | fil
|
| 21 |
-
bg_mode: str = "duck" # none | duck | demucs
|
| 22 |
-
do_clone: bool = True
|
| 23 |
-
voice_source: str = "video" # preset | video | upload
|
| 24 |
-
speaker_wav: str = "" # khi upload/preset đã có sẵn
|
| 25 |
-
preset: str = ""
|
| 26 |
-
style: str = "natural"
|
| 27 |
-
engine: str = "auto"
|
| 28 |
-
demucs_model: str = "htdemucs"
|
| 29 |
-
multi_speaker: bool = False # ≥3 người nói: clone giọng từng người
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@dataclass
|
| 33 |
-
class DubResult:
|
| 34 |
-
video_out: str
|
| 35 |
-
audio_out: str
|
| 36 |
-
srt_out: str
|
| 37 |
-
language: str
|
| 38 |
-
n_lines: int
|
| 39 |
-
note: str = "" # ghi chú khi phải dùng đường dự phòng
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
def _srt_time(t: float) -> str:
|
| 43 |
-
h = int(t // 3600); t -= h * 3600
|
| 44 |
-
m = int(t // 60); t -= m * 60
|
| 45 |
-
s = int(t); ms = int(round((t - s) * 1000))
|
| 46 |
-
return f"{h:02d}:{m:02d}:{s:02d},{ms:03d}"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def write_srt(lines: list[translate.TranslatedLine], path: str) -> str:
|
| 50 |
-
with open(path, "w", encoding="utf-8") as f:
|
| 51 |
-
for i, ln in enumerate(lines, 1):
|
| 52 |
-
f.write(f"{i}\n{_srt_time(ln.start)} --> {_srt_time(ln.end)}\n{ln.text}\n\n")
|
| 53 |
-
return path
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
@dataclass
|
| 57 |
-
class TtsOptions:
|
| 58 |
-
text: str
|
| 59 |
-
target_lang: str = "vi" # en | vi | fil
|
| 60 |
-
voice_source: str = "preset" # preset | upload
|
| 61 |
-
speaker_wav: str = "" # mẫu clone khi voice_source=upload
|
| 62 |
-
preset: str = "" # tên giọng có sẵn
|
| 63 |
-
style: str = "natural"
|
| 64 |
-
engine: str = "auto" # auto | vieneu | xtts | voxcpm
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def run_tts(opt: TtsOptions, out_dir: str,
|
| 68 |
-
progress: Callable[[float, str], None] | None = None) -> str:
|
| 69 |
-
"""Sinh 1 đoạn voice từ text nhập vào -> trả về đường dẫn wav.
|
| 70 |
-
|
| 71 |
-
Dùng chung engine TTS với pipeline lồng tiếng; chọn model theo ngôn ngữ
|
| 72 |
-
(vi->VieNeu, en->XTTS/VoxCPM2, fil->VoxCPM2) hoặc engine người dùng chọn.
|
| 73 |
-
"""
|
| 74 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 75 |
-
if progress:
|
| 76 |
-
progress(0.1, "Nạp model giọng")
|
| 77 |
-
out_wav = os.path.join(out_dir, "voice.wav")
|
| 78 |
-
vcfg = tts.VoiceConfig(
|
| 79 |
-
target_lang=opt.target_lang, voice_source=opt.voice_source,
|
| 80 |
-
speaker_wav=opt.speaker_wav, preset=opt.preset, style=opt.style,
|
| 81 |
-
engine=opt.engine)
|
| 82 |
-
try:
|
| 83 |
-
engine = tts.load_engine(vcfg)
|
| 84 |
-
if progress:
|
| 85 |
-
progress(0.4, "Tổng hợp giọng đọc")
|
| 86 |
-
tts.synth_line(engine, opt.text, out_wav, vcfg)
|
| 87 |
-
except Exception:
|
| 88 |
-
# thiếu model TTS -> tạo clip lặng theo độ dài ước lượng để luồng không vỡ
|
| 89 |
-
if progress:
|
| 90 |
-
progress(0.6, "Thiếu model giọng — tạo clip mẫu")
|
| 91 |
-
dur = max(1.0, len(opt.text.split()) / 2.5)
|
| 92 |
-
tts._silence(out_wav, min(30.0, dur))
|
| 93 |
-
if progress:
|
| 94 |
-
progress(1.0, "Hoàn tất")
|
| 95 |
-
return out_wav
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def _passthrough(video: str, out_dir: str, opt: DubOptions, note: str) -> DubResult:
|
| 99 |
-
"""Đường DỰ PHÒNG: khi thiếu model/khoá API, vẫn TRẢ VỀ video tải được.
|
| 100 |
-
|
| 101 |
-
Xuất ra video gốc (chuẩn hóa mp4) + audio gốc + srt rỗng, kèm ghi chú lý do.
|
| 102 |
-
Đảm bảo người dùng luôn TẢI ĐƯỢC video, không bị treo/job lỗi.
|
| 103 |
-
"""
|
| 104 |
-
import shutil as _sh
|
| 105 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 106 |
-
ffmpeg = _sh.which("ffmpeg") or "ffmpeg"
|
| 107 |
-
video_out = os.path.join(out_dir, f"video_translated_{opt.target_lang}.mp4")
|
| 108 |
-
try:
|
| 109 |
-
import subprocess
|
| 110 |
-
subprocess.run([ffmpeg, "-y", "-hide_banner", "-loglevel", "error",
|
| 111 |
-
"-i", video, "-c", "copy", video_out],
|
| 112 |
-
check=True, capture_output=True)
|
| 113 |
-
except Exception:
|
| 114 |
-
_sh.copy(video, video_out)
|
| 115 |
-
audio_out = os.path.join(out_dir, "audio.m4a")
|
| 116 |
-
try:
|
| 117 |
-
import subprocess
|
| 118 |
-
subprocess.run([ffmpeg, "-y", "-hide_banner", "-loglevel", "error",
|
| 119 |
-
"-i", video, "-vn", "-c:a", "aac", audio_out],
|
| 120 |
-
check=True, capture_output=True)
|
| 121 |
-
except Exception:
|
| 122 |
-
audio_out = video_out
|
| 123 |
-
srt_out = os.path.join(out_dir, "subtitle.srt")
|
| 124 |
-
with open(srt_out, "w", encoding="utf-8") as f:
|
| 125 |
-
f.write("")
|
| 126 |
-
return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out,
|
| 127 |
-
language="unknown", n_lines=0, note=note)
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
def run_dub(video: str, out_dir: str, opt: DubOptions,
|
| 131 |
-
progress: Callable[[float, str], None] | None = None) -> DubResult:
|
| 132 |
-
"""Bọc pipeline thật: nếu BẤT KỲ bước nào lỗi (thiếu model/API/mạng) thì
|
| 133 |
-
rơi về _passthrough để người dùng vẫn tải được video. Không bao giờ treo job."""
|
| 134 |
-
try:
|
| 135 |
-
return _run_dub_full(video, out_dir, opt, progress)
|
| 136 |
-
except Exception as e:
|
| 137 |
-
import traceback
|
| 138 |
-
traceback.print_exc() # in chi tiết ra console uvicorn để biết tầng nào hỏng
|
| 139 |
-
if progress:
|
| 140 |
-
progress(1.0, "Dùng video gốc (xem ghi chú lỗi)")
|
| 141 |
-
return _passthrough(video, out_dir, opt,
|
| 142 |
-
note=f"Chưa lồng tiếng được nên trả video gốc: {e}")
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def _run_dub_full(video: str, out_dir: str, opt: DubOptions,
|
| 146 |
-
progress: Callable[[float, str], None] | None = None) -> DubResult:
|
| 147 |
-
def emit(p, m):
|
| 148 |
-
if progress:
|
| 149 |
-
progress(p, m)
|
| 150 |
-
|
| 151 |
-
work = os.path.join(out_dir, "work")
|
| 152 |
-
os.makedirs(work, exist_ok=True)
|
| 153 |
-
total = video_duration(video)
|
| 154 |
-
|
| 155 |
-
emit(0.05, "Tách audio & xử lý nền nhạc")
|
| 156 |
-
bundle = audio.build_audio(video, work, opt.bg_mode, opt.demucs_model)
|
| 157 |
-
|
| 158 |
-
emit(0.25, "Nhận dạng lời thoại (Whisper)")
|
| 159 |
-
try:
|
| 160 |
-
segments, language = asr.transcribe(bundle.asr_wav)
|
| 161 |
-
except Exception as e:
|
| 162 |
-
raise RuntimeError(f"[Bước ASR/Groq Whisper] {e}") from e
|
| 163 |
-
|
| 164 |
-
emit(0.45, f"Dịch sang {opt.target_lang} (vừa khít thời lượng)")
|
| 165 |
-
try:
|
| 166 |
-
translated = translate.translate_all(segments, opt.target_lang)
|
| 167 |
-
except Exception as e:
|
| 168 |
-
raise RuntimeError(f"[Bước Dịch/Gemini] {e}") from e
|
| 169 |
-
srt_out = write_srt(translated, os.path.join(out_dir, "subtitle.srt"))
|
| 170 |
-
|
| 171 |
-
# mẫu clone
|
| 172 |
-
speaker_wav = opt.speaker_wav
|
| 173 |
-
if opt.do_clone and opt.voice_source == "video" and segments:
|
| 174 |
-
emit(0.55, "Cắt mẫu giọng để clone")
|
| 175 |
-
speaker_wav = audio.cut_clone_sample(
|
| 176 |
-
bundle.voice_wav, work, start=max(0.0, segments[0].start), length=15.0)
|
| 177 |
-
|
| 178 |
-
emit(0.6, "Tổng hợp giọng đọc (TTS) + clone")
|
| 179 |
-
vcfg = tts.VoiceConfig(
|
| 180 |
-
target_lang=opt.target_lang, voice_source=opt.voice_source,
|
| 181 |
-
speaker_wav=speaker_wav, preset=opt.preset, style=opt.style,
|
| 182 |
-
engine=opt.engine)
|
| 183 |
-
try:
|
| 184 |
-
engine = tts.load_engine(vcfg)
|
| 185 |
-
except Exception as e:
|
| 186 |
-
raise RuntimeError(f"[Bước Giọng/TTS — model chạy local] {e}") from e
|
| 187 |
-
|
| 188 |
-
placed: list[mux.PlacedLine] = []
|
| 189 |
-
tts_dir = os.path.join(work, "tts"); os.makedirs(tts_dir, exist_ok=True)
|
| 190 |
-
n = len(translated)
|
| 191 |
-
for i, ln in enumerate(translated):
|
| 192 |
-
raw = os.path.join(tts_dir, f"line_{i:04d}.wav")
|
| 193 |
-
tts.synth_line(engine, ln.text, raw, vcfg)
|
| 194 |
-
# khe = đến đầu câu kế (hoặc hết video với câu cuối)
|
| 195 |
-
slot_end = translated[i + 1].start if i + 1 < n else total
|
| 196 |
-
slot = max(0.3, slot_end - ln.start)
|
| 197 |
-
fitted = os.path.join(tts_dir, f"fit_{i:04d}.wav")
|
| 198 |
-
mux.fit_to_slot(raw, slot, fitted)
|
| 199 |
-
placed.append(mux.PlacedLine(start=ln.start, wav=fitted))
|
| 200 |
-
emit(0.6 + 0.25 * (i + 1) / max(1, n), f"TTS câu {i+1}/{n}")
|
| 201 |
-
|
| 202 |
-
emit(0.9, "Neo mốc thời gian & trộn nền")
|
| 203 |
-
voice_track = mux.assemble_voice(placed, total, os.path.join(work, "voice.wav"))
|
| 204 |
-
final_audio = mux.mix_over_bg(voice_track, bundle.bg_wav,
|
| 205 |
-
os.path.join(work, "final_audio.wav"))
|
| 206 |
-
|
| 207 |
-
emit(0.96, "Ghép vào video")
|
| 208 |
-
video_out = mux.mux_to_video(
|
| 209 |
-
video, final_audio,
|
| 210 |
-
os.path.join(out_dir, f"video_translated_{opt.target_lang}.mp4"))
|
| 211 |
-
|
| 212 |
-
emit(1.0, "Hoàn tất")
|
| 213 |
-
return DubResult(video_out=video_out, audio_out=final_audio, srt_out=srt_out,
|
| 214 |
-
language=language, n_lines=n)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/probe_dur.py
DELETED
|
@@ -1,13 +0,0 @@
|
|
| 1 |
-
"""Helper nhỏ: lấy thời lượng video."""
|
| 2 |
-
import shutil, subprocess
|
| 3 |
-
FFPROBE = shutil.which("ffprobe") or "ffprobe"
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
def video_duration(path: str) -> float:
|
| 7 |
-
out = subprocess.run(
|
| 8 |
-
[FFPROBE, "-v", "error", "-show_entries", "format=duration",
|
| 9 |
-
"-of", "csv=p=0", path], capture_output=True, text=True).stdout.strip()
|
| 10 |
-
try:
|
| 11 |
-
return float(out)
|
| 12 |
-
except ValueError:
|
| 13 |
-
return 0.0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/translate.py
DELETED
|
@@ -1,88 +0,0 @@
|
|
| 1 |
-
"""Bước DỊCH — dịch-vừa-khít-thời-lượng.
|
| 2 |
-
|
| 3 |
-
- Chỉ hỗ trợ đích: tiếng Anh (en), tiếng Việt (vi), tiếng Filipino (fil).
|
| 4 |
-
- Mỗi câu tính sẵn ngân sách ký tự = thời lượng × tốc độ đọc trung bình, đưa cả số giây
|
| 5 |
-
lẫn hạn ký tự vào prompt; ưu tiên paraphrase ngắn gọn tự nhiên, giữ tên riêng/số/dấu câu.
|
| 6 |
-
- Model chính: Gemini 2.0 Flash (Google GenLang). Dự phòng: GPT-OSS 20B (Groq).
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
from __future__ import annotations
|
| 10 |
-
|
| 11 |
-
from dataclasses import dataclass
|
| 12 |
-
|
| 13 |
-
from ...config import settings
|
| 14 |
-
from .asr import Segment
|
| 15 |
-
|
| 16 |
-
# tốc độ đọc trung bình (ký tự/giây) theo ngôn ngữ đích
|
| 17 |
-
CPS = {"en": 15.0, "vi": 16.0, "fil": 15.0}
|
| 18 |
-
LANG_NAME = {"en": "English", "vi": "Vietnamese", "fil": "Filipino"}
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
@dataclass
|
| 22 |
-
class TranslatedLine:
|
| 23 |
-
start: float
|
| 24 |
-
end: float
|
| 25 |
-
source: str
|
| 26 |
-
text: str
|
| 27 |
-
char_budget: int
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
def char_budget(seg: Segment, target_lang: str) -> int:
|
| 31 |
-
cps = CPS.get(target_lang, 15.0)
|
| 32 |
-
return max(8, int(seg.dur * cps))
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def _prompt(seg: Segment, target_lang: str, budget: int) -> str:
|
| 36 |
-
name = LANG_NAME.get(target_lang, target_lang)
|
| 37 |
-
return (
|
| 38 |
-
f"Translate the following line into natural {name} for video dubbing.\n"
|
| 39 |
-
f"It must be speakable in about {seg.dur:.1f} seconds, so keep it within "
|
| 40 |
-
f"~{budget} characters. Prefer a concise, natural paraphrase over a literal "
|
| 41 |
-
f"translation if needed to fit the time. Keep proper nouns, numbers and "
|
| 42 |
-
f"punctuation. Return ONLY the translated line, no quotes, no notes.\n\n"
|
| 43 |
-
f"Line: {seg.text}"
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
def _gemini(prompt: str) -> str:
|
| 48 |
-
import google.generativeai as genai # type: ignore
|
| 49 |
-
|
| 50 |
-
genai.configure(api_key=settings.GOOGLE_API_KEY)
|
| 51 |
-
model = genai.GenerativeModel(settings.GEMINI_MODEL)
|
| 52 |
-
resp = model.generate_content(prompt)
|
| 53 |
-
return (resp.text or "").strip()
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def _groq_llm(prompt: str) -> str:
|
| 57 |
-
from groq import Groq # type: ignore
|
| 58 |
-
|
| 59 |
-
client = Groq(api_key=settings.GROQ_API_KEY)
|
| 60 |
-
resp = client.chat.completions.create(
|
| 61 |
-
model=settings.GROQ_LLM_FALLBACK,
|
| 62 |
-
messages=[{"role": "user", "content": prompt}],
|
| 63 |
-
temperature=0.3,
|
| 64 |
-
)
|
| 65 |
-
return resp.choices[0].message.content.strip()
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
def translate_line(seg: Segment, target_lang: str) -> str:
|
| 69 |
-
budget = char_budget(seg, target_lang)
|
| 70 |
-
prompt = _prompt(seg, target_lang, budget)
|
| 71 |
-
try:
|
| 72 |
-
return _gemini(prompt)
|
| 73 |
-
except Exception:
|
| 74 |
-
try:
|
| 75 |
-
return _groq_llm(prompt) # tự động dùng dự phòng
|
| 76 |
-
except Exception:
|
| 77 |
-
return seg.text # cùng lắm giữ nguyên để không vỡ luồng
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def translate_all(segs: list[Segment], target_lang: str) -> list[TranslatedLine]:
|
| 81 |
-
if target_lang not in LANG_NAME:
|
| 82 |
-
raise ValueError(f"Ngôn ngữ đích chưa hỗ trợ: {target_lang}")
|
| 83 |
-
out = []
|
| 84 |
-
for s in segs:
|
| 85 |
-
txt = translate_line(s, target_lang)
|
| 86 |
-
out.append(TranslatedLine(s.start, s.end, s.text, txt,
|
| 87 |
-
char_budget(s, target_lang)))
|
| 88 |
-
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/dubber/tts.py
DELETED
|
@@ -1,144 +0,0 @@
|
|
| 1 |
-
"""Bước TỔNG HỢP GIỌNG (TTS).
|
| 2 |
-
|
| 3 |
-
Chọn engine theo NGÔN NGỮ ĐÍCH (đã bỏ hoàn toàn edge-tts):
|
| 4 |
-
- vi -> VieNeu-TTS (local real-time trên CPU)
|
| 5 |
-
- en -> XTTS-v2 (hoặc VoxCPM2 thay thế)
|
| 6 |
-
- fil -> VoxCPM2 (en cũng có thể dùng VoxCPM2)
|
| 7 |
-
|
| 8 |
-
Tất cả đều clone được giọng từ mẫu 10-20s, hoặc dùng giọng preset.
|
| 9 |
-
Nguồn giọng clone (voice_source): preset | video | upload.
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
from __future__ import annotations
|
| 13 |
-
|
| 14 |
-
import os
|
| 15 |
-
import shutil
|
| 16 |
-
import subprocess
|
| 17 |
-
from dataclasses import dataclass
|
| 18 |
-
|
| 19 |
-
FFMPEG = shutil.which("ffmpeg") or "ffmpeg"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
@dataclass
|
| 23 |
-
class VoiceConfig:
|
| 24 |
-
target_lang: str
|
| 25 |
-
voice_source: str = "video" # preset | video | upload
|
| 26 |
-
speaker_wav: str = "" # mẫu clone (khi video/upload)
|
| 27 |
-
preset: str = "" # tên giọng preset (khi preset)
|
| 28 |
-
style: str = "natural" # phong cách đọc (lấy từ video hoặc người chọn)
|
| 29 |
-
engine: str = "auto" # auto | vieneu | xtts | voxcpm
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def pick_engine(cfg: VoiceConfig) -> str:
|
| 33 |
-
if cfg.engine != "auto":
|
| 34 |
-
return cfg.engine
|
| 35 |
-
return {"vi": "vieneu", "en": "xtts", "fil": "voxcpm"}.get(cfg.target_lang, "voxcpm")
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
class TTSEngine:
|
| 39 |
-
"""Giao diện chung cho các engine. synth(text, out_wav) -> path."""
|
| 40 |
-
|
| 41 |
-
def synth(self, text: str, out_wav: str, cfg: VoiceConfig) -> str: # pragma: no cover
|
| 42 |
-
raise NotImplementedError
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
class VieNeuEngine(TTSEngine): # pragma: no cover - cần model local
|
| 46 |
-
def __init__(self):
|
| 47 |
-
import vieneu # type: ignore
|
| 48 |
-
self.model = None
|
| 49 |
-
# 1) Thử factory() — kiểu khởi tạo khuyến nghị nếu có
|
| 50 |
-
fac = getattr(vieneu, "factory", None)
|
| 51 |
-
if callable(fac):
|
| 52 |
-
try:
|
| 53 |
-
self.model = fac()
|
| 54 |
-
except Exception:
|
| 55 |
-
self.model = None
|
| 56 |
-
# 2) Nếu chưa được, thử class Vieneu()
|
| 57 |
-
if self.model is None:
|
| 58 |
-
cls = getattr(vieneu, "Vieneu", None) or getattr(vieneu, "VieNeuTTS", None)
|
| 59 |
-
if cls is None:
|
| 60 |
-
raise ImportError(
|
| 61 |
-
"Không thấy entrypoint TTS trong 'vieneu'. Tên có: "
|
| 62 |
-
+ ", ".join(n for n in dir(vieneu) if not n.startswith("_"))
|
| 63 |
-
)
|
| 64 |
-
self.model = cls()
|
| 65 |
-
|
| 66 |
-
def synth(self, text, out_wav, cfg):
|
| 67 |
-
ref = cfg.speaker_wav or None
|
| 68 |
-
for meth, kwargs in (
|
| 69 |
-
("tts_to_file", {"text": text, "speaker_wav": ref, "file_path": out_wav}),
|
| 70 |
-
("tts_to_file", {"text": text, "file_path": out_wav}),
|
| 71 |
-
("synthesize", {"text": text, "speaker_wav": ref, "output_path": out_wav}),
|
| 72 |
-
("synthesize", {"text": text, "ref_audio": ref, "output_path": out_wav}),
|
| 73 |
-
("generate", {"text": text, "ref_audio": ref, "output_path": out_wav}),
|
| 74 |
-
("infer", {"text": text, "ref_audio": ref, "output_path": out_wav}),
|
| 75 |
-
("tts", {"text": text, "speaker_wav": ref, "output_path": out_wav}),
|
| 76 |
-
):
|
| 77 |
-
fn = getattr(self.model, meth, None)
|
| 78 |
-
if fn:
|
| 79 |
-
try:
|
| 80 |
-
fn(**kwargs)
|
| 81 |
-
return out_wav
|
| 82 |
-
except TypeError:
|
| 83 |
-
continue
|
| 84 |
-
raise RuntimeError(
|
| 85 |
-
"Vieneu không có hàm xuất file quen thuộc. Hàm đang có: "
|
| 86 |
-
+ ", ".join(m for m in dir(self.model) if not m.startswith("_"))
|
| 87 |
-
)
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
class XTTSEngine(TTSEngine): # pragma: no cover - cần model local
|
| 91 |
-
def __init__(self):
|
| 92 |
-
from TTS.api import TTS # type: ignore
|
| 93 |
-
self.model = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
|
| 94 |
-
|
| 95 |
-
def synth(self, text, out_wav, cfg):
|
| 96 |
-
self.model.tts_to_file(
|
| 97 |
-
text=text, file_path=out_wav,
|
| 98 |
-
speaker_wav=cfg.speaker_wav or None,
|
| 99 |
-
language=cfg.target_lang if cfg.target_lang != "fil" else "en",
|
| 100 |
-
)
|
| 101 |
-
return out_wav
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
class VoxCPMEngine(TTSEngine): # pragma: no cover - cần model local
|
| 105 |
-
def __init__(self):
|
| 106 |
-
from voxcpm import VoxCPM # type: ignore
|
| 107 |
-
self.model = VoxCPM.from_pretrained("openbmb/VoxCPM2")
|
| 108 |
-
|
| 109 |
-
def synth(self, text, out_wav, cfg):
|
| 110 |
-
self.model.generate_to_file(
|
| 111 |
-
text=text, prompt_wav=cfg.speaker_wav or None, output_path=out_wav)
|
| 112 |
-
return out_wav
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
_ENGINES: dict[str, type[TTSEngine]] = {
|
| 116 |
-
"vieneu": VieNeuEngine, "xtts": XTTSEngine, "voxcpm": VoxCPMEngine,
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def load_engine(cfg: VoiceConfig) -> TTSEngine:
|
| 121 |
-
name = pick_engine(cfg)
|
| 122 |
-
cls = _ENGINES.get(name)
|
| 123 |
-
if cls is None:
|
| 124 |
-
raise ValueError(f"engine không hợp lệ: {name}")
|
| 125 |
-
return cls()
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
def synth_line(engine: TTSEngine, text: str, out_wav: str, cfg: VoiceConfig) -> str:
|
| 129 |
-
"""Tổng hợp 1 câu, chịu lỗi: câu rỗng -> để file lặng ngắn."""
|
| 130 |
-
if not text.strip():
|
| 131 |
-
_silence(out_wav, 0.2)
|
| 132 |
-
return out_wav
|
| 133 |
-
try:
|
| 134 |
-
return engine.synth(text, out_wav, cfg)
|
| 135 |
-
except Exception:
|
| 136 |
-
_silence(out_wav, 0.4) # lỗi 1 câu -> để khoảng lặng, đi tiếp
|
| 137 |
-
return out_wav
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
def _silence(out_wav: str, dur: float):
|
| 141 |
-
subprocess.run(
|
| 142 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 143 |
-
"-f", "lavfi", "-i", "anullsrc=r=24000:cl=mono",
|
| 144 |
-
"-t", f"{dur:.3f}", out_wav], check=True, capture_output=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/listing/__init__.py
DELETED
|
File without changes
|
app/services/shuffler/__init__.py
DELETED
|
File without changes
|
app/services/shuffler/engine.py
DELETED
|
@@ -1,204 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Engine "Xào video" — tạo nhiều biến thể video bằng cách:
|
| 3 |
-
1) chọn 1 clip cho mỗi KHÚC (tổ hợp Cartesian các pool clip), và
|
| 4 |
-
2) ĐẢO THỨ TỰ các khúc theo chế độ của TỪNG khúc (không còn cờ toàn cục).
|
| 5 |
-
|
| 6 |
-
Mô hình khúc (order_mode)
|
| 7 |
-
-------------------------
|
| 8 |
-
- "lock" : khúc GIỮ NGUYÊN vị trí (khóa thứ tự).
|
| 9 |
-
- "shuffle" : khúc được phép đảo chỗ với các khúc shuffle khác.
|
| 10 |
-
- "drop" : BỎ hẳn khúc khỏi output (không xuất hiện trong video).
|
| 11 |
-
|
| 12 |
-
Ví dụ với 5 khúc 1..5:
|
| 13 |
-
- tất cả lock -> 1,2,3,4,5
|
| 14 |
-
- lock 1 & 4, còn lại shuffle -> 1,_,_,4,_ + hoán vị {2,3,5} -> 1,5,2,4,3 ...
|
| 15 |
-
- lock 1, drop 4, còn lại shuffle -> 1 + hoán vị {2,3,5} -> 1,3,5,2 ...
|
| 16 |
-
|
| 17 |
-
Nếu KHÔNG bật lọc tương đồng (threshold>=1.0) và random_ratio>=1.0
|
| 18 |
-
thì engine trả về TOÀN BỘ tổ hợp (lấy hết video).
|
| 19 |
-
"""
|
| 20 |
-
|
| 21 |
-
from __future__ import annotations
|
| 22 |
-
|
| 23 |
-
from dataclasses import dataclass
|
| 24 |
-
from itertools import permutations, product
|
| 25 |
-
from math import factorial, prod
|
| 26 |
-
from typing import Iterable
|
| 27 |
-
|
| 28 |
-
import numpy as np
|
| 29 |
-
|
| 30 |
-
PAD = -9 # sentinel pad cho bằng độ dài, không bao giờ trùng clip thật
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
@dataclass
|
| 34 |
-
class Segment:
|
| 35 |
-
"""Một khúc và pool clip ứng viên của nó."""
|
| 36 |
-
clips: list[int] # danh sách clip_id ứng viên
|
| 37 |
-
order_mode: str = "shuffle" # "lock" | "shuffle" | "drop"
|
| 38 |
-
|
| 39 |
-
@property
|
| 40 |
-
def locked(self) -> bool:
|
| 41 |
-
return self.order_mode == "lock"
|
| 42 |
-
|
| 43 |
-
@property
|
| 44 |
-
def dropped(self) -> bool:
|
| 45 |
-
# "optional" giữ lại như alias cũ = drop
|
| 46 |
-
return self.order_mode in ("drop", "optional")
|
| 47 |
-
|
| 48 |
-
def pool(self) -> list[int]:
|
| 49 |
-
return list(self.clips)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
@dataclass
|
| 53 |
-
class ShuffleConfig:
|
| 54 |
-
segments: list[Segment]
|
| 55 |
-
similarity_threshold: float = 0.80 # >=1.0 => tắt lọc trùng (lấy hết)
|
| 56 |
-
random_ratio: float = 1.0 # >=1.0 => không lấy mẫu ngẫu nhiên
|
| 57 |
-
max_outputs: int | None = None
|
| 58 |
-
seed: int | None = None
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
@dataclass
|
| 62 |
-
class ShuffleEstimate:
|
| 63 |
-
selection_count: int
|
| 64 |
-
ordering_count: int
|
| 65 |
-
raw_total: int
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# ----------------------------------------------------------- helpers ---
|
| 69 |
-
|
| 70 |
-
def _active(cfg: ShuffleConfig):
|
| 71 |
-
"""Trả về danh sách khúc KHÔNG bị drop (giữ nguyên thứ tự khai báo)."""
|
| 72 |
-
return [s for s in cfg.segments if not s.dropped]
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
def _ordering_indices(locked_flags: list[bool]) -> list[tuple[int, ...]]:
|
| 76 |
-
"""Các hoán vị thứ tự trên những khúc còn hoạt động.
|
| 77 |
-
|
| 78 |
-
Khúc locked giữ đúng slot của nó; các khúc shuffle hoán vị trong slot trống.
|
| 79 |
-
Index ở đây là 0..M-1 ứng với danh sách khúc đã loại drop.
|
| 80 |
-
"""
|
| 81 |
-
m = len(locked_flags)
|
| 82 |
-
if m == 0:
|
| 83 |
-
return [()]
|
| 84 |
-
free_positions = [i for i in range(m) if not locked_flags[i]]
|
| 85 |
-
if len(free_positions) <= 1:
|
| 86 |
-
return [tuple(range(m))]
|
| 87 |
-
|
| 88 |
-
out: list[tuple[int, ...]] = []
|
| 89 |
-
for perm in permutations(free_positions):
|
| 90 |
-
arr: list[int | None] = [None] * m
|
| 91 |
-
for pos in range(m):
|
| 92 |
-
if locked_flags[pos]:
|
| 93 |
-
arr[pos] = pos
|
| 94 |
-
for slot, item in zip(free_positions, perm):
|
| 95 |
-
arr[slot] = item
|
| 96 |
-
out.append(tuple(arr)) # type: ignore[arg-type]
|
| 97 |
-
return out
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
def estimate(cfg: ShuffleConfig) -> ShuffleEstimate:
|
| 101 |
-
active = _active(cfg)
|
| 102 |
-
pools = [s.pool() for s in active]
|
| 103 |
-
selection_count = prod(len(p) for p in pools) if pools else 0
|
| 104 |
-
|
| 105 |
-
free = sum(0 if s.locked else 1 for s in active)
|
| 106 |
-
ordering_count = factorial(free) if free > 1 else 1
|
| 107 |
-
|
| 108 |
-
return ShuffleEstimate(
|
| 109 |
-
selection_count=selection_count,
|
| 110 |
-
ordering_count=ordering_count,
|
| 111 |
-
raw_total=selection_count * ordering_count,
|
| 112 |
-
)
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
# ----------------------------------------------------------- generate ---
|
| 116 |
-
|
| 117 |
-
def _iter_raw_sequences(cfg: ShuffleConfig) -> Iterable[list[int]]:
|
| 118 |
-
active = _active(cfg)
|
| 119 |
-
if not active:
|
| 120 |
-
return
|
| 121 |
-
pools = [s.pool() for s in active]
|
| 122 |
-
locked_flags = [s.locked for s in active]
|
| 123 |
-
orderings = _ordering_indices(locked_flags)
|
| 124 |
-
|
| 125 |
-
for selection in product(*pools):
|
| 126 |
-
for order in orderings:
|
| 127 |
-
seq = [selection[i] for i in order]
|
| 128 |
-
if seq:
|
| 129 |
-
yield seq
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
def _pad_matrix(seqs: list[list[int]]) -> np.ndarray:
|
| 133 |
-
if not seqs:
|
| 134 |
-
return np.empty((0, 0), dtype=np.int64)
|
| 135 |
-
width = max(len(s) for s in seqs)
|
| 136 |
-
mat = np.full((len(seqs), width), PAD, dtype=np.int64)
|
| 137 |
-
for r, s in enumerate(seqs):
|
| 138 |
-
mat[r, : len(s)] = s
|
| 139 |
-
return mat
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
def similarity(a: list[int], b: list[int]) -> float:
|
| 143 |
-
width = max(len(a), len(b))
|
| 144 |
-
if width == 0:
|
| 145 |
-
return 1.0
|
| 146 |
-
pa = np.full(width, PAD, dtype=np.int64)
|
| 147 |
-
pb = np.full(width, PAD, dtype=np.int64)
|
| 148 |
-
pa[: len(a)] = a
|
| 149 |
-
pb[: len(b)] = b
|
| 150 |
-
eq = (pa == pb) & (pa != PAD)
|
| 151 |
-
return float(eq.sum()) / width
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
def _greedy_dedup(mat: np.ndarray, threshold: float) -> list[int]:
|
| 155 |
-
n, width = mat.shape
|
| 156 |
-
if n == 0:
|
| 157 |
-
return []
|
| 158 |
-
kept_idx: list[int] = []
|
| 159 |
-
kept_rows = np.empty((0, width), dtype=np.int64)
|
| 160 |
-
valid = mat != PAD
|
| 161 |
-
|
| 162 |
-
for i in range(n):
|
| 163 |
-
if not kept_idx:
|
| 164 |
-
kept_idx.append(i)
|
| 165 |
-
kept_rows = mat[i : i + 1].copy()
|
| 166 |
-
continue
|
| 167 |
-
row = mat[i]
|
| 168 |
-
eq = (kept_rows == row) & valid[i]
|
| 169 |
-
sims = eq.sum(axis=1) / width
|
| 170 |
-
if sims.max() < threshold:
|
| 171 |
-
kept_idx.append(i)
|
| 172 |
-
kept_rows = np.vstack([kept_rows, row])
|
| 173 |
-
return kept_idx
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
def generate(cfg: ShuffleConfig) -> list[list[int]]:
|
| 177 |
-
seqs = list(_iter_raw_sequences(cfg))
|
| 178 |
-
if not seqs:
|
| 179 |
-
return []
|
| 180 |
-
|
| 181 |
-
rng = np.random.default_rng(cfg.seed)
|
| 182 |
-
|
| 183 |
-
# 1) lọc trùng theo ngưỡng tương đồng (bỏ qua nếu >=1.0 => lấy hết)
|
| 184 |
-
if 0.0 < cfg.similarity_threshold < 1.0:
|
| 185 |
-
mat = _pad_matrix(seqs)
|
| 186 |
-
perm = rng.permutation(len(seqs))
|
| 187 |
-
mat = mat[perm]
|
| 188 |
-
keep = _greedy_dedup(mat, cfg.similarity_threshold)
|
| 189 |
-
result = [[int(c) for c in mat[k] if c != PAD] for k in keep]
|
| 190 |
-
else:
|
| 191 |
-
result = [list(s) for s in seqs]
|
| 192 |
-
|
| 193 |
-
# 2) lấy mẫu ngẫu nhiên theo tỉ lệ (bỏ qua nếu >=1.0 => giữ hết)
|
| 194 |
-
if 0.0 < cfg.random_ratio < 1.0 and result:
|
| 195 |
-
k = max(1, int(round(len(result) * cfg.random_ratio)))
|
| 196 |
-
idx = rng.choice(len(result), size=min(k, len(result)), replace=False)
|
| 197 |
-
result = [result[i] for i in sorted(idx.tolist())]
|
| 198 |
-
|
| 199 |
-
# 3) trần số lượng
|
| 200 |
-
if cfg.max_outputs is not None and len(result) > cfg.max_outputs:
|
| 201 |
-
idx = rng.choice(len(result), size=cfg.max_outputs, replace=False)
|
| 202 |
-
result = [result[i] for i in sorted(idx.tolist())]
|
| 203 |
-
|
| 204 |
-
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/shuffler/render.py
DELETED
|
@@ -1,81 +0,0 @@
|
|
| 1 |
-
"""Render biến thể xào: ghép clip theo sequence -> mp4; tùy chọn lồng âm thanh; zip.
|
| 2 |
-
|
| 3 |
-
- clip_id -> đường dẫn clip (người dùng nạp vào từng khúc).
|
| 4 |
-
- Mỗi sequence (list clip_id) -> 1 video biến thể (ảnh, KHÔNG tiếng).
|
| 5 |
-
- Nếu có pool âm thanh: mỗi biến thể được gán 1 track (xoay vòng theo thứ tự),
|
| 6 |
-
ví dụ 3 âm thanh s1,s2,s3 -> biến thể 1->s1, 2->s2, 3->s3, 4->s1 ...
|
| 7 |
-
"""
|
| 8 |
-
|
| 9 |
-
from __future__ import annotations
|
| 10 |
-
|
| 11 |
-
import os
|
| 12 |
-
import shutil
|
| 13 |
-
import subprocess
|
| 14 |
-
import zipfile
|
| 15 |
-
|
| 16 |
-
FFMPEG = shutil.which("ffmpeg") or "ffmpeg"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def _concat_reencode(clip_paths: list[str], out_path: str):
|
| 20 |
-
"""Ghép nhiều clip bằng concat filter (re-encode, video-only)."""
|
| 21 |
-
inputs = []
|
| 22 |
-
for p in clip_paths:
|
| 23 |
-
inputs += ["-i", p]
|
| 24 |
-
n = len(clip_paths)
|
| 25 |
-
streams = ""
|
| 26 |
-
for i in range(n):
|
| 27 |
-
streams += f"[{i}:v]setsar=1,fps=30,format=yuv420p[v{i}];"
|
| 28 |
-
concat_in = "".join(f"[v{i}]" for i in range(n))
|
| 29 |
-
fg = streams + f"{concat_in}concat=n={n}:v=1:a=0[outv]"
|
| 30 |
-
subprocess.run(
|
| 31 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error", *inputs,
|
| 32 |
-
"-filter_complex", fg, "-map", "[outv]",
|
| 33 |
-
"-c:v", "libx264", "-preset", "veryfast", "-crf", "20", out_path],
|
| 34 |
-
check=True, capture_output=True)
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
def attach_audio(video_path: str, audio_path: str, out_path: str):
|
| 38 |
-
"""Lồng 1 track âm thanh vào video, GIỮ NGUYÊN độ dài video.
|
| 39 |
-
|
| 40 |
-
- Âm thanh ngắn hơn video: phần dư của video sẽ LẶNG (pad silence), KHÔNG lặp.
|
| 41 |
-
- Âm thanh dài hơn video: cắt theo độ dài video.
|
| 42 |
-
"""
|
| 43 |
-
subprocess.run(
|
| 44 |
-
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 45 |
-
"-i", video_path, "-i", audio_path,
|
| 46 |
-
"-filter_complex", "[1:a]apad[aud]",
|
| 47 |
-
"-map", "0:v:0", "-map", "[aud]",
|
| 48 |
-
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
|
| 49 |
-
"-shortest", out_path],
|
| 50 |
-
check=True, capture_output=True)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def render_variations(sequences: list[list[int]], clip_map: dict[int, str],
|
| 54 |
-
out_dir: str, prefix: str = "Variation",
|
| 55 |
-
audio_paths: list[str] | None = None) -> list[dict]:
|
| 56 |
-
os.makedirs(out_dir, exist_ok=True)
|
| 57 |
-
results = []
|
| 58 |
-
for i, seq in enumerate(sequences, 1):
|
| 59 |
-
paths = [clip_map[c] for c in seq]
|
| 60 |
-
name = f"{prefix}_{i:02d}.mp4"
|
| 61 |
-
out = os.path.join(out_dir, name)
|
| 62 |
-
if audio_paths:
|
| 63 |
-
tmp = os.path.join(out_dir, f".tmp_{i:02d}.mp4")
|
| 64 |
-
_concat_reencode(paths, tmp)
|
| 65 |
-
track = audio_paths[(i - 1) % len(audio_paths)]
|
| 66 |
-
attach_audio(tmp, track, out)
|
| 67 |
-
os.remove(tmp)
|
| 68 |
-
else:
|
| 69 |
-
_concat_reencode(paths, out)
|
| 70 |
-
results.append({
|
| 71 |
-
"name": name, "path": out,
|
| 72 |
-
"sequence": seq, "size": os.path.getsize(out),
|
| 73 |
-
})
|
| 74 |
-
return results
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
def zip_variations(results: list[dict], zip_path: str) -> str:
|
| 78 |
-
with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zf:
|
| 79 |
-
for r in results:
|
| 80 |
-
zf.write(r["path"], arcname=r["name"])
|
| 81 |
-
return zip_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app/services/videogen/__init__.py
DELETED
|
File without changes
|
{app → backend}/__init__.py
RENAMED
|
File without changes
|
{app → backend/app}/api/auth.py
RENAMED
|
File without changes
|
backend/app/api/common.py
CHANGED
|
@@ -55,10 +55,7 @@ def list_inputs(sid: str):
|
|
| 55 |
@router.get("/files/{sid}/{path:path}")
|
| 56 |
def get_file(sid: str, path: str):
|
| 57 |
base = os.path.abspath(session_dir(sid))
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
full = os.path.abspath(os.path.join(base, *rel.split("/")))
|
| 61 |
-
# phải nằm trong phiên VÀ là FILE thật (đường dẫn thư mục -> 404, không 500)
|
| 62 |
-
if not full.startswith(base) or not os.path.isfile(full):
|
| 63 |
raise HTTPException(404, "file không tồn tại")
|
| 64 |
return FileResponse(full, filename=os.path.basename(full))
|
|
|
|
| 55 |
@router.get("/files/{sid}/{path:path}")
|
| 56 |
def get_file(sid: str, path: str):
|
| 57 |
base = os.path.abspath(session_dir(sid))
|
| 58 |
+
full = os.path.abspath(os.path.join(base, path))
|
| 59 |
+
if not full.startswith(base) or not os.path.exists(full):
|
|
|
|
|
|
|
|
|
|
| 60 |
raise HTTPException(404, "file không tồn tại")
|
| 61 |
return FileResponse(full, filename=os.path.basename(full))
|
backend/app/api/cut.py
CHANGED
|
@@ -4,18 +4,16 @@ Luồng FE:
|
|
| 4 |
1) upload video -> /upload (trả session + info)
|
| 5 |
2) /preview -> sinh khúc theo mode + thumbnail strip để xem trước/kéo điểm cắt
|
| 6 |
3) /execute -> cắt theo các điểm cắt (đã chỉnh tay) -> zip
|
| 7 |
-
4) /execute-batch -> cắt NHIỀU video SONG SONG, trả timeline RIÊNG cho từng video
|
| 8 |
"""
|
| 9 |
|
| 10 |
from __future__ import annotations
|
| 11 |
|
| 12 |
import os
|
| 13 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 14 |
|
| 15 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 19 |
from ..core.jobs import jobs
|
| 20 |
from ..services.cutter import cut as cutter
|
| 21 |
from ..services.cutter import modes
|
|
@@ -31,7 +29,7 @@ async def upload(file: UploadFile = File(...), session: str = Form(None)):
|
|
| 31 |
info = probe(path)
|
| 32 |
return {
|
| 33 |
"session": sid,
|
| 34 |
-
"file":
|
| 35 |
"info": {"duration": info.duration, "fps": info.fps,
|
| 36 |
"nb_frames": info.nb_frames,
|
| 37 |
"width": info.width, "height": info.height},
|
|
@@ -43,7 +41,7 @@ class PreviewReq(BaseModel):
|
|
| 43 |
session: str
|
| 44 |
file: str
|
| 45 |
mode: str = "even" # even | scene | highlight | smart
|
| 46 |
-
count: int | None = None # số khúc (even
|
| 47 |
min_len: float = 1.5
|
| 48 |
sensitivity: float = 0.5
|
| 49 |
top_ratio: float = 0.4
|
|
@@ -132,81 +130,53 @@ class BatchItem(BaseModel):
|
|
| 132 |
class ExecuteBatchReq(BaseModel):
|
| 133 |
session: str
|
| 134 |
videos: list[BatchItem]
|
| 135 |
-
workers: int = 3 # số video xử lý song song
|
| 136 |
|
| 137 |
|
| 138 |
@router.post("/execute-batch")
|
| 139 |
def execute_batch(req: ExecuteBatchReq):
|
| 140 |
-
"""Cắt NHIỀU video
|
| 141 |
|
| 142 |
-
|
| 143 |
-
(timeline riêng). Tên clip có tiền tố theo video để không đè nhau.
|
| 144 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
job = jobs.create("cut")
|
| 146 |
-
videos = list(req.videos)
|
| 147 |
-
workers = max(1, min(req.workers, len(videos) or 1))
|
| 148 |
|
| 149 |
def work(h):
|
| 150 |
out_dir = sub_dir(req.session, "clips")
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
it = items[vi]
|
| 160 |
-
base = os.path.splitext(os.path.basename(item.file))[0]
|
| 161 |
-
try:
|
| 162 |
-
src = os.path.join(session_dir(req.session), item.file)
|
| 163 |
-
info = probe(src)
|
| 164 |
-
if item.segments:
|
| 165 |
-
segs = [modes.Segment(i, s.start_frame, s.end_frame, info.fps)
|
| 166 |
-
for i, s in enumerate(item.segments)]
|
| 167 |
-
else:
|
| 168 |
-
segs = modes.build_segments(info, item.mode, n=item.count)
|
| 169 |
-
h.item_status(it, "running", detail=f"cắt {len(segs)} khúc")
|
| 170 |
-
clips = []
|
| 171 |
-
for i, seg in enumerate(segs):
|
| 172 |
name = f"v{vi+1}_clip_{i+1:03d}.mp4"
|
| 173 |
r = cutter.cut_segment(src, seg, os.path.join(out_dir, name))
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
except Exception as e:
|
| 184 |
-
h.item_status(it, "error", error=str(e))
|
| 185 |
-
return {"video": base, "file": item.file, "clips": [], "error": str(e)}
|
| 186 |
-
finally:
|
| 187 |
-
done_count[0] += 1
|
| 188 |
-
h.set_progress(done_count[0] / max(1, len(videos)))
|
| 189 |
-
|
| 190 |
-
with ThreadPoolExecutor(max_workers=workers) as ex:
|
| 191 |
-
futs = {ex.submit(process, vi, item): vi
|
| 192 |
-
for vi, item in enumerate(videos)}
|
| 193 |
-
for fut in as_completed(futs):
|
| 194 |
-
vi = futs[fut]
|
| 195 |
-
per_video[vi] = fut.result()
|
| 196 |
-
|
| 197 |
-
ordered = [per_video[vi] for vi in sorted(per_video)]
|
| 198 |
-
total_clips = sum(len(v["clips"]) for v in ordered)
|
| 199 |
-
return {"session": req.session, "videos": ordered, "clips": total_clips}
|
| 200 |
|
| 201 |
jobs.submit(job, work)
|
| 202 |
return {"job_id": job.id, "session": req.session}
|
| 203 |
|
| 204 |
|
| 205 |
-
class PushShuffleReq(BaseModel):
|
| 206 |
-
session: str
|
| 207 |
-
clips: list[str] # đường dẫn tương đối các khúc đã cắt
|
| 208 |
-
|
| 209 |
-
|
| 210 |
@router.post("/push-shuffle")
|
| 211 |
def push_shuffle(req: PushShuffleReq):
|
| 212 |
"""Đẩy các khúc đã cắt sang tool Xào: mỗi khúc thành 1 segment (seg_i).
|
|
|
|
| 4 |
1) upload video -> /upload (trả session + info)
|
| 5 |
2) /preview -> sinh khúc theo mode + thumbnail strip để xem trước/kéo điểm cắt
|
| 6 |
3) /execute -> cắt theo các điểm cắt (đã chỉnh tay) -> zip
|
|
|
|
| 7 |
"""
|
| 8 |
|
| 9 |
from __future__ import annotations
|
| 10 |
|
| 11 |
import os
|
|
|
|
| 12 |
|
| 13 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 14 |
from pydantic import BaseModel
|
| 15 |
|
| 16 |
+
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 17 |
from ..core.jobs import jobs
|
| 18 |
from ..services.cutter import cut as cutter
|
| 19 |
from ..services.cutter import modes
|
|
|
|
| 29 |
info = probe(path)
|
| 30 |
return {
|
| 31 |
"session": sid,
|
| 32 |
+
"file": os.path.relpath(path, session_dir(sid)),
|
| 33 |
"info": {"duration": info.duration, "fps": info.fps,
|
| 34 |
"nb_frames": info.nb_frames,
|
| 35 |
"width": info.width, "height": info.height},
|
|
|
|
| 41 |
session: str
|
| 42 |
file: str
|
| 43 |
mode: str = "even" # even | scene | highlight | smart
|
| 44 |
+
count: int | None = None # số khúc (even) — None để máy tự chia
|
| 45 |
min_len: float = 1.5
|
| 46 |
sensitivity: float = 0.5
|
| 47 |
top_ratio: float = 0.4
|
|
|
|
| 130 |
class ExecuteBatchReq(BaseModel):
|
| 131 |
session: str
|
| 132 |
videos: list[BatchItem]
|
|
|
|
| 133 |
|
| 134 |
|
| 135 |
@router.post("/execute-batch")
|
| 136 |
def execute_batch(req: ExecuteBatchReq):
|
| 137 |
+
"""Cắt NHIỀU video cùng lúc trong một phiên -> 1 danh sách clip gộp.
|
| 138 |
|
| 139 |
+
Tên clip có tiền tố theo video để không đè nhau: v{vi}_clip_{i}.mp4.
|
|
|
|
| 140 |
"""
|
| 141 |
+
plans = []
|
| 142 |
+
for vi, item in enumerate(req.videos):
|
| 143 |
+
src = os.path.join(session_dir(req.session), item.file)
|
| 144 |
+
info = probe(src)
|
| 145 |
+
if item.segments:
|
| 146 |
+
segs = [modes.Segment(i, s.start_frame, s.end_frame, info.fps)
|
| 147 |
+
for i, s in enumerate(item.segments)]
|
| 148 |
+
else:
|
| 149 |
+
segs = modes.build_segments(info, item.mode, n=item.count)
|
| 150 |
+
plans.append((vi, item.file, src, segs))
|
| 151 |
+
|
| 152 |
job = jobs.create("cut")
|
|
|
|
|
|
|
| 153 |
|
| 154 |
def work(h):
|
| 155 |
out_dir = sub_dir(req.session, "clips")
|
| 156 |
+
results = []
|
| 157 |
+
total = sum(len(p[3]) for p in plans) or 1
|
| 158 |
+
done = 0
|
| 159 |
+
for vi, fname, src, segs in plans:
|
| 160 |
+
base = os.path.splitext(os.path.basename(fname))[0]
|
| 161 |
+
for i, seg in enumerate(segs):
|
| 162 |
+
it = h.add_item(f"v{vi+1}_clip_{i+1:03d}")
|
| 163 |
+
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
name = f"v{vi+1}_clip_{i+1:03d}.mp4"
|
| 165 |
r = cutter.cut_segment(src, seg, os.path.join(out_dir, name))
|
| 166 |
+
results.append({"name": name, "duration": r.duration,
|
| 167 |
+
"size": r.size, "video": base,
|
| 168 |
+
"url": f"/api/files/{req.session}/clips/{name}"})
|
| 169 |
+
h.item_status(it, "done", detail=f"{r.duration}s", result=results[-1])
|
| 170 |
+
except Exception as e:
|
| 171 |
+
h.item_status(it, "error", error=str(e))
|
| 172 |
+
done += 1
|
| 173 |
+
h.set_progress(done / total)
|
| 174 |
+
return {"session": req.session, "clips": len(results)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
jobs.submit(job, work)
|
| 177 |
return {"job_id": job.id, "session": req.session}
|
| 178 |
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
@router.post("/push-shuffle")
|
| 181 |
def push_shuffle(req: PushShuffleReq):
|
| 182 |
"""Đẩy các khúc đã cắt sang tool Xào: mỗi khúc thành 1 segment (seg_i).
|
backend/app/api/download.py
CHANGED
|
@@ -9,7 +9,7 @@ from fastapi import APIRouter
|
|
| 9 |
from pydantic import BaseModel
|
| 10 |
|
| 11 |
from ..config import settings
|
| 12 |
-
from ..core.files import new_session, session_dir, sub_dir, zip_paths
|
| 13 |
from ..core.jobs import DONE, ERROR, RUNNING, jobs
|
| 14 |
from ..services.downloader.ydl import detect_source, download_one, parse_links
|
| 15 |
|
|
@@ -23,6 +23,7 @@ class ParseReq(BaseModel):
|
|
| 23 |
class StartReq(BaseModel):
|
| 24 |
urls: list[str]
|
| 25 |
zip_output: bool = True
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
@router.post("/parse")
|
|
@@ -42,45 +43,32 @@ def start(req: StartReq):
|
|
| 42 |
job = jobs.create("download")
|
| 43 |
|
| 44 |
def work(h):
|
| 45 |
-
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 46 |
-
import threading
|
| 47 |
items = [h.add_item(u, item_id=str(i)) for i, u in enumerate(req.urls)]
|
| 48 |
-
n = len(req.urls)
|
| 49 |
done_paths, failed = [], []
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
def fetch(i: int, url: str):
|
| 54 |
-
it = items[i]
|
| 55 |
if h.cancelled():
|
| 56 |
-
|
|
|
|
| 57 |
h.item_status(it, RUNNING, detail="đang tải")
|
| 58 |
h.log(f"[{i+1}/{n}] tải {url}")
|
| 59 |
-
res = download_one(url, out_dir)
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
h.set_progress(done[0] / max(1, n))
|
| 77 |
-
|
| 78 |
-
# Tải SONG SONG (network-bound) -> nhanh hơn nhiều khi nhiều link.
|
| 79 |
-
workers = max(1, min(5, n))
|
| 80 |
-
with ThreadPoolExecutor(max_workers=workers) as ex:
|
| 81 |
-
futs = [ex.submit(fetch, i, url) for i, url in enumerate(req.urls)]
|
| 82 |
-
for fut in as_completed(futs):
|
| 83 |
-
fut.result()
|
| 84 |
|
| 85 |
zip_rel = None
|
| 86 |
if req.zip_output and done_paths:
|
|
@@ -121,5 +109,5 @@ def push_to_tool(req: PushReq):
|
|
| 121 |
if os.path.exists(src):
|
| 122 |
d = os.path.join(dst, os.path.basename(src))
|
| 123 |
shutil.copy(src, d)
|
| 124 |
-
copied.append(
|
| 125 |
return {"session": new_sid, "target_tool": req.target_tool, "files": copied}
|
|
|
|
| 9 |
from pydantic import BaseModel
|
| 10 |
|
| 11 |
from ..config import settings
|
| 12 |
+
from ..core.files import new_session, session_dir, sub_dir, zip_paths
|
| 13 |
from ..core.jobs import DONE, ERROR, RUNNING, jobs
|
| 14 |
from ..services.downloader.ydl import detect_source, download_one, parse_links
|
| 15 |
|
|
|
|
| 23 |
class StartReq(BaseModel):
|
| 24 |
urls: list[str]
|
| 25 |
zip_output: bool = True
|
| 26 |
+
cookies_file: str | None = None
|
| 27 |
|
| 28 |
|
| 29 |
@router.post("/parse")
|
|
|
|
| 43 |
job = jobs.create("download")
|
| 44 |
|
| 45 |
def work(h):
|
|
|
|
|
|
|
| 46 |
items = [h.add_item(u, item_id=str(i)) for i, u in enumerate(req.urls)]
|
|
|
|
| 47 |
done_paths, failed = [], []
|
| 48 |
+
n = len(req.urls)
|
| 49 |
+
for i, url in enumerate(req.urls):
|
|
|
|
|
|
|
|
|
|
| 50 |
if h.cancelled():
|
| 51 |
+
break
|
| 52 |
+
it = items[i]
|
| 53 |
h.item_status(it, RUNNING, detail="đang tải")
|
| 54 |
h.log(f"[{i+1}/{n}] tải {url}")
|
| 55 |
+
res = download_one(url, out_dir, req.cookies_file)
|
| 56 |
+
if res.ok:
|
| 57 |
+
done_paths.append(res.path)
|
| 58 |
+
h.item_status(it, DONE, detail=res.title, result={
|
| 59 |
+
"path": os.path.relpath(res.path, session_dir(sid)),
|
| 60 |
+
"url": f"/api/files/{sid}/" + os.path.relpath(
|
| 61 |
+
res.path, session_dir(sid)).replace(os.sep, "/"),
|
| 62 |
+
"title": res.title, "ext": res.ext, "size": res.size,
|
| 63 |
+
"duration": res.duration, "thumbnail": res.thumbnail,
|
| 64 |
+
"source": detect_source(url),
|
| 65 |
+
})
|
| 66 |
+
h.log(f" ✔ {res.title} ({res.size} bytes)")
|
| 67 |
+
else:
|
| 68 |
+
failed.append({"url": url, "error": res.error})
|
| 69 |
+
h.item_status(it, ERROR, error=res.error)
|
| 70 |
+
h.log(f" ✗ LỖI: {res.error}", level="error")
|
| 71 |
+
h.set_progress((i + 1) / max(1, n))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
zip_rel = None
|
| 74 |
if req.zip_output and done_paths:
|
|
|
|
| 109 |
if os.path.exists(src):
|
| 110 |
d = os.path.join(dst, os.path.basename(src))
|
| 111 |
shutil.copy(src, d)
|
| 112 |
+
copied.append(os.path.relpath(d, session_dir(new_sid)))
|
| 113 |
return {"session": new_sid, "target_tool": req.target_tool, "files": copied}
|
backend/app/api/dub.py
CHANGED
|
@@ -15,7 +15,7 @@ import os
|
|
| 15 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 19 |
from ..core.jobs import jobs
|
| 20 |
from ..services.dubber import pipeline
|
| 21 |
|
|
@@ -30,7 +30,7 @@ LANGS = {"en", "vi", "fil"}
|
|
| 30 |
async def upload(file: UploadFile = File(...), session: str = Form(None)):
|
| 31 |
sid = session or new_session("dub")
|
| 32 |
path = save_upload(sid, file.filename, await file.read(), folder="inputs")
|
| 33 |
-
rel =
|
| 34 |
return {"session": sid, "file": rel, "name": os.path.basename(path),
|
| 35 |
"size": os.path.getsize(path)}
|
| 36 |
|
|
@@ -40,7 +40,7 @@ async def voice_sample(file: UploadFile = File(...), session: str = Form(None)):
|
|
| 40 |
"""File giọng mẫu (WAV/MP3) user tự up để clone. Tạo phiên nếu chưa có."""
|
| 41 |
sid = session or new_session("dub")
|
| 42 |
path = save_upload(sid, file.filename, await file.read(), folder="voice")
|
| 43 |
-
rel =
|
| 44 |
return {"session": sid, "file": rel, "name": os.path.basename(path)}
|
| 45 |
|
| 46 |
|
|
@@ -57,7 +57,6 @@ class DubReq(BaseModel):
|
|
| 57 |
preset: str = "" # tên preset nếu voice_source=preset
|
| 58 |
style: str = "natural"
|
| 59 |
engine: str = "auto"
|
| 60 |
-
gender: str = "female" # nam/nữ cho Edge TTS
|
| 61 |
multi_speaker: bool = False
|
| 62 |
|
| 63 |
|
|
@@ -78,7 +77,6 @@ def start(req: DubReq):
|
|
| 78 |
preset=req.preset,
|
| 79 |
style=req.style,
|
| 80 |
engine=req.engine,
|
| 81 |
-
gender=req.gender,
|
| 82 |
multi_speaker=req.multi_speaker,
|
| 83 |
)
|
| 84 |
|
|
@@ -136,8 +134,7 @@ class TtsReq(BaseModel):
|
|
| 136 |
speaker_file: str = "" # rel path nếu voice_source=upload (clone)
|
| 137 |
preset: str = "" # tên giọng có sẵn
|
| 138 |
style: str = "natural"
|
| 139 |
-
engine: str = "auto" # auto | vieneu | xtts | voxcpm
|
| 140 |
-
gender: str = "female" # nam/nữ cho Edge TTS
|
| 141 |
|
| 142 |
|
| 143 |
@router.post("/tts")
|
|
@@ -153,7 +150,7 @@ def tts_from_text(req: TtsReq):
|
|
| 153 |
opt = pipeline.TtsOptions(
|
| 154 |
text=req.text, target_lang=target, voice_source=req.voice_source,
|
| 155 |
speaker_wav=speaker_wav, preset=req.preset, style=req.style,
|
| 156 |
-
engine=req.engine
|
| 157 |
|
| 158 |
job = jobs.create("tts")
|
| 159 |
|
|
|
|
| 15 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 16 |
from pydantic import BaseModel
|
| 17 |
|
| 18 |
+
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 19 |
from ..core.jobs import jobs
|
| 20 |
from ..services.dubber import pipeline
|
| 21 |
|
|
|
|
| 30 |
async def upload(file: UploadFile = File(...), session: str = Form(None)):
|
| 31 |
sid = session or new_session("dub")
|
| 32 |
path = save_upload(sid, file.filename, await file.read(), folder="inputs")
|
| 33 |
+
rel = os.path.relpath(path, session_dir(sid))
|
| 34 |
return {"session": sid, "file": rel, "name": os.path.basename(path),
|
| 35 |
"size": os.path.getsize(path)}
|
| 36 |
|
|
|
|
| 40 |
"""File giọng mẫu (WAV/MP3) user tự up để clone. Tạo phiên nếu chưa có."""
|
| 41 |
sid = session or new_session("dub")
|
| 42 |
path = save_upload(sid, file.filename, await file.read(), folder="voice")
|
| 43 |
+
rel = os.path.relpath(path, session_dir(sid))
|
| 44 |
return {"session": sid, "file": rel, "name": os.path.basename(path)}
|
| 45 |
|
| 46 |
|
|
|
|
| 57 |
preset: str = "" # tên preset nếu voice_source=preset
|
| 58 |
style: str = "natural"
|
| 59 |
engine: str = "auto"
|
|
|
|
| 60 |
multi_speaker: bool = False
|
| 61 |
|
| 62 |
|
|
|
|
| 77 |
preset=req.preset,
|
| 78 |
style=req.style,
|
| 79 |
engine=req.engine,
|
|
|
|
| 80 |
multi_speaker=req.multi_speaker,
|
| 81 |
)
|
| 82 |
|
|
|
|
| 134 |
speaker_file: str = "" # rel path nếu voice_source=upload (clone)
|
| 135 |
preset: str = "" # tên giọng có sẵn
|
| 136 |
style: str = "natural"
|
| 137 |
+
engine: str = "auto" # auto | vieneu | xtts | voxcpm
|
|
|
|
| 138 |
|
| 139 |
|
| 140 |
@router.post("/tts")
|
|
|
|
| 150 |
opt = pipeline.TtsOptions(
|
| 151 |
text=req.text, target_lang=target, voice_source=req.voice_source,
|
| 152 |
speaker_wav=speaker_wav, preset=req.preset, style=req.style,
|
| 153 |
+
engine=req.engine)
|
| 154 |
|
| 155 |
job = jobs.create("tts")
|
| 156 |
|
{app → backend/app}/api/listing.py
RENAMED
|
File without changes
|
backend/app/api/shuffle.py
CHANGED
|
@@ -17,7 +17,7 @@ import os
|
|
| 17 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 18 |
from pydantic import BaseModel
|
| 19 |
|
| 20 |
-
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 21 |
from ..core.jobs import jobs
|
| 22 |
from ..services.shuffler import engine
|
| 23 |
from ..services.shuffler import render as shuffle_render
|
|
@@ -37,7 +37,7 @@ async def upload(
|
|
| 37 |
sid = session or new_session("shuffle")
|
| 38 |
folder = f"seg_{segment}"
|
| 39 |
path = save_upload(sid, file.filename, await file.read(), folder=folder)
|
| 40 |
-
rel =
|
| 41 |
return {
|
| 42 |
"session": sid, "segment": segment, "file": rel,
|
| 43 |
"name": os.path.basename(path), "size": os.path.getsize(path),
|
|
@@ -52,7 +52,7 @@ async def upload_audio(
|
|
| 52 |
):
|
| 53 |
"""Tải 1 track âm thanh dùng để lồng vào các biến thể."""
|
| 54 |
path = save_upload(session, file.filename, await file.read(), folder="audio")
|
| 55 |
-
rel =
|
| 56 |
return {
|
| 57 |
"session": session, "file": rel,
|
| 58 |
"name": os.path.basename(path), "size": os.path.getsize(path),
|
|
@@ -153,56 +153,36 @@ def generate(req: ShuffleReq):
|
|
| 153 |
job = jobs.create("shuffle")
|
| 154 |
|
| 155 |
def work(h):
|
| 156 |
-
|
| 157 |
-
n = len(sequences)
|
| 158 |
-
h.log(f"Sẽ render {n} biến thể"
|
| 159 |
+ (f" + lồng {len(audio_paths)} track âm thanh" if audio_paths else ""))
|
| 160 |
out_dir = sub_dir(req.session, "variations")
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
results[i] = {"name": name, "path": out, "sequence": seq, "size": size}
|
| 186 |
-
h.item_status(it, "done", detail=f"{size//1024} KB", result={
|
| 187 |
-
"name": name, "size": size, "sequence": seq,
|
| 188 |
-
"url": f"/api/files/{req.session}/variations/{name}"})
|
| 189 |
-
|
| 190 |
-
with ThreadPoolExecutor(max_workers=4) as ex:
|
| 191 |
-
futs = {ex.submit(render_one, i, seq): i
|
| 192 |
-
for i, seq in enumerate(sequences, 1)}
|
| 193 |
-
for fut in as_completed(futs):
|
| 194 |
-
i = futs[fut]
|
| 195 |
-
try:
|
| 196 |
-
fut.result()
|
| 197 |
-
except Exception as e:
|
| 198 |
-
h.item_status(items[i], "error", error=str(e))
|
| 199 |
-
done[0] += 1
|
| 200 |
-
h.set_progress(done[0] / max(1, n))
|
| 201 |
-
|
| 202 |
-
ordered = [results[i] for i in sorted(results)]
|
| 203 |
zp = os.path.join(session_dir(req.session), "variations.zip")
|
| 204 |
-
shuffle_render.zip_variations(
|
| 205 |
-
return {"session": req.session, "count": len(
|
| 206 |
|
| 207 |
jobs.submit(job, work)
|
| 208 |
return {"job_id": job.id, "session": req.session, "planned": len(sequences)}
|
|
|
|
| 17 |
from fastapi import APIRouter, File, Form, UploadFile
|
| 18 |
from pydantic import BaseModel
|
| 19 |
|
| 20 |
+
from ..core.files import new_session, session_dir, sub_dir, save_upload
|
| 21 |
from ..core.jobs import jobs
|
| 22 |
from ..services.shuffler import engine
|
| 23 |
from ..services.shuffler import render as shuffle_render
|
|
|
|
| 37 |
sid = session or new_session("shuffle")
|
| 38 |
folder = f"seg_{segment}"
|
| 39 |
path = save_upload(sid, file.filename, await file.read(), folder=folder)
|
| 40 |
+
rel = os.path.relpath(path, session_dir(sid))
|
| 41 |
return {
|
| 42 |
"session": sid, "segment": segment, "file": rel,
|
| 43 |
"name": os.path.basename(path), "size": os.path.getsize(path),
|
|
|
|
| 52 |
):
|
| 53 |
"""Tải 1 track âm thanh dùng để lồng vào các biến thể."""
|
| 54 |
path = save_upload(session, file.filename, await file.read(), folder="audio")
|
| 55 |
+
rel = os.path.relpath(path, session_dir(session))
|
| 56 |
return {
|
| 57 |
"session": session, "file": rel,
|
| 58 |
"name": os.path.basename(path), "size": os.path.getsize(path),
|
|
|
|
| 153 |
job = jobs.create("shuffle")
|
| 154 |
|
| 155 |
def work(h):
|
| 156 |
+
h.log(f"Sẽ render {len(sequences)} biến thể"
|
|
|
|
|
|
|
| 157 |
+ (f" + lồng {len(audio_paths)} track âm thanh" if audio_paths else ""))
|
| 158 |
out_dir = sub_dir(req.session, "variations")
|
| 159 |
+
results = []
|
| 160 |
+
n = len(sequences)
|
| 161 |
+
for i, seq in enumerate(sequences, 1):
|
| 162 |
+
it = h.add_item(f"Variation_{i:02d}")
|
| 163 |
+
try:
|
| 164 |
+
paths = [clip_map[c] for c in seq]
|
| 165 |
+
name = f"Variation_{i:02d}.mp4"
|
| 166 |
+
out = os.path.join(out_dir, name)
|
| 167 |
+
if audio_paths:
|
| 168 |
+
tmp = os.path.join(out_dir, f".tmp_{i:02d}.mp4")
|
| 169 |
+
shuffle_render._concat_reencode(paths, tmp)
|
| 170 |
+
track = audio_paths[(i - 1) % len(audio_paths)]
|
| 171 |
+
shuffle_render.attach_audio(tmp, track, out)
|
| 172 |
+
os.remove(tmp)
|
| 173 |
+
else:
|
| 174 |
+
shuffle_render._concat_reencode(paths, out)
|
| 175 |
+
size = os.path.getsize(out)
|
| 176 |
+
results.append({"name": name, "path": out, "sequence": seq, "size": size})
|
| 177 |
+
h.item_status(it, "done", detail=f"{size//1024} KB", result={
|
| 178 |
+
"name": name, "size": size, "sequence": seq,
|
| 179 |
+
"url": f"/api/files/{req.session}/variations/{name}"})
|
| 180 |
+
except Exception as e:
|
| 181 |
+
h.item_status(it, "error", error=str(e))
|
| 182 |
+
h.set_progress(i / max(1, n))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
zp = os.path.join(session_dir(req.session), "variations.zip")
|
| 184 |
+
shuffle_render.zip_variations(results, zp)
|
| 185 |
+
return {"session": req.session, "count": len(results), "zip": "variations.zip"}
|
| 186 |
|
| 187 |
jobs.submit(job, work)
|
| 188 |
return {"job_id": job.id, "session": req.session, "planned": len(sequences)}
|
{app → backend/app}/api/video.py
RENAMED
|
File without changes
|
backend/app/config.py
CHANGED
|
@@ -7,12 +7,12 @@ import tempfile
|
|
| 7 |
from dataclasses import dataclass, field
|
| 8 |
|
| 9 |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 10 |
-
BACKEND_DIR =
|
| 11 |
try:
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
-
for _cand in (os.path.join(BASE_DIR, ".env"),
|
| 14 |
-
os.path.join(
|
| 15 |
-
os.path.join(os.getcwd(), ".env")):
|
| 16 |
if os.path.exists(_cand):
|
| 17 |
load_dotenv(_cand, override=True)
|
| 18 |
break
|
|
@@ -36,14 +36,28 @@ class Settings:
|
|
| 36 |
# API keys cho pipeline lồng tiếng
|
| 37 |
GROQ_API_KEY: str = field(default_factory=lambda: _env("GROQ_API_KEY"))
|
| 38 |
GOOGLE_API_KEY: str = field(default_factory=lambda: _env("GOOGLE_API_KEY"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# model TTS / config
|
| 41 |
DEMUCS_MODEL: str = field(default_factory=lambda: _env("DEMUCS_MODEL", "htdemucs"))
|
| 42 |
WHISPER_MODEL: str = field(default_factory=lambda: _env("WHISPER_MODEL", "whisper-large-v3"))
|
| 43 |
GEMINI_MODEL: str = field(default_factory=lambda: _env("GEMINI_MODEL", "gemini-2.0-flash"))
|
| 44 |
GROQ_LLM_FALLBACK: str = field(default_factory=lambda: _env("GROQ_LLM_FALLBACK", "openai/gpt-oss-20b"))
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
LOCAL_WHISPER_MODEL: str = field(default_factory=lambda: _env("LOCAL_WHISPER_MODEL", "base"))
|
| 48 |
|
| 49 |
# giới hạn
|
|
|
|
| 7 |
from dataclasses import dataclass, field
|
| 8 |
|
| 9 |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 10 |
+
BACKEND_DIR = BASE_DIR
|
| 11 |
try:
|
| 12 |
from dotenv import load_dotenv
|
| 13 |
+
for _cand in (os.path.join(BASE_DIR, ".env"),
|
| 14 |
+
os.path.join(os.path.dirname(BASE_DIR), ".env"),
|
| 15 |
+
os.path.join(os.getcwd(), ".env")):
|
| 16 |
if os.path.exists(_cand):
|
| 17 |
load_dotenv(_cand, override=True)
|
| 18 |
break
|
|
|
|
| 36 |
# API keys cho pipeline lồng tiếng
|
| 37 |
GROQ_API_KEY: str = field(default_factory=lambda: _env("GROQ_API_KEY"))
|
| 38 |
GOOGLE_API_KEY: str = field(default_factory=lambda: _env("GOOGLE_API_KEY"))
|
| 39 |
+
# API keys cho Listing ảnh
|
| 40 |
+
OPENAI_API_KEY: str = field(default_factory=lambda: _env("OPENAI_API_KEY"))
|
| 41 |
+
OPENAI_IMAGE_API_KEY: str = field(default_factory=lambda: _env("OPENAI_IMAGE_API_KEY"))
|
| 42 |
+
|
| 43 |
+
# Database cho user auth (để trống thì dùng JSON file)
|
| 44 |
+
DATABASE_URL: str = field(default_factory=lambda: _env("DATABASE_URL", ""))
|
| 45 |
+
|
| 46 |
+
# Danh sách user được dùng key chung (cách nhau bằng dấu phẩy)
|
| 47 |
+
AUTHORIZED_USERS: list[str] = field(
|
| 48 |
+
default_factory=lambda: [u.strip() for u in _env("AUTHORIZED_USERS", "").split(",") if u.strip()]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# Video Gen (Gradio proxy)
|
| 52 |
+
VIDEO_ADMIN_TOKEN: str = field(default_factory=lambda: _env("VIDEO_ADMIN_TOKEN"))
|
| 53 |
|
| 54 |
# model TTS / config
|
| 55 |
DEMUCS_MODEL: str = field(default_factory=lambda: _env("DEMUCS_MODEL", "htdemucs"))
|
| 56 |
WHISPER_MODEL: str = field(default_factory=lambda: _env("WHISPER_MODEL", "whisper-large-v3"))
|
| 57 |
GEMINI_MODEL: str = field(default_factory=lambda: _env("GEMINI_MODEL", "gemini-2.0-flash"))
|
| 58 |
GROQ_LLM_FALLBACK: str = field(default_factory=lambda: _env("GROQ_LLM_FALLBACK", "openai/gpt-oss-20b"))
|
| 59 |
+
|
| 60 |
+
# ASR chạy LOCAL (faster-whisper) khi không có/không gọi được Groq
|
| 61 |
LOCAL_WHISPER_MODEL: str = field(default_factory=lambda: _env("LOCAL_WHISPER_MODEL", "base"))
|
| 62 |
|
| 63 |
# giới hạn
|
backend/app/core/files.py
CHANGED
|
@@ -45,12 +45,6 @@ def sub_dir(sid: str, *parts: str) -> str:
|
|
| 45 |
return d
|
| 46 |
|
| 47 |
|
| 48 |
-
def rel_path(full: str, sid: str) -> str:
|
| 49 |
-
"""Đường dẫn tương đối trong phiên, LUÔN dùng "/" (an toàn cho URL/JSON,
|
| 50 |
-
tránh dấu "\\" của Windows làm hỏng <video src> / khớp đường dẫn)."""
|
| 51 |
-
return os.path.relpath(full, session_dir(sid)).replace(os.sep, "/")
|
| 52 |
-
|
| 53 |
-
|
| 54 |
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:
|
| 55 |
d = sub_dir(sid, folder)
|
| 56 |
safe = os.path.basename(filename).replace("/", "_")
|
|
|
|
| 45 |
return d
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def save_upload(sid: str, filename: str, data: bytes, folder: str = "uploads") -> str:
|
| 49 |
d = sub_dir(sid, folder)
|
| 50 |
safe = os.path.basename(filename).replace("/", "_")
|
backend/app/core/jobs.py
CHANGED
|
@@ -5,7 +5,6 @@ Không phụ thuộc FastAPI -> test độc lập được. Dùng ThreadPool đ
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
| 8 |
-
import os
|
| 9 |
import threading
|
| 10 |
import time
|
| 11 |
import traceback
|
|
@@ -102,11 +101,7 @@ class JobHandle:
|
|
| 102 |
|
| 103 |
|
| 104 |
class JobManager:
|
| 105 |
-
def __init__(self, max_workers: int
|
| 106 |
-
# nhiều tool/job chạy CÙNG LÚC: cho phép kha khá job song song (mỗi tool 1 job).
|
| 107 |
-
# I/O (tải/encode) chủ yếu ở subprocess ffmpeg/yt-dlp nên oversubscribe nhẹ là ổn.
|
| 108 |
-
if max_workers is None:
|
| 109 |
-
max_workers = max(8, (os.cpu_count() or 4))
|
| 110 |
self._jobs: dict[str, Job] = {}
|
| 111 |
self._lock = threading.RLock()
|
| 112 |
self._pool = ThreadPoolExecutor(max_workers=max_workers)
|
|
|
|
| 5 |
|
| 6 |
from __future__ import annotations
|
| 7 |
|
|
|
|
| 8 |
import threading
|
| 9 |
import time
|
| 10 |
import traceback
|
|
|
|
| 101 |
|
| 102 |
|
| 103 |
class JobManager:
|
| 104 |
+
def __init__(self, max_workers: int = 4):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
self._jobs: dict[str, Job] = {}
|
| 106 |
self._lock = threading.RLock()
|
| 107 |
self._pool = ThreadPoolExecutor(max_workers=max_workers)
|
backend/app/main.py
CHANGED
|
@@ -1,7 +1,6 @@
|
|
| 1 |
"""TNT Group - AI Content Create — FastAPI entrypoint.
|
| 2 |
|
| 3 |
-
Chạy:
|
| 4 |
-
cd backend
|
| 5 |
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
| 6 |
|
| 7 |
Cần Python 3.10+ và ffmpeg/ffprobe trong PATH. Các thư viện nặng
|
|
@@ -12,14 +11,20 @@ tool tương ứng — xem requirements.txt.
|
|
| 12 |
from __future__ import annotations
|
| 13 |
|
| 14 |
import os
|
|
|
|
| 15 |
|
| 16 |
from fastapi import FastAPI
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 18 |
from fastapi.staticfiles import StaticFiles
|
| 19 |
|
| 20 |
from .config import settings
|
| 21 |
from .core.files import cleanup_old
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
app = FastAPI(title="TNT Group - AI Content Create", version="1.0.0")
|
| 25 |
|
|
@@ -36,6 +41,9 @@ app.include_router(download.router)
|
|
| 36 |
app.include_router(cut.router)
|
| 37 |
app.include_router(shuffle.router)
|
| 38 |
app.include_router(dub.router)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
|
| 41 |
@app.get("/api/health")
|
|
@@ -43,6 +51,11 @@ def health():
|
|
| 43 |
return {"ok": True, "name": "tnt-content-ai", "version": "1.0.0"}
|
| 44 |
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
@app.on_event("startup")
|
| 47 |
def _startup():
|
| 48 |
os.makedirs(settings.STORAGE_DIR, exist_ok=True)
|
|
@@ -53,7 +66,17 @@ def _startup():
|
|
| 53 |
|
| 54 |
|
| 55 |
# Phục vụ frontend đã build (frontend/dist) nếu có — gắn CUỐI để không nuốt /api.
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
app.mount("/", StaticFiles(directory=_dist, html=True), name="frontend")
|
|
|
|
| 1 |
"""TNT Group - AI Content Create — FastAPI entrypoint.
|
| 2 |
|
| 3 |
+
Chạy (từ thư mục gốc project):
|
|
|
|
| 4 |
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
|
| 5 |
|
| 6 |
Cần Python 3.10+ và ffmpeg/ffprobe trong PATH. Các thư viện nặng
|
|
|
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
import os
|
| 14 |
+
import sys
|
| 15 |
|
| 16 |
from fastapi import FastAPI
|
| 17 |
from fastapi.middleware.cors import CORSMiddleware
|
| 18 |
+
from fastapi.responses import RedirectResponse, FileResponse
|
| 19 |
from fastapi.staticfiles import StaticFiles
|
| 20 |
|
| 21 |
from .config import settings
|
| 22 |
from .core.files import cleanup_old
|
| 23 |
+
_here = os.path.dirname(os.path.abspath(__file__))
|
| 24 |
+
_parent = os.path.dirname(_here)
|
| 25 |
+
sys.path.insert(0, _parent)
|
| 26 |
+
|
| 27 |
+
from .api import common, download, cut, shuffle, dub, listing, video, auth
|
| 28 |
|
| 29 |
app = FastAPI(title="TNT Group - AI Content Create", version="1.0.0")
|
| 30 |
|
|
|
|
| 41 |
app.include_router(cut.router)
|
| 42 |
app.include_router(shuffle.router)
|
| 43 |
app.include_router(dub.router)
|
| 44 |
+
app.include_router(listing.router)
|
| 45 |
+
app.include_router(video.router)
|
| 46 |
+
app.include_router(auth.router)
|
| 47 |
|
| 48 |
|
| 49 |
@app.get("/api/health")
|
|
|
|
| 51 |
return {"ok": True, "name": "tnt-content-ai", "version": "1.0.0"}
|
| 52 |
|
| 53 |
|
| 54 |
+
@app.get("/logo.png")
|
| 55 |
+
def logo_png():
|
| 56 |
+
return RedirectResponse(url="/logo.svg")
|
| 57 |
+
|
| 58 |
+
|
| 59 |
@app.on_event("startup")
|
| 60 |
def _startup():
|
| 61 |
os.makedirs(settings.STORAGE_DIR, exist_ok=True)
|
|
|
|
| 66 |
|
| 67 |
|
| 68 |
# Phục vụ frontend đã build (frontend/dist) nếu có — gắn CUỐI để không nuốt /api.
|
| 69 |
+
_here = os.path.dirname(os.path.abspath(__file__))
|
| 70 |
+
_candidates = [
|
| 71 |
+
os.path.join(_here, "..", "..", "frontend", "dist"), # local dev
|
| 72 |
+
os.path.join(_here, "..", "frontend", "dist"), # HF Spaces
|
| 73 |
+
"/app/frontend/dist", # Docker
|
| 74 |
+
]
|
| 75 |
+
_dist = ""
|
| 76 |
+
for p in _candidates:
|
| 77 |
+
p = os.path.abspath(p)
|
| 78 |
+
if os.path.isdir(p):
|
| 79 |
+
_dist = p
|
| 80 |
+
break
|
| 81 |
+
if _dist and os.path.isdir(_dist):
|
| 82 |
app.mount("/", StaticFiles(directory=_dist, html=True), name="frontend")
|
{app/core → backend/app/services/auth}/__init__.py
RENAMED
|
File without changes
|
{app → backend/app}/services/auth/manager.py
RENAMED
|
File without changes
|
{app → backend/app}/services/auth/storage.py
RENAMED
|
File without changes
|
backend/app/services/cutter/modes.py
CHANGED
|
@@ -1,32 +1,20 @@
|
|
| 1 |
"""4 chế độ cắt -> trả về danh sách KHÚC (start_frame, end_frame) frame-accurate.
|
| 2 |
|
| 3 |
- even : chia N khúc bằng nhau.
|
| 4 |
-
- scene : nhận diện CHUYỂN CẢNH
|
| 5 |
- highlight : tìm đoạn CHUYỂN ĐỘNG MẠNH (motion energy), tách thành khúc.
|
| 6 |
-
- smart :
|
| 7 |
-
|
| 8 |
-
Frame được đọc THẲNG qua pipe ffmpeg (không cần OpenCV) nên chạy được trên môi
|
| 9 |
-
trường tối giản. Đặc trưng (motion/màu/CLIP) trích MỘT lần rồi cache theo
|
| 10 |
-
(path, mtime, sample_fps, use_clip) để kéo tham số/preview lại gần như tức thì.
|
| 11 |
|
| 12 |
Boundary = (start_frame, end_frame) — end_frame exclusive theo frame index.
|
| 13 |
"""
|
| 14 |
|
| 15 |
from __future__ import annotations
|
| 16 |
|
| 17 |
-
import
|
| 18 |
-
from dataclasses import dataclass, field
|
| 19 |
|
| 20 |
import numpy as np
|
| 21 |
|
| 22 |
-
from .probe import
|
| 23 |
-
|
| 24 |
-
# Lấy mẫu để phân tích: ~3 frame/giây là đủ cho scene/highlight/smart mà vẫn nhanh.
|
| 25 |
-
SAMPLE_FPS = 3.0
|
| 26 |
-
_FEATURE_CACHE: dict[tuple, "FrameFeatures"] = {}
|
| 27 |
-
_CACHE_MAX = 6
|
| 28 |
-
_CLIP_OK: bool | None = None # cache: CLIP (torch+open_clip) có dùng được không
|
| 29 |
-
_CLIP_MODEL = None # cache model CLIP đã nạp (tránh nạp lại mỗi video)
|
| 30 |
|
| 31 |
|
| 32 |
@dataclass
|
|
@@ -68,14 +56,6 @@ def _segments_from_cuts(cut_frames: list[int], total: int, fps: float) -> list[S
|
|
| 68 |
return segs
|
| 69 |
|
| 70 |
|
| 71 |
-
def _segments_from_time_cuts(cut_times: list[float], info: VideoInfo) -> list[Segment]:
|
| 72 |
-
"""Quy đổi các điểm cắt theo giây -> frame index rồi dựng Segment."""
|
| 73 |
-
total = info.nb_frames
|
| 74 |
-
fps = info.fps
|
| 75 |
-
cuts = [int(round(t * fps)) for t in cut_times]
|
| 76 |
-
return _segments_from_cuts(cuts, total, fps)
|
| 77 |
-
|
| 78 |
-
|
| 79 |
# ----------------------------------------------------------------- even ------
|
| 80 |
|
| 81 |
def even_cut(info: VideoInfo, n: int) -> list[Segment]:
|
|
@@ -97,203 +77,83 @@ def auto_segment_count(info: VideoInfo, target_len: float = 6.0) -> int:
|
|
| 97 |
return max(1, round(info.duration / max(0.5, target_len)))
|
| 98 |
|
| 99 |
|
| 100 |
-
# -----------------------------------------------
|
| 101 |
-
|
| 102 |
-
@dataclass
|
| 103 |
-
class FrameFeatures:
|
| 104 |
-
"""Đặc trưng đã trích cho 1 video ở 1 sample_fps. Tái dùng cho mọi tham số."""
|
| 105 |
-
sample_fps: float
|
| 106 |
-
times: np.ndarray = field(default_factory=lambda: np.zeros(0))
|
| 107 |
-
motion: np.ndarray = field(default_factory=lambda: np.zeros(0)) # [0..1]
|
| 108 |
-
color_hist: np.ndarray = field(default_factory=lambda: np.zeros((0, 48)))
|
| 109 |
-
clip_emb: np.ndarray | None = None
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def _scaled_width(info: VideoInfo, target_h: int) -> int:
|
| 113 |
-
if info.height <= 0 or info.width <= 0:
|
| 114 |
-
w = int(target_h * 16 / 9)
|
| 115 |
-
else:
|
| 116 |
-
w = int(round(target_h * info.width / info.height))
|
| 117 |
-
return max(2, w - (w % 2))
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def _iter_frames_rgb(info: VideoInfo, sample_fps: float, height: int):
|
| 121 |
-
"""Sinh từng frame RGB (H×W×3) numpy uint8, lấy mẫu theo sample_fps.
|
| 122 |
-
|
| 123 |
-
Đọc rawvideo trực tiếp từ stdout của ffmpeg -> không ghi đĩa, không cần cv2.
|
| 124 |
-
"""
|
| 125 |
-
width = _scaled_width(info, height)
|
| 126 |
-
cmd = [
|
| 127 |
-
FFMPEG, "-hide_banner", "-loglevel", "error", "-i", info.path,
|
| 128 |
-
"-vf", f"fps={sample_fps},scale={width}:{height}",
|
| 129 |
-
"-pix_fmt", "rgb24", "-f", "rawvideo", "pipe:1",
|
| 130 |
-
]
|
| 131 |
-
frame_bytes = width * height * 3
|
| 132 |
-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
| 133 |
-
try:
|
| 134 |
-
while True:
|
| 135 |
-
buf = proc.stdout.read(frame_bytes)
|
| 136 |
-
if not buf or len(buf) < frame_bytes:
|
| 137 |
-
break
|
| 138 |
-
yield np.frombuffer(buf, dtype=np.uint8).reshape((height, width, 3))
|
| 139 |
-
finally:
|
| 140 |
-
if proc.stdout:
|
| 141 |
-
proc.stdout.close()
|
| 142 |
-
proc.wait()
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
def _color_hist(rgb: np.ndarray) -> np.ndarray:
|
| 146 |
-
"""Histogram 16 bin/kênh, chuẩn hóa -> vector 48 chiều."""
|
| 147 |
-
parts = [np.histogram(rgb[..., c], bins=16, range=(0, 255))[0] / rgb[..., c].size
|
| 148 |
-
for c in range(3)]
|
| 149 |
-
return np.concatenate(parts)
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
def _clip_available() -> bool:
|
| 153 |
-
"""Có thể chạy CLIP không (đủ torch + open_clip + PIL)? Cache kết quả."""
|
| 154 |
-
global _CLIP_OK
|
| 155 |
-
if _CLIP_OK is not None:
|
| 156 |
-
return _CLIP_OK
|
| 157 |
-
try:
|
| 158 |
-
import open_clip # noqa: F401
|
| 159 |
-
import torch # noqa: F401
|
| 160 |
-
from PIL import Image # noqa: F401
|
| 161 |
-
_CLIP_OK = True
|
| 162 |
-
except Exception:
|
| 163 |
-
_CLIP_OK = False
|
| 164 |
-
return _CLIP_OK
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
def _load_clip(): # pragma: no cover - cần model local
|
| 168 |
-
"""Nạp model CLIP MỘT lần rồi cache (model, preprocess, device, torch)."""
|
| 169 |
-
global _CLIP_MODEL
|
| 170 |
-
if _CLIP_MODEL is not None:
|
| 171 |
-
return _CLIP_MODEL
|
| 172 |
-
import open_clip
|
| 173 |
-
import torch
|
| 174 |
-
dev = "cuda" if torch.cuda.is_available() else "cpu"
|
| 175 |
-
model, _, prep = open_clip.create_model_and_transforms(
|
| 176 |
-
"ViT-B-32", pretrained="laion2b_s34b_b79k")
|
| 177 |
-
model = model.to(dev).eval()
|
| 178 |
-
_CLIP_MODEL = (model, prep, dev, torch)
|
| 179 |
-
return _CLIP_MODEL
|
| 180 |
-
|
| 181 |
|
| 182 |
-
def
|
| 183 |
-
"""
|
| 184 |
-
|
| 185 |
-
from PIL import Image
|
| 186 |
-
model, prep, dev, torch = _load_clip()
|
| 187 |
-
except Exception:
|
| 188 |
-
return None
|
| 189 |
-
try:
|
| 190 |
-
embs: list[np.ndarray] = []
|
| 191 |
-
with torch.no_grad():
|
| 192 |
-
for i in range(0, len(frames), 64):
|
| 193 |
-
batch = [prep(Image.fromarray(f)) for f in frames[i:i + 64]]
|
| 194 |
-
x = torch.stack(batch).to(dev)
|
| 195 |
-
v = model.encode_image(x)
|
| 196 |
-
v = v / v.norm(dim=-1, keepdim=True)
|
| 197 |
-
embs.append(v.cpu().numpy())
|
| 198 |
-
return np.concatenate(embs, axis=0) if embs else None
|
| 199 |
-
except Exception:
|
| 200 |
-
return None
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
motion.append(0.0 if prev_gray is None
|
| 226 |
-
else float(np.abs(gray - prev_gray).mean()))
|
| 227 |
-
prev_gray = gray
|
| 228 |
-
hists.append(_color_hist(rgb))
|
| 229 |
-
if want_clip:
|
| 230 |
-
rgb_frames.append(rgb)
|
| 231 |
-
|
| 232 |
-
n = len(motion)
|
| 233 |
-
feats = FrameFeatures(
|
| 234 |
-
sample_fps=sample_fps,
|
| 235 |
-
times=np.array([i / sample_fps for i in range(n)], dtype=np.float64),
|
| 236 |
-
motion=_norm01(motion),
|
| 237 |
-
color_hist=np.array(hists) if hists else np.zeros((0, 48)),
|
| 238 |
-
)
|
| 239 |
-
if want_clip and rgb_frames:
|
| 240 |
-
feats.clip_emb = _clip_embed(rgb_frames)
|
| 241 |
-
|
| 242 |
-
if key is not None:
|
| 243 |
-
if len(_FEATURE_CACHE) >= _CACHE_MAX:
|
| 244 |
-
_FEATURE_CACHE.pop(next(iter(_FEATURE_CACHE)))
|
| 245 |
-
_FEATURE_CACHE[key] = feats
|
| 246 |
-
return feats
|
| 247 |
-
|
| 248 |
|
| 249 |
-
# -------------------------------------------------------- scene / highlight --
|
| 250 |
|
| 251 |
def scene_cut(info: VideoInfo, min_len: float = 1.5, sensitivity: float = 0.5) -> list[Segment]:
|
| 252 |
-
"""Cắt tại điểm chuyển cảnh
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
if not cut_times:
|
| 257 |
-
return even_cut(info, max(1, auto_segment_count(info)))
|
| 258 |
-
segs = _segments_from_time_cuts(cut_times, info)
|
| 259 |
-
return _merge_short(segs, info.fps, min_len)
|
| 260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
import re
|
| 265 |
-
proc = subprocess.run(
|
| 266 |
-
[FFMPEG, "-hide_banner", "-i", path, "-filter:v",
|
| 267 |
-
f"select='gt(scene,{threshold})',showinfo",
|
| 268 |
-
"-an", "-f", "null", "-loglevel", "info", "-"],
|
| 269 |
-
capture_output=True, text=True)
|
| 270 |
-
times = re.findall(r"pts_time:([\d.]+)", proc.stderr or "")
|
| 271 |
-
return sorted(set(float(t) for t in times))
|
| 272 |
|
| 273 |
|
| 274 |
def highlight_cut(info: VideoInfo, top_ratio: float = 0.4,
|
| 275 |
min_len: float = 2.0, pad: float = 0.3) -> list[Segment]:
|
| 276 |
"""Giữ các đoạn CHUYỂN ĐỘNG MẠNH (cao trào) thành khúc riêng."""
|
| 277 |
-
|
| 278 |
-
motion
|
| 279 |
-
|
| 280 |
-
return even_cut(info, max(1, auto_segment_count(info)))
|
| 281 |
|
| 282 |
-
sfps = feats.sample_fps
|
| 283 |
thr = np.quantile(motion, 1.0 - float(np.clip(top_ratio, 0.05, 0.95)))
|
| 284 |
hot = motion >= thr
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
i = 0
|
| 289 |
while i < len(hot):
|
| 290 |
if hot[i]:
|
| 291 |
j = i
|
| 292 |
while j < len(hot) and hot[j]:
|
| 293 |
j += 1
|
| 294 |
-
s = max(0
|
| 295 |
-
e = min(
|
| 296 |
-
if e - s >=
|
| 297 |
spans.append((s, e))
|
| 298 |
i = j
|
| 299 |
else:
|
|
@@ -302,6 +162,7 @@ def highlight_cut(info: VideoInfo, top_ratio: float = 0.4,
|
|
| 302 |
if not spans:
|
| 303 |
return even_cut(info, max(1, auto_segment_count(info)))
|
| 304 |
|
|
|
|
| 305 |
spans.sort()
|
| 306 |
merged = [list(spans[0])]
|
| 307 |
for s, e in spans[1:]:
|
|
@@ -310,96 +171,70 @@ def highlight_cut(info: VideoInfo, top_ratio: float = 0.4,
|
|
| 310 |
else:
|
| 311 |
merged.append([s, e])
|
| 312 |
|
| 313 |
-
|
| 314 |
-
cuts: list[float] = []
|
| 315 |
-
for s, e in merged:
|
| 316 |
-
cuts.extend([s, e])
|
| 317 |
-
segs = _segments_from_time_cuts(cuts, info)
|
| 318 |
-
return _merge_short(segs, info.fps, min(min_len, 1.0))
|
| 319 |
|
| 320 |
|
| 321 |
-
|
|
|
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
Tìm các "đỉnh" nội dung/chuyển động rồi cắt tại ĐÁY năng lượng giữa 2 đỉnh —
|
| 328 |
-
nên điểm cắt rơi vào chỗ lặng/chuyển tiếp tự nhiên. Không cần torch vẫn chạy
|
| 329 |
-
(lùi về histogram màu). Nếu video quá ngắn -> chia đều.
|
| 330 |
"""
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
eff_min = min_len
|
| 338 |
-
if dur < min_len * 2.2:
|
| 339 |
-
eff_min = max(1.0, round(dur / 3.0, 1))
|
| 340 |
-
|
| 341 |
-
content = feats.clip_emb if feats.clip_emb is not None else feats.color_hist
|
| 342 |
-
# độ đổi nội dung: 1 - cossim với frame ~1.5s trước (giống videoCook)
|
| 343 |
-
k = max(1, int(1.5 * sfps))
|
| 344 |
-
nov = _norm01([1.0 - _cossim(content[i], content[max(0, i - k)])
|
| 345 |
-
for i in range(len(content))])
|
| 346 |
-
|
| 347 |
-
# NGỮ NGHĨA quan trọng hơn chuyển động: nội dung 1.0, motion 0.4 (videoCook)
|
| 348 |
-
energy = _norm01(0.4 * feats.motion + 1.0 * np.asarray(nov))
|
| 349 |
-
sm = max(1, int(sfps * 0.5))
|
| 350 |
-
energy = np.convolve(energy, np.ones(sm) / sm, mode="same")
|
| 351 |
-
|
| 352 |
-
thr = float(energy.mean() + float(sensitivity) * energy.std())
|
| 353 |
-
peaks: list[tuple[int, float, float]] = []
|
| 354 |
-
last_t = -1e9
|
| 355 |
-
for i in range(1, len(energy) - 1):
|
| 356 |
-
if (energy[i] >= thr and energy[i] >= energy[i - 1]
|
| 357 |
-
and energy[i] >= energy[i + 1]
|
| 358 |
-
and feats.times[i] - last_t >= eff_min):
|
| 359 |
-
peaks.append((i, float(feats.times[i]), float(energy[i])))
|
| 360 |
-
last_t = feats.times[i]
|
| 361 |
-
|
| 362 |
-
if not peaks:
|
| 363 |
-
return even_cut(info, n or max(1, auto_segment_count(info)))
|
| 364 |
-
|
| 365 |
-
if n:
|
| 366 |
-
# chọn n đỉnh mạnh nhất rồi sắp theo thời gian -> ~n khúc
|
| 367 |
-
peaks = sorted(sorted(peaks, key=lambda p: p[2], reverse=True)[:max(1, n - 1)],
|
| 368 |
-
key=lambda p: p[1])
|
| 369 |
-
|
| 370 |
-
cuts: list[float] = []
|
| 371 |
-
for a, b in zip(peaks, peaks[1:]):
|
| 372 |
-
v = a[0] + int(np.argmin(energy[a[0]:b[0] + 1]))
|
| 373 |
-
cuts.append(float(feats.times[v]))
|
| 374 |
-
# nếu chỉ có 1 đỉnh, cắt tại đáy năng lượng quanh đỉnh đó
|
| 375 |
-
if not cuts and peaks:
|
| 376 |
-
pi = peaks[0][0]
|
| 377 |
-
if pi > 1:
|
| 378 |
-
v = int(np.argmin(energy[:pi + 1]))
|
| 379 |
-
cuts.append(float(feats.times[v]))
|
| 380 |
-
|
| 381 |
-
segs = _segments_from_time_cuts(cuts, info)
|
| 382 |
-
segs = _merge_short(segs, info.fps, eff_min)
|
| 383 |
-
if n and len(segs) != n:
|
| 384 |
-
segs = _force_n(segs, n, info.fps)
|
| 385 |
-
return segs
|
| 386 |
|
|
|
|
| 387 |
|
| 388 |
-
# ----------------------------------------------------------------- helpers ---
|
| 389 |
|
| 390 |
-
def
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
if hi - lo < 1e-9:
|
| 396 |
-
return np.zeros_like(a)
|
| 397 |
-
return np.clip((a - lo) / (hi - lo), 0, 1)
|
| 398 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
|
| 400 |
-
def _cossim(a, b) -> float:
|
| 401 |
-
return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-9))
|
| 402 |
|
|
|
|
| 403 |
|
| 404 |
def _merge_short(segs: list[Segment], fps: float, min_len: float) -> list[Segment]:
|
| 405 |
if not segs:
|
|
@@ -407,34 +242,16 @@ def _merge_short(segs: list[Segment], fps: float, min_len: float) -> list[Segmen
|
|
| 407 |
min_f = int(min_len * fps)
|
| 408 |
merged: list[Segment] = []
|
| 409 |
for s in segs:
|
| 410 |
-
if merged and (s.end_frame -
|
|
|
|
| 411 |
merged[-1] = Segment(merged[-1].index, merged[-1].start_frame,
|
| 412 |
s.end_frame, fps)
|
| 413 |
else:
|
| 414 |
merged.append(Segment(len(merged), s.start_frame, s.end_frame, fps))
|
|
|
|
| 415 |
return [Segment(i, m.start_frame, m.end_frame, fps) for i, m in enumerate(merged)]
|
| 416 |
|
| 417 |
|
| 418 |
-
def _force_n(segs: list[Segment], n: int, fps: float) -> list[Segment]:
|
| 419 |
-
"""Ép về đúng n khúc: gộp khúc ngắn nhất hoặc chẻ đôi khúc dài nhất."""
|
| 420 |
-
items = [[s.start_frame, s.end_frame] for s in segs]
|
| 421 |
-
n = max(1, n)
|
| 422 |
-
while len(items) > n:
|
| 423 |
-
i = min(range(len(items)), key=lambda k: items[k][1] - items[k][0])
|
| 424 |
-
j = i - 1 if i > 0 else i + 1
|
| 425 |
-
lo, hi = min(items[i][0], items[j][0]), max(items[i][1], items[j][1])
|
| 426 |
-
items[min(i, j)] = [lo, hi]
|
| 427 |
-
items.pop(max(i, j))
|
| 428 |
-
while len(items) < n and items:
|
| 429 |
-
i = max(range(len(items)), key=lambda k: items[k][1] - items[k][0])
|
| 430 |
-
s, e = items[i]
|
| 431 |
-
mid = (s + e) // 2
|
| 432 |
-
if mid <= s or mid >= e:
|
| 433 |
-
break
|
| 434 |
-
items[i:i + 1] = [[s, mid], [mid, e]]
|
| 435 |
-
return [Segment(i, s, e, fps) for i, (s, e) in enumerate(items)]
|
| 436 |
-
|
| 437 |
-
|
| 438 |
def build_segments(info: VideoInfo, mode: str, **kw) -> list[Segment]:
|
| 439 |
if mode == "even":
|
| 440 |
n = kw.get("n") or auto_segment_count(info)
|
|
@@ -444,6 +261,5 @@ def build_segments(info: VideoInfo, mode: str, **kw) -> list[Segment]:
|
|
| 444 |
if mode == "highlight":
|
| 445 |
return highlight_cut(info, kw.get("top_ratio", 0.4), kw.get("min_len", 2.0))
|
| 446 |
if mode == "smart":
|
| 447 |
-
return smart_cut(info, kw.get("n")
|
| 448 |
-
kw.get("min_len", 1.5))
|
| 449 |
raise ValueError(f"unknown mode: {mode}")
|
|
|
|
| 1 |
"""4 chế độ cắt -> trả về danh sách KHÚC (start_frame, end_frame) frame-accurate.
|
| 2 |
|
| 3 |
- even : chia N khúc bằng nhau.
|
| 4 |
+
- scene : nhận diện CHUYỂN CẢNH (opencv hist-diff), khúc quá ngắn gộp lại.
|
| 5 |
- highlight : tìm đoạn CHUYỂN ĐỘNG MẠNH (motion energy), tách thành khúc.
|
| 6 |
+
- smart : dùng model CLIP cắt theo ngữ nghĩa (cần torch + open_clip; có fallback).
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
Boundary = (start_frame, end_frame) — end_frame exclusive theo frame index.
|
| 9 |
"""
|
| 10 |
|
| 11 |
from __future__ import annotations
|
| 12 |
|
| 13 |
+
from dataclasses import dataclass
|
|
|
|
| 14 |
|
| 15 |
import numpy as np
|
| 16 |
|
| 17 |
+
from .probe import VideoInfo
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
@dataclass
|
|
|
|
| 56 |
return segs
|
| 57 |
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# ----------------------------------------------------------------- even ------
|
| 60 |
|
| 61 |
def even_cut(info: VideoInfo, n: int) -> list[Segment]:
|
|
|
|
| 77 |
return max(1, round(info.duration / max(0.5, target_len)))
|
| 78 |
|
| 79 |
|
| 80 |
+
# -------------------------------------------------------- scene / highlight --
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
def _frame_signals(path: str, max_dim: int = 160):
|
| 83 |
+
"""Quét video 1 lần, trả (hist_diff[], motion[]) theo từng frame (chuẩn hóa)."""
|
| 84 |
+
import cv2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
cap = cv2.VideoCapture(path)
|
| 87 |
+
prev_gray = None
|
| 88 |
+
prev_hist = None
|
| 89 |
+
hist_diff: list[float] = [0.0]
|
| 90 |
+
motion: list[float] = [0.0]
|
| 91 |
|
| 92 |
+
while True:
|
| 93 |
+
ok, frame = cap.read()
|
| 94 |
+
if not ok:
|
| 95 |
+
break
|
| 96 |
+
h, w = frame.shape[:2]
|
| 97 |
+
scale = max_dim / max(h, w)
|
| 98 |
+
if scale < 1:
|
| 99 |
+
frame = cv2.resize(frame, (int(w * scale), int(h * scale)))
|
| 100 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 101 |
+
hist = cv2.calcHist([frame], [0, 1, 2], None, [8, 8, 8],
|
| 102 |
+
[0, 256, 0, 256, 0, 256])
|
| 103 |
+
cv2.normalize(hist, hist)
|
| 104 |
+
|
| 105 |
+
if prev_hist is not None:
|
| 106 |
+
d = cv2.compareHist(prev_hist, hist, cv2.HISTCMP_BHATTACHARYYA)
|
| 107 |
+
hist_diff.append(float(d))
|
| 108 |
+
m = float(np.mean(cv2.absdiff(gray, prev_gray)))
|
| 109 |
+
motion.append(m)
|
| 110 |
+
prev_gray, prev_hist = gray, hist
|
| 111 |
+
|
| 112 |
+
cap.release()
|
| 113 |
+
return np.array(hist_diff), np.array(motion)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
|
|
|
| 115 |
|
| 116 |
def scene_cut(info: VideoInfo, min_len: float = 1.5, sensitivity: float = 0.5) -> list[Segment]:
|
| 117 |
+
"""Cắt tại các điểm chuyển cảnh; khúc < min_len gộp với khúc liền kề."""
|
| 118 |
+
hist_diff, _ = _frame_signals(info.path)
|
| 119 |
+
if len(hist_diff) < 2:
|
| 120 |
+
return even_cut(info, 1)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
+
# ngưỡng = mean + k*std, k tỉ lệ nghịch với sensitivity
|
| 123 |
+
k = 3.0 - 2.0 * float(np.clip(sensitivity, 0, 1)) # sensitivity 1 -> k=1, 0 -> k=3
|
| 124 |
+
thr = hist_diff.mean() + k * hist_diff.std()
|
| 125 |
+
raw_cuts = [int(i) for i in np.where(hist_diff > thr)[0] if i > 0]
|
| 126 |
|
| 127 |
+
segs = _segments_from_cuts(raw_cuts, info.nb_frames, info.fps)
|
| 128 |
+
return _merge_short(segs, info.fps, min_len)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
|
| 131 |
def highlight_cut(info: VideoInfo, top_ratio: float = 0.4,
|
| 132 |
min_len: float = 2.0, pad: float = 0.3) -> list[Segment]:
|
| 133 |
"""Giữ các đoạn CHUYỂN ĐỘNG MẠNH (cao trào) thành khúc riêng."""
|
| 134 |
+
_, motion = _frame_signals(info.path)
|
| 135 |
+
if len(motion) < 2:
|
| 136 |
+
return even_cut(info, 1)
|
|
|
|
| 137 |
|
|
|
|
| 138 |
thr = np.quantile(motion, 1.0 - float(np.clip(top_ratio, 0.05, 0.95)))
|
| 139 |
hot = motion >= thr
|
| 140 |
|
| 141 |
+
fps = info.fps
|
| 142 |
+
pad_f = int(pad * fps)
|
| 143 |
+
min_f = int(min_len * fps)
|
| 144 |
+
total = info.nb_frames
|
| 145 |
+
|
| 146 |
+
# gom các frame "nóng" liền nhau thành đoạn, nới biên pad, lọc đoạn ngắn
|
| 147 |
+
spans: list[tuple[int, int]] = []
|
| 148 |
i = 0
|
| 149 |
while i < len(hot):
|
| 150 |
if hot[i]:
|
| 151 |
j = i
|
| 152 |
while j < len(hot) and hot[j]:
|
| 153 |
j += 1
|
| 154 |
+
s = max(0, i - pad_f)
|
| 155 |
+
e = min(total, j + pad_f)
|
| 156 |
+
if e - s >= min_f:
|
| 157 |
spans.append((s, e))
|
| 158 |
i = j
|
| 159 |
else:
|
|
|
|
| 162 |
if not spans:
|
| 163 |
return even_cut(info, max(1, auto_segment_count(info)))
|
| 164 |
|
| 165 |
+
# merge spans chồng lấn
|
| 166 |
spans.sort()
|
| 167 |
merged = [list(spans[0])]
|
| 168 |
for s, e in spans[1:]:
|
|
|
|
| 171 |
else:
|
| 172 |
merged.append([s, e])
|
| 173 |
|
| 174 |
+
return [Segment(i, s, e, fps) for i, (s, e) in enumerate(merged)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
|
| 177 |
+
def smart_cut(info: VideoInfo, n: int | None = None) -> list[Segment]:
|
| 178 |
+
"""Cắt theo ngữ nghĩa bằng CLIP. Cần `torch` + `open_clip_torch`.
|
| 179 |
|
| 180 |
+
Lấy embedding CLIP cho frame mẫu, phát hiện điểm ngữ nghĩa đổi mạnh
|
| 181 |
+
(cosine distance giữa frame liền kề vượt ngưỡng) -> điểm cắt.
|
| 182 |
+
Nếu thiếu thư viện/model -> fallback sang scene_cut.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
"""
|
| 184 |
+
try:
|
| 185 |
+
import torch # noqa: F401
|
| 186 |
+
import open_clip # noqa: F401
|
| 187 |
+
except Exception:
|
| 188 |
+
# Fallback an toàn để luồng không vỡ khi chưa cài model.
|
| 189 |
+
return scene_cut(info)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
+
return _smart_cut_clip(info, n)
|
| 192 |
|
|
|
|
| 193 |
|
| 194 |
+
def _smart_cut_clip(info: VideoInfo, n: int | None) -> list[Segment]: # pragma: no cover
|
| 195 |
+
import cv2
|
| 196 |
+
import numpy as np
|
| 197 |
+
import open_clip
|
| 198 |
+
import torch
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 201 |
+
model, _, preprocess = open_clip.create_model_and_transforms(
|
| 202 |
+
"ViT-B-32", pretrained="laion2b_s34b_b79k"
|
| 203 |
+
)
|
| 204 |
+
model = model.to(device).eval()
|
| 205 |
+
|
| 206 |
+
cap = cv2.VideoCapture(info.path)
|
| 207 |
+
fps = info.fps
|
| 208 |
+
step = max(1, int(fps)) # 1 frame/giây
|
| 209 |
+
feats, frame_idx = [], []
|
| 210 |
+
idx = 0
|
| 211 |
+
from PIL import Image
|
| 212 |
+
while True:
|
| 213 |
+
ok, frame = cap.read()
|
| 214 |
+
if not ok:
|
| 215 |
+
break
|
| 216 |
+
if idx % step == 0:
|
| 217 |
+
img = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 218 |
+
with torch.no_grad():
|
| 219 |
+
t = preprocess(img).unsqueeze(0).to(device)
|
| 220 |
+
f = model.encode_image(t)
|
| 221 |
+
f = f / f.norm(dim=-1, keepdim=True)
|
| 222 |
+
feats.append(f.cpu().numpy()[0])
|
| 223 |
+
frame_idx.append(idx)
|
| 224 |
+
idx += 1
|
| 225 |
+
cap.release()
|
| 226 |
+
|
| 227 |
+
if len(feats) < 2:
|
| 228 |
+
return scene_cut(info)
|
| 229 |
+
feats = np.array(feats)
|
| 230 |
+
dist = 1.0 - np.sum(feats[1:] * feats[:-1], axis=1)
|
| 231 |
+
thr = dist.mean() + dist.std()
|
| 232 |
+
cuts = [frame_idx[i + 1] for i in np.where(dist > thr)[0]]
|
| 233 |
+
segs = _segments_from_cuts(cuts, info.nb_frames, info.fps)
|
| 234 |
+
return _merge_short(segs, info.fps, 1.5)
|
| 235 |
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
# ----------------------------------------------------------------- helpers ---
|
| 238 |
|
| 239 |
def _merge_short(segs: list[Segment], fps: float, min_len: float) -> list[Segment]:
|
| 240 |
if not segs:
|
|
|
|
| 242 |
min_f = int(min_len * fps)
|
| 243 |
merged: list[Segment] = []
|
| 244 |
for s in segs:
|
| 245 |
+
if merged and (s.end_frame - merged[-1].start_frame) and \
|
| 246 |
+
(s.end_frame - s.start_frame) < min_f:
|
| 247 |
merged[-1] = Segment(merged[-1].index, merged[-1].start_frame,
|
| 248 |
s.end_frame, fps)
|
| 249 |
else:
|
| 250 |
merged.append(Segment(len(merged), s.start_frame, s.end_frame, fps))
|
| 251 |
+
# đánh số lại
|
| 252 |
return [Segment(i, m.start_frame, m.end_frame, fps) for i, m in enumerate(merged)]
|
| 253 |
|
| 254 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
def build_segments(info: VideoInfo, mode: str, **kw) -> list[Segment]:
|
| 256 |
if mode == "even":
|
| 257 |
n = kw.get("n") or auto_segment_count(info)
|
|
|
|
| 261 |
if mode == "highlight":
|
| 262 |
return highlight_cut(info, kw.get("top_ratio", 0.4), kw.get("min_len", 2.0))
|
| 263 |
if mode == "smart":
|
| 264 |
+
return smart_cut(info, kw.get("n"))
|
|
|
|
| 265 |
raise ValueError(f"unknown mode: {mode}")
|
backend/app/services/downloader/ydl.py
CHANGED
|
@@ -92,97 +92,46 @@ def detect_source(url: str) -> str:
|
|
| 92 |
return "other"
|
| 93 |
|
| 94 |
|
| 95 |
-
|
| 96 |
-
_UA = ("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
|
| 97 |
-
"(KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36")
|
| 98 |
-
|
| 99 |
-
# Referer theo nguồn để vượt kiểm tra chống hotlink.
|
| 100 |
-
_REFERER = {
|
| 101 |
-
"douyin": "https://www.douyin.com/",
|
| 102 |
-
"tiktok": "https://www.tiktok.com/",
|
| 103 |
-
"facebook": "https://www.facebook.com/",
|
| 104 |
-
"instagram": "https://www.instagram.com/",
|
| 105 |
-
"shopee": "https://shopee.vn/",
|
| 106 |
-
"1688": "https://www.1688.com/",
|
| 107 |
-
"taobao": "https://www.taobao.com/",
|
| 108 |
-
}
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
def _build_opts(out_dir: str, generic: bool = False, source: str = "",
|
| 112 |
-
fmt: str = "bv*+ba/b") -> dict:
|
| 113 |
-
headers = {"User-Agent": _UA}
|
| 114 |
-
ref = _REFERER.get(source)
|
| 115 |
-
if ref:
|
| 116 |
-
headers["Referer"] = ref
|
| 117 |
opts = {
|
| 118 |
"outtmpl": os.path.join(out_dir, "%(title).80s [%(id)s].%(ext)s"),
|
| 119 |
-
"format":
|
| 120 |
"merge_output_format": "mp4",
|
| 121 |
"noplaylist": True,
|
| 122 |
"quiet": True,
|
| 123 |
"no_warnings": True,
|
| 124 |
"retries": 3,
|
| 125 |
-
"extractor_retries": 3,
|
| 126 |
"concurrent_fragment_downloads": 4,
|
| 127 |
"writethumbnail": False,
|
| 128 |
-
"http_headers": headers,
|
| 129 |
}
|
| 130 |
if generic:
|
| 131 |
# thử quét video nhúng trong trang (Shopee/1688/Taobao... không có extractor riêng)
|
| 132 |
opts["force_generic_extractor"] = True
|
|
|
|
|
|
|
| 133 |
return opts
|
| 134 |
|
| 135 |
|
| 136 |
-
def _resolve_path(info: dict, ydl) -> str:
|
| 137 |
-
"""Tìm ĐÚNG file đã tải. Ưu tiên filepath thật trong info; lùi về prepare_filename
|
| 138 |
-
rồi thử đuôi .mp4. Phải là FILE (không phải thư mục) mới hợp lệ."""
|
| 139 |
-
# yt-dlp ghi đường dẫn file cuối (đã merge) ở đây cho từng video tải về
|
| 140 |
-
for d in (info.get("requested_downloads") or []):
|
| 141 |
-
p = d.get("filepath") or d.get("_filename")
|
| 142 |
-
if p and os.path.isfile(p):
|
| 143 |
-
return p
|
| 144 |
-
path = ydl.prepare_filename(info)
|
| 145 |
-
base, _ = os.path.splitext(path)
|
| 146 |
-
for cand in (path, base + ".mp4", base + ".mkv", base + ".webm"):
|
| 147 |
-
if os.path.isfile(cand):
|
| 148 |
-
return cand
|
| 149 |
-
return path
|
| 150 |
-
|
| 151 |
-
|
| 152 |
def _attempt(url: str, out_dir: str, opts: dict) -> DownloadResult:
|
| 153 |
import yt_dlp # type: ignore
|
| 154 |
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 155 |
info = ydl.extract_info(url, download=True)
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
if not os.path.isfile(path):
|
| 162 |
-
# KHÔNG ra được file thật -> coi như lỗi để fallback tầng sau xử lý
|
| 163 |
-
raise RuntimeError("yt-dlp không tạo được file video (không có định dạng tải được)")
|
| 164 |
-
base = os.path.splitext(path)[0]
|
| 165 |
return DownloadResult(
|
| 166 |
url=url, ok=True, path=path,
|
| 167 |
title=info.get("title", "") or os.path.basename(base),
|
| 168 |
ext=os.path.splitext(path)[1].lstrip("."),
|
| 169 |
-
size=
|
| 170 |
duration=float(info.get("duration") or 0),
|
| 171 |
thumbnail=info.get("thumbnail", "") or "",
|
| 172 |
)
|
| 173 |
|
| 174 |
|
| 175 |
-
def
|
| 176 |
-
"""Lỗi do KHÔNG có đúng định dạng yêu cầu -> nên thử lại với format đơn giản."""
|
| 177 |
-
e = (err or "").lower()
|
| 178 |
-
return any(k in e for k in (
|
| 179 |
-
"requested format", "format is not available", "no video formats",
|
| 180 |
-
"no formats", "requested merge"))
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
def download_one(url: str, out_dir: str) -> DownloadResult:
|
| 184 |
-
"""Tải 1 link — KHÔNG dùng cookie. Thử nhiều cách để tải được nhiều link nhất:
|
| 185 |
-
format tốt nhất -> format đơn giản (nếu lỗi định dạng) -> generic extractor."""
|
| 186 |
try:
|
| 187 |
import yt_dlp # noqa: F401 # type: ignore
|
| 188 |
except Exception as e: # pragma: no cover
|
|
@@ -192,35 +141,27 @@ def download_one(url: str, out_dir: str) -> DownloadResult:
|
|
| 192 |
os.makedirs(out_dir, exist_ok=True)
|
| 193 |
url = normalize_url(url)
|
| 194 |
source = detect_source(url)
|
| 195 |
-
|
| 196 |
-
# 1) Thử trực tiếp với format tốt nhất (đã tự lùi về "b" trong cùng lần gọi).
|
| 197 |
try:
|
| 198 |
-
return _attempt(url, out_dir, _build_opts(out_dir,
|
| 199 |
except Exception as e: # pragma: no cover - lỗi mạng/extractor
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
# 3) Generic extractor (quét video nhúng trong trang) — last-resort cho MỌI nguồn.
|
| 210 |
-
try:
|
| 211 |
-
return _attempt(url, out_dir, _build_opts(out_dir, generic=True, source=source))
|
| 212 |
-
except Exception as e:
|
| 213 |
-
last = clean_error(f"{source}: {e}") if source != "other" else clean_error(e)
|
| 214 |
-
|
| 215 |
-
return DownloadResult(url=url, ok=False, error=last)
|
| 216 |
|
| 217 |
|
| 218 |
def download_many(urls: list[str], out_dir: str,
|
|
|
|
| 219 |
on_item: Callable[[int, DownloadResult], None] | None = None
|
| 220 |
) -> list[DownloadResult]:
|
| 221 |
results = []
|
| 222 |
for i, url in enumerate(urls):
|
| 223 |
-
res = download_one(url, out_dir)
|
| 224 |
results.append(res)
|
| 225 |
if on_item:
|
| 226 |
on_item(i, res)
|
|
|
|
| 92 |
return "other"
|
| 93 |
|
| 94 |
|
| 95 |
+
def _build_opts(out_dir: str, cookies_file: str | None, generic: bool = False) -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
opts = {
|
| 97 |
"outtmpl": os.path.join(out_dir, "%(title).80s [%(id)s].%(ext)s"),
|
| 98 |
+
"format": "bv*+ba/b",
|
| 99 |
"merge_output_format": "mp4",
|
| 100 |
"noplaylist": True,
|
| 101 |
"quiet": True,
|
| 102 |
"no_warnings": True,
|
| 103 |
"retries": 3,
|
|
|
|
| 104 |
"concurrent_fragment_downloads": 4,
|
| 105 |
"writethumbnail": False,
|
|
|
|
| 106 |
}
|
| 107 |
if generic:
|
| 108 |
# thử quét video nhúng trong trang (Shopee/1688/Taobao... không có extractor riêng)
|
| 109 |
opts["force_generic_extractor"] = True
|
| 110 |
+
if cookies_file and os.path.exists(cookies_file):
|
| 111 |
+
opts["cookiefile"] = cookies_file
|
| 112 |
return opts
|
| 113 |
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def _attempt(url: str, out_dir: str, opts: dict) -> DownloadResult:
|
| 116 |
import yt_dlp # type: ignore
|
| 117 |
with yt_dlp.YoutubeDL(opts) as ydl:
|
| 118 |
info = ydl.extract_info(url, download=True)
|
| 119 |
+
path = ydl.prepare_filename(info)
|
| 120 |
+
base, _ = os.path.splitext(path)
|
| 121 |
+
if not os.path.exists(path) and os.path.exists(base + ".mp4"):
|
| 122 |
+
path = base + ".mp4"
|
| 123 |
+
size = os.path.getsize(path) if os.path.exists(path) else 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
return DownloadResult(
|
| 125 |
url=url, ok=True, path=path,
|
| 126 |
title=info.get("title", "") or os.path.basename(base),
|
| 127 |
ext=os.path.splitext(path)[1].lstrip("."),
|
| 128 |
+
size=size,
|
| 129 |
duration=float(info.get("duration") or 0),
|
| 130 |
thumbnail=info.get("thumbnail", "") or "",
|
| 131 |
)
|
| 132 |
|
| 133 |
|
| 134 |
+
def download_one(url: str, out_dir: str, cookies_file: str | None = None) -> DownloadResult:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
try:
|
| 136 |
import yt_dlp # noqa: F401 # type: ignore
|
| 137 |
except Exception as e: # pragma: no cover
|
|
|
|
| 141 |
os.makedirs(out_dir, exist_ok=True)
|
| 142 |
url = normalize_url(url)
|
| 143 |
source = detect_source(url)
|
|
|
|
|
|
|
| 144 |
try:
|
| 145 |
+
return _attempt(url, out_dir, _build_opts(out_dir, cookies_file))
|
| 146 |
except Exception as e: # pragma: no cover - lỗi mạng/extractor
|
| 147 |
+
first_err = clean_error(e)
|
| 148 |
+
# Shopee/1688/Taobao: thử lại bằng generic extractor (quét video trong trang)
|
| 149 |
+
if source in ("shopee", "1688", "taobao"):
|
| 150 |
+
try:
|
| 151 |
+
return _attempt(url, out_dir, _build_opts(out_dir, cookies_file, generic=True))
|
| 152 |
+
except Exception as e2:
|
| 153 |
+
return DownloadResult(url=url, ok=False,
|
| 154 |
+
error=clean_error(f"{source}: {e2}"))
|
| 155 |
+
return DownloadResult(url=url, ok=False, error=first_err)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
|
| 158 |
def download_many(urls: list[str], out_dir: str,
|
| 159 |
+
cookies_file: str | None = None,
|
| 160 |
on_item: Callable[[int, DownloadResult], None] | None = None
|
| 161 |
) -> list[DownloadResult]:
|
| 162 |
results = []
|
| 163 |
for i, url in enumerate(urls):
|
| 164 |
+
res = download_one(url, out_dir, cookies_file)
|
| 165 |
results.append(res)
|
| 166 |
if on_item:
|
| 167 |
on_item(i, res)
|
backend/app/services/dubber/asr.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
-
"""Bước NHẬN DẠNG (ASR).
|
| 2 |
|
| 3 |
-
-
|
| 4 |
-
-
|
| 5 |
-
faster-whisper chạy LOCAL nên luồng lồng tiếng KHÔNG vỡ vì thiếu API.
|
| 6 |
-
- Lọc đoạn không phải giọng nói; nếu lọc sạch hết thì GIỮ NGUYÊN để không mất nội dung.
|
| 7 |
- Gộp các mảnh vụn thành CÂU trọn vẹn để ngữ điệu mượt & ngân sách thời gian có nghĩa.
|
| 8 |
"""
|
| 9 |
|
|
@@ -14,8 +12,6 @@ from dataclasses import dataclass
|
|
| 14 |
|
| 15 |
from ...config import settings
|
| 16 |
|
| 17 |
-
_LOCAL_WHISPER = None # cache model faster-whisper (nạp 1 lần)
|
| 18 |
-
|
| 19 |
|
| 20 |
@dataclass
|
| 21 |
class Segment:
|
|
@@ -29,16 +25,7 @@ class Segment:
|
|
| 29 |
|
| 30 |
|
| 31 |
def transcribe(asr_wav: str, model: str | None = None) -> tuple[list[Segment], str]:
|
| 32 |
-
"""Trả (segments, language).
|
| 33 |
-
if settings.GROQ_API_KEY:
|
| 34 |
-
try:
|
| 35 |
-
return _transcribe_groq(asr_wav, model)
|
| 36 |
-
except Exception:
|
| 37 |
-
pass # mất mạng / lỗi Groq -> dùng local
|
| 38 |
-
return _transcribe_local(asr_wav)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
def _transcribe_groq(asr_wav: str, model: str | None = None) -> tuple[list[Segment], str]:
|
| 42 |
from groq import Groq # type: ignore
|
| 43 |
|
| 44 |
client = Groq(api_key=settings.GROQ_API_KEY)
|
|
@@ -50,29 +37,14 @@ def _transcribe_groq(asr_wav: str, model: str | None = None) -> tuple[list[Segme
|
|
| 50 |
temperature=0.0,
|
| 51 |
)
|
| 52 |
data = resp.model_dump() if hasattr(resp, "model_dump") else resp
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def _transcribe_local(asr_wav: str) -> tuple[list[Segment], str]:
|
| 57 |
-
"""ASR LOCAL bằng faster-whisper (CPU, int8 cho máy ít RAM)."""
|
| 58 |
-
global _LOCAL_WHISPER
|
| 59 |
-
from faster_whisper import WhisperModel # type: ignore
|
| 60 |
-
if _LOCAL_WHISPER is None:
|
| 61 |
-
_LOCAL_WHISPER = WhisperModel(
|
| 62 |
-
settings.LOCAL_WHISPER_MODEL, device="cpu", compute_type="int8")
|
| 63 |
-
segs, info = _LOCAL_WHISPER.transcribe(asr_wav, vad_filter=True)
|
| 64 |
-
raw = [{"start": s.start, "end": s.end, "text": s.text,
|
| 65 |
-
"no_speech_prob": getattr(s, "no_speech_prob", 0.0) or 0.0,
|
| 66 |
-
"avg_logprob": getattr(s, "avg_logprob", 0.0) or 0.0} for s in segs]
|
| 67 |
-
return _finalize(raw, getattr(info, "language", "") or "")
|
| 68 |
-
|
| 69 |
|
| 70 |
-
def _finalize(raw: list[dict], language: str) -> tuple[list[Segment], str]:
|
| 71 |
kept = _filter_speech(raw)
|
| 72 |
if not kept: # đừng để mất nội dung
|
| 73 |
-
kept = [Segment(
|
| 74 |
-
|
| 75 |
-
return
|
| 76 |
|
| 77 |
|
| 78 |
def _filter_speech(raw: list[dict],
|
|
|
|
| 1 |
+
"""Bước NHẬN DẠNG (ASR) — Whisper large-v3 qua API Groq.
|
| 2 |
|
| 3 |
+
- response_format=verbose_json, lấy segment kèm timestamp + no_speech_prob + avg_logprob.
|
| 4 |
+
- Lọc đoạn không phải giọng nói thật; nếu lọc sạch hết thì GIỮ NGUYÊN để không mất nội dung.
|
|
|
|
|
|
|
| 5 |
- Gộp các mảnh vụn thành CÂU trọn vẹn để ngữ điệu mượt & ngân sách thời gian có nghĩa.
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 12 |
|
| 13 |
from ...config import settings
|
| 14 |
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@dataclass
|
| 17 |
class Segment:
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
def transcribe(asr_wav: str, model: str | None = None) -> tuple[list[Segment], str]:
|
| 28 |
+
"""Trả (segments, language). Cần GROQ_API_KEY và `pip install groq`."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
from groq import Groq # type: ignore
|
| 30 |
|
| 31 |
client = Groq(api_key=settings.GROQ_API_KEY)
|
|
|
|
| 37 |
temperature=0.0,
|
| 38 |
)
|
| 39 |
data = resp.model_dump() if hasattr(resp, "model_dump") else resp
|
| 40 |
+
language = data.get("language", "")
|
| 41 |
+
raw = data.get("segments", []) or []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
| 43 |
kept = _filter_speech(raw)
|
| 44 |
if not kept: # đừng để mất nội dung
|
| 45 |
+
kept = [Segment(s["start"], s["end"], s["text"].strip()) for s in raw]
|
| 46 |
+
sentences = merge_into_sentences(kept)
|
| 47 |
+
return sentences, language
|
| 48 |
|
| 49 |
|
| 50 |
def _filter_speech(raw: list[dict],
|
backend/app/services/dubber/mux.py
CHANGED
|
@@ -117,16 +117,11 @@ def mix_over_bg(voice_wav: str, bg_wav: str, out_wav: str) -> str:
|
|
| 117 |
|
| 118 |
|
| 119 |
def mux_to_video(video: str, audio_wav: str, out_mp4: str) -> str:
|
| 120 |
-
"""Ghép audio
|
| 121 |
-
|
| 122 |
-
`-map 1:a:0` = CHỈ lấy track audio mới (bỏ hẳn tiếng gốc). `+faststart` đẩy
|
| 123 |
-
moov atom lên đầu để trình duyệt phát/hiện hình ngay (không thì <video> đen).
|
| 124 |
-
"""
|
| 125 |
subprocess.run(
|
| 126 |
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 127 |
"-i", video, "-i", audio_wav,
|
| 128 |
"-map", "0:v:0", "-map", "1:a:0",
|
| 129 |
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
|
| 130 |
-
"-
|
| 131 |
-
check=True, capture_output=True)
|
| 132 |
return out_mp4
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
def mux_to_video(video: str, audio_wav: str, out_mp4: str) -> str:
|
| 120 |
+
"""Ghép audio mới vào luồng hình gốc (giữ nguyên video)."""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
subprocess.run(
|
| 122 |
[FFMPEG, "-y", "-hide_banner", "-loglevel", "error",
|
| 123 |
"-i", video, "-i", audio_wav,
|
| 124 |
"-map", "0:v:0", "-map", "1:a:0",
|
| 125 |
"-c:v", "copy", "-c:a", "aac", "-b:a", "192k",
|
| 126 |
+
"-shortest", out_mp4], check=True, capture_output=True)
|
|
|
|
| 127 |
return out_mp4
|