Spaces:
Running
Running
up
Browse files
backend/app/services/dubber/pipeline.py
CHANGED
|
@@ -172,6 +172,42 @@ def _run_dub_full(video: str, out_dir: str, opt: DubOptions,
|
|
| 172 |
except Exception as e:
|
| 173 |
raise RuntimeError(f"[Bước ASR/Groq Whisper] {e}") from e
|
| 174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
emit(0.45, f"Dịch sang {opt.target_lang} (vừa khít thời lượng)")
|
| 176 |
try:
|
| 177 |
translated = translate.translate_all(segments, opt.target_lang)
|
|
|
|
| 172 |
except Exception as e:
|
| 173 |
raise RuntimeError(f"[Bước ASR/Groq Whisper] {e}") from e
|
| 174 |
|
| 175 |
+
# ── Nếu ngôn ngữ gốc TRÙNG ngôn ngữ đích → giữ nguyên video gốc ──
|
| 176 |
+
_lang_map = {"vi": "vi", "vie": "vi", "english": "en", "en": "en",
|
| 177 |
+
"filipino": "fil", "fil": "fil", "tagalog": "fil"}
|
| 178 |
+
_detected = _lang_map.get(language.lower().strip(), language.lower().strip())
|
| 179 |
+
_target = _lang_map.get(opt.target_lang.lower().strip(), opt.target_lang.lower().strip())
|
| 180 |
+
if _detected and _detected == _target:
|
| 181 |
+
if progress:
|
| 182 |
+
progress(1.0, f"Video đã là ngôn ngữ {opt.target_lang} — giữ nguyên")
|
| 183 |
+
import shutil as _sh2
|
| 184 |
+
video_out = os.path.join(out_dir, f"video_translated_{opt.target_lang}.mp4")
|
| 185 |
+
try:
|
| 186 |
+
import subprocess
|
| 187 |
+
subprocess.run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error",
|
| 188 |
+
"-i", video, "-c:v", "libx264", "-preset", "veryfast",
|
| 189 |
+
"-crf", "20", "-pix_fmt", "yuv420p", "-movflags", "+faststart",
|
| 190 |
+
"-c:a", "aac", video_out],
|
| 191 |
+
check=True, capture_output=True)
|
| 192 |
+
except Exception:
|
| 193 |
+
_sh2.copy(video, video_out)
|
| 194 |
+
audio_out = os.path.join(out_dir, "audio.m4a")
|
| 195 |
+
try:
|
| 196 |
+
import subprocess
|
| 197 |
+
subprocess.run(["ffmpeg", "-y", "-hide_banner", "-loglevel", "error",
|
| 198 |
+
"-i", video, "-vn", "-c:a", "aac", audio_out],
|
| 199 |
+
check=True, capture_output=True)
|
| 200 |
+
except Exception:
|
| 201 |
+
audio_out = video_out
|
| 202 |
+
srt_out = os.path.join(out_dir, "subtitle.srt")
|
| 203 |
+
with open(srt_out, "w", encoding="utf-8") as f:
|
| 204 |
+
for i, ln in enumerate(segments, 1):
|
| 205 |
+
st = _srt_time(ln.start); et = _srt_time(ln.end)
|
| 206 |
+
f.write(f"{i}\n{st} --> {et}\n{ln.text}\n\n")
|
| 207 |
+
return DubResult(video_out=video_out, audio_out=audio_out, srt_out=srt_out,
|
| 208 |
+
language=language, n_lines=len(segments),
|
| 209 |
+
note=f"Video đã là tiếng {_detected.upper()} — giữ nguyên, không dịch lại.")
|
| 210 |
+
|
| 211 |
emit(0.45, f"Dịch sang {opt.target_lang} (vừa khít thời lượng)")
|
| 212 |
try:
|
| 213 |
translated = translate.translate_all(segments, opt.target_lang)
|
frontend/src/pages/DubVideo.jsx
CHANGED
|
@@ -335,38 +335,53 @@ export default function DubVideo() {
|
|
| 335 |
{/* kết quả */}
|
| 336 |
{st.outputs.length > 0 && (
|
| 337 |
<div className="card">
|
| 338 |
-
<div className="panel-title">
|
| 339 |
<div className="stack">
|
| 340 |
-
{st.outputs.map((o) =>
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
<
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
<div style={{ flex: 1, minWidth: 220 }}>
|
| 350 |
-
<div className="small muted" style={{ marginBottom: 6 }}>
|
| 351 |
-
<video src={
|
| 352 |
</div>
|
| 353 |
-
|
| 354 |
-
<div style={{
|
| 355 |
-
<
|
| 356 |
-
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
</div>
|
| 358 |
</div>
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
<a className="btn sm" href={fileUrl(o.audio_url)} download><IcDownload size={14} /> Audio</a>
|
| 362 |
-
<a className="btn sm" href={fileUrl(o.srt_url)} download><IcDoc size={14} /> Subtitle SRT</a>
|
| 363 |
-
<button className="btn sm primary" onClick={() => {
|
| 364 |
-
const rel = (u) => (u || "").replace(`/api/files/${st.session}/`, "");
|
| 365 |
-
setSubEd({ videoUrl: fileUrl(o.video_url), videoRel: rel(o.video_url), srtRel: rel(o.srt_url) });
|
| 366 |
-
}}><IcText size={14} /> Chèn phụ đề</button>
|
| 367 |
-
</div>
|
| 368 |
-
</div>
|
| 369 |
-
))}
|
| 370 |
</div>
|
| 371 |
</div>
|
| 372 |
)}
|
|
|
|
| 335 |
{/* kết quả */}
|
| 336 |
{st.outputs.length > 0 && (
|
| 337 |
<div className="card">
|
| 338 |
+
<div className="panel-title">Kết quả</div>
|
| 339 |
<div className="stack">
|
| 340 |
+
{st.outputs.map((o) => {
|
| 341 |
+
const isSameLang = o.note && o.note.includes("giữ nguyên");
|
| 342 |
+
return (
|
| 343 |
+
<div key={o.name} style={{ border: "1px solid " + (isSameLang ? "var(--green)" : "var(--line)"), borderRadius: 12, padding: 14, background: isSameLang ? "var(--green-soft)" : "transparent" }}>
|
| 344 |
+
<div className="flex between center" style={{ marginBottom: 10 }}>
|
| 345 |
+
<b className="small">{o.name}</b>
|
| 346 |
+
<span className={"badge " + (isSameLang ? "done" : "done")}><span className="dot" /> {o.n_lines} câu · {o.language}</span>
|
| 347 |
+
</div>
|
| 348 |
+
{isSameLang ? (
|
| 349 |
+
<div style={{ padding: "10px 14px", background: "var(--green-soft)", borderRadius: "var(--radius-sm)", marginBottom: 10, display: "flex", alignItems: "center", gap: 8 }}>
|
| 350 |
+
<span style={{ fontSize: 18 }}>✅</span>
|
| 351 |
+
<div>
|
| 352 |
+
<div style={{ fontSize: 13, fontWeight: 600, color: "var(--green)" }}>Video đã là ngôn ngữ đích — giữ nguyên</div>
|
| 353 |
+
<div className="small muted" style={{ marginTop: 2 }}>{o.note}</div>
|
| 354 |
+
</div>
|
| 355 |
+
</div>
|
| 356 |
+
) : o.note ? (
|
| 357 |
+
<div className="small" style={{ color: "var(--orange-700)", marginBottom: 8 }}>⚠ {o.note}</div>
|
| 358 |
+
) : null}
|
| 359 |
+
<div className="row wrap">
|
| 360 |
+
{beforeUrl(o.name) && (
|
| 361 |
+
<div style={{ flex: 1, minWidth: 220 }}>
|
| 362 |
+
<div className="small muted" style={{ marginBottom: 6 }}>Trước (gốc)</div>
|
| 363 |
+
<video src={beforeUrl(o.name)} controls style={vidStyle} />
|
| 364 |
+
</div>
|
| 365 |
+
)}
|
| 366 |
<div style={{ flex: 1, minWidth: 220 }}>
|
| 367 |
+
<div className="small muted" style={{ marginBottom: 6 }}>{isSameLang ? "Video gốc (giữ nguyên)" : "Sau (đã dịch)"}</div>
|
| 368 |
+
<video src={fileUrl(o.video_url)} controls style={vidStyle} />
|
| 369 |
</div>
|
| 370 |
+
</div>
|
| 371 |
+
<div className="flex gap8 mt12" style={{ flexWrap: "wrap" }}>
|
| 372 |
+
<a className="btn sm" href={fileUrl(o.video_url)} download><IcDownload size={14} /> Video MP4</a>
|
| 373 |
+
<a className="btn sm" href={fileUrl(o.audio_url)} download><IcDownload size={14} /> Audio</a>
|
| 374 |
+
<a className="btn sm" href={fileUrl(o.srt_url)} download><IcDoc size={14} /> Subtitle SRT</a>
|
| 375 |
+
{!isSameLang && (
|
| 376 |
+
<button className="btn sm primary" onClick={() => {
|
| 377 |
+
const rel = (u) => (u || "").replace(`/api/files/${st.session}/`, "");
|
| 378 |
+
setSubEd({ videoUrl: fileUrl(o.video_url), videoRel: rel(o.video_url), srtRel: rel(o.srt_url) });
|
| 379 |
+
}}><IcText size={14} /> Chèn phụ đề</button>
|
| 380 |
+
)}
|
| 381 |
</div>
|
| 382 |
</div>
|
| 383 |
+
);
|
| 384 |
+
})}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 385 |
</div>
|
| 386 |
</div>
|
| 387 |
)}
|