File size: 11,233 Bytes
56ff7e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import json
import os
import shutil
import subprocess
import threading
import uuid
from datetime import datetime, timedelta
from pathlib import Path
from typing import List, Optional

from fastapi import FastAPI, File, HTTPException, Request, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from faster_whisper import WhisperModel
from pydantic import BaseModel, Field


APP_DIR = Path(__file__).resolve().parent
WORK_DIR = APP_DIR / "workspace"
TEMPLATES_DIR = APP_DIR / "templates"
STATIC_DIR = APP_DIR / "static"
WORK_DIR.mkdir(parents=True, exist_ok=True)


app = FastAPI(title="Viet AutoSub Editor")
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
templates = Jinja2Templates(directory=str(TEMPLATES_DIR))


MODEL_LOCK = threading.Lock()
MODEL_CACHE = {}
DEFAULT_MODEL_SIZE = os.getenv("WHISPER_MODEL_SIZE", "small")
MAX_UPLOAD_MB = int(os.getenv("MAX_UPLOAD_MB", "250"))
KEEP_HOURS = int(os.getenv("KEEP_HOURS", "24"))


class SegmentIn(BaseModel):
    id: int
    start: str
    end: str
    text: str = Field(default="")


class ExportRequest(BaseModel):
    job_id: str
    segments: List[SegmentIn]
    burn_in: bool = True


class SegmentOut(BaseModel):
    id: int
    start: float
    end: float
    text: str



def cleanup_old_jobs() -> None:
    cutoff = datetime.utcnow() - timedelta(hours=KEEP_HOURS)
    for folder in WORK_DIR.iterdir():
        if not folder.is_dir():
            continue
        try:
            modified = datetime.utcfromtimestamp(folder.stat().st_mtime)
            if modified < cutoff:
                shutil.rmtree(folder, ignore_errors=True)
        except Exception:
            continue



def get_model(model_size: str = DEFAULT_MODEL_SIZE) -> WhisperModel:
    with MODEL_LOCK:
        if model_size not in MODEL_CACHE:
            MODEL_CACHE[model_size] = WhisperModel(
                model_size,
                device="cpu",
                compute_type="int8",
            )
        return MODEL_CACHE[model_size]



def ffmpeg_exists() -> bool:
    return shutil.which("ffmpeg") is not None and shutil.which("ffprobe") is not None



def save_upload(upload: UploadFile, target_dir: Path) -> Path:
    suffix = Path(upload.filename or "video.mp4").suffix or ".mp4"
    video_path = target_dir / f"source{suffix}"
    with video_path.open("wb") as f:
        while True:
            chunk = upload.file.read(1024 * 1024)
            if not chunk:
                break
            f.write(chunk)
            if f.tell() > MAX_UPLOAD_MB * 1024 * 1024:
                raise HTTPException(status_code=413, detail=f"File quá lớn. Giới hạn {MAX_UPLOAD_MB} MB.")
    return video_path



def run_ffprobe_duration(video_path: Path) -> Optional[float]:
    try:
        cmd = [
            "ffprobe",
            "-v",
            "error",
            "-show_entries",
            "format=duration",
            "-of",
            "default=noprint_wrappers=1:nokey=1",
            str(video_path),
        ]
        result = subprocess.run(cmd, capture_output=True, text=True, check=True)
        return float(result.stdout.strip())
    except Exception:
        return None



def transcribe_video(video_path: Path, model_size: str = DEFAULT_MODEL_SIZE) -> List[SegmentOut]:
    model = get_model(model_size)
    segments, _info = model.transcribe(
        str(video_path),
        language="vi",
        vad_filter=True,
        beam_size=5,
        condition_on_previous_text=True,
    )
    rows: List[SegmentOut] = []
    for idx, seg in enumerate(segments, start=1):
        text = (seg.text or "").strip()
        if not text:
            continue
        rows.append(
            SegmentOut(
                id=idx,
                start=float(seg.start),
                end=float(seg.end),
                text=text,
            )
        )
    if not rows:
        raise HTTPException(status_code=400, detail="Không nhận diện được lời thoại trong video.")
    return rows



def format_srt_time(seconds: float) -> str:
    total_ms = max(0, int(round(seconds * 1000)))
    hours = total_ms // 3600000
    total_ms %= 3600000
    minutes = total_ms // 60000
    total_ms %= 60000
    secs = total_ms // 1000
    millis = total_ms % 1000
    return f"{hours:02d}:{minutes:02d}:{secs:02d},{millis:03d}"



def parse_time_string(value: str) -> float:
    value = value.strip()
    if not value:
        return 0.0
    value = value.replace(".", ",")
    try:
        hhmmss, ms = value.split(",") if "," in value else (value, "0")
        parts = hhmmss.split(":")
        if len(parts) == 2:
            hours = 0
            minutes, secs = parts
        elif len(parts) == 3:
            hours, minutes, secs = parts
        else:
            raise ValueError
        return int(hours) * 3600 + int(minutes) * 60 + int(secs) + int(ms.ljust(3, "0")[:3]) / 1000.0
    except Exception as exc:
        raise HTTPException(status_code=400, detail=f"Sai định dạng thời gian: {value}") from exc



def write_srt(job_dir: Path, segments: List[SegmentIn]) -> Path:
    srt_path = job_dir / "edited.srt"
    lines: List[str] = []
    cleaned = sorted(segments, key=lambda s: parse_time_string(s.start))
    for idx, seg in enumerate(cleaned, start=1):
        start_sec = parse_time_string(seg.start)
        end_sec = parse_time_string(seg.end)
        if end_sec <= start_sec:
            end_sec = start_sec + 1.0
        text = (seg.text or "").strip()
        if not text:
            continue
        lines.extend(
            [
                str(idx),
                f"{format_srt_time(start_sec)} --> {format_srt_time(end_sec)}",
                text,
                "",
            ]
        )
    if not lines:
        raise HTTPException(status_code=400, detail="Không có subtitle hợp lệ để xuất SRT.")
    srt_path.write_text("\n".join(lines), encoding="utf-8")
    return srt_path



def burn_subtitles(job_dir: Path, video_path: Path, srt_path: Path) -> Path:
    output_path = job_dir / "output_subtitled.mp4"
    subtitle_filter = (
        "subtitles=edited.srt:"
        "force_style='FontName=DejaVu Sans,FontSize=20,Outline=1,Shadow=0,MarginV=18,Alignment=2'"
    )
    cmd = [
        "ffmpeg",
        "-y",
        "-i",
        video_path.name,
        "-vf",
        subtitle_filter,
        "-c:v",
        "libx264",
        "-preset",
        "veryfast",
        "-crf",
        "23",
        "-c:a",
        "aac",
        "-b:a",
        "192k",
        output_path.name,
    ]
    try:
        subprocess.run(cmd, cwd=job_dir, capture_output=True, text=True, check=True)
    except subprocess.CalledProcessError as exc:
        stderr = (exc.stderr or "").strip()
        raise HTTPException(status_code=500, detail=f"FFmpeg lỗi khi xuất MP4: {stderr[:1200]}") from exc
    return output_path



def job_meta_path(job_dir: Path) -> Path:
    return job_dir / "meta.json"



def save_job_meta(job_dir: Path, data: dict) -> None:
    job_meta_path(job_dir).write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")



def load_job_meta(job_id: str) -> dict:
    meta = job_meta_path(WORK_DIR / job_id)
    if not meta.exists():
        raise HTTPException(status_code=404, detail="Không tìm thấy job.")
    return json.loads(meta.read_text(encoding="utf-8"))


@app.get("/", response_class=HTMLResponse)
def home(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


@app.get("/health")
def health():
    return {
        "ok": True,
        "ffmpeg": ffmpeg_exists(),
        "workspace": str(WORK_DIR),
        "default_model": DEFAULT_MODEL_SIZE,
    }


@app.post("/api/transcribe")
def api_transcribe(file: UploadFile = File(...)):
    cleanup_old_jobs()
    if not ffmpeg_exists():
        raise HTTPException(status_code=500, detail="Máy chủ chưa có FFmpeg.")

    filename = file.filename or "video.mp4"
    if not filename.lower().endswith((".mp4", ".mov", ".mkv", ".avi", ".webm", ".m4v")):
        raise HTTPException(status_code=400, detail="Chỉ hỗ trợ video mp4, mov, mkv, avi, webm, m4v.")

    job_id = uuid.uuid4().hex
    job_dir = WORK_DIR / job_id
    job_dir.mkdir(parents=True, exist_ok=True)
    try:
        video_path = save_upload(file, job_dir)
        duration = run_ffprobe_duration(video_path)
        segments = transcribe_video(video_path)
        save_job_meta(
            job_dir,
            {
                "job_id": job_id,
                "video_path": video_path.name,
                "duration": duration,
                "created_at": datetime.utcnow().isoformat() + "Z",
            },
        )
        return JSONResponse(
            {
                "job_id": job_id,
                "duration": duration,
                "segments": [
                    {
                        "id": seg.id,
                        "start": format_srt_time(seg.start),
                        "end": format_srt_time(seg.end),
                        "text": seg.text,
                    }
                    for seg in segments
                ],
            }
        )
    except Exception:
        shutil.rmtree(job_dir, ignore_errors=True)
        raise


@app.post("/api/export")
def api_export(payload: ExportRequest):
    job_dir = WORK_DIR / payload.job_id
    if not job_dir.exists():
        raise HTTPException(status_code=404, detail="Job đã hết hạn hoặc không tồn tại.")

    meta = load_job_meta(payload.job_id)
    video_path = job_dir / meta["video_path"]
    if not video_path.exists():
        raise HTTPException(status_code=404, detail="Không tìm thấy video gốc để xuất lại.")

    srt_path = write_srt(job_dir, payload.segments)
    response = {
        "job_id": payload.job_id,
        "srt_url": f"/download/{payload.job_id}/srt",
        "mp4_url": None,
    }

    if payload.burn_in:
        mp4_path = burn_subtitles(job_dir, video_path, srt_path)
        response["mp4_url"] = f"/download/{payload.job_id}/mp4"
        response["mp4_size_mb"] = round(mp4_path.stat().st_size / (1024 * 1024), 2)

    return JSONResponse(response)


@app.get("/download/{job_id}/srt")
def download_srt(job_id: str):
    path = WORK_DIR / job_id / "edited.srt"
    if not path.exists():
        raise HTTPException(status_code=404, detail="Chưa có file SRT.")
    return FileResponse(path, media_type="application/x-subrip", filename=f"{job_id}.srt")


@app.get("/download/{job_id}/mp4")
def download_mp4(job_id: str):
    path = WORK_DIR / job_id / "output_subtitled.mp4"
    if not path.exists():
        raise HTTPException(status_code=404, detail="Chưa có file MP4.")
    return FileResponse(path, media_type="video/mp4", filename=f"{job_id}.mp4")


if __name__ == "__main__":
    import uvicorn

    port = int(os.getenv("PORT", "7860"))
    uvicorn.run("app:app", host="0.0.0.0", port=port, reload=False)