File size: 23,444 Bytes
94e11ec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
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, Form, 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 SubtitleStyle(BaseModel):
    font_name: str = "DejaVu Sans"
    font_color: str = "#FFFFFF"          # Hex color for text
    highlight_color: str = "#FFD700"     # Hex color for karaoke highlight
    outline_color: str = "#000000"       # Hex color for outline
    outline_width: int = 2               # Outline thickness (px)
    font_size_pct: int = 100             # Font size percentage (50-200)
    position_pct: int = 90               # Vertical position 0=top, 100=bottom
    karaoke_mode: bool = False           # Word-by-word karaoke highlight


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


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


# ============================================================
#  TRANSCRIPTION — 2 chế độ: "music" (lời bài hát) và "speech" (giọng nói)
# ============================================================

def merge_segments_music(raw_segments: list, max_gap: float = 0.8, max_len: float = 8.0) -> list:
    """
    Gộp các segment ngắn liên tiếp thành câu dài hơn, phù hợp lời bài hát.
    - max_gap: khoảng trống tối đa giữa 2 segment để gộp (giây)
    - max_len: độ dài tối đa 1 segment sau gộp (giây)
    """
    if not raw_segments:
        return []

    merged = []
    current = {
        "start": raw_segments[0]["start"],
        "end": raw_segments[0]["end"],
        "text": raw_segments[0]["text"],
    }

    for seg in raw_segments[1:]:
        gap = seg["start"] - current["end"]
        new_duration = seg["end"] - current["start"]

        # Gộp nếu: khoảng trống nhỏ VÀ tổng thời lượng không quá dài
        if gap <= max_gap and new_duration <= max_len:
            current["end"] = seg["end"]
            current["text"] = current["text"] + " " + seg["text"]
        else:
            merged.append(current)
            current = {
                "start": seg["start"],
                "end": seg["end"],
                "text": seg["text"],
            }

    merged.append(current)
    return merged


def fill_timeline_gaps(segments: list, total_duration: Optional[float] = None, min_gap: float = 0.3) -> list:
    """
    Lấp khoảng trống lớn giữa các segment.
    Nếu khoảng trống > min_gap, điều chỉnh end/start của segment kề cho liền mạch.
    Giúp subtitle phủ toàn bộ timeline video.
    """
    if not segments:
        return segments

    result = []
    for i, seg in enumerate(segments):
        s = dict(seg)

        # Kéo start sớm hơn để lấp gap phía trước
        if i > 0:
            prev_end = result[-1]["end"]
            gap = s["start"] - prev_end
            if 0 < gap <= 1.5:
                # Gap nhỏ: kéo start segment hiện tại lùi lại
                s["start"] = prev_end
            elif gap > 1.5:
                # Gap lớn: kéo end segment trước ra + kéo start hiện tại lùi
                half = gap / 2
                result[-1]["end"] = prev_end + min(half, 0.5)
                s["start"] = s["start"] - min(half, 0.5)

        result.append(s)

    # Xử lý end của segment cuối nếu có total_duration
    if total_duration and result:
        last = result[-1]
        remaining = total_duration - last["end"]
        if 0 < remaining <= 2.0:
            last["end"] = total_duration

    return result


def transcribe_video_music(video_path: Path, duration: Optional[float] = None,
                           model_size: str = DEFAULT_MODEL_SIZE) -> List[SegmentOut]:
    """
    Chế độ LỜI BÀI HÁT: tối ưu để nhận diện toàn bộ lyrics.
    - Tắt VAD filter (không cắt đoạn nhạc nền)
    - Tăng beam_size cho accuracy
    - Bật word_timestamps cho khớp chính xác
    - Gộp segment thông minh
    - Lấp khoảng trống timeline
    """
    model = get_model(model_size)

    segments, info = model.transcribe(
        str(video_path),
        language="vi",
        vad_filter=False,           # QUAN TRỌNG: tắt VAD để không bỏ sót lời hát
        beam_size=8,                # Tăng beam cho accuracy lời bài hát
        best_of=5,                  # Sample nhiều hơn, chọn tốt nhất
        patience=1.5,               # Kiên nhẫn hơn khi decode
        condition_on_previous_text=True,
        word_timestamps=True,       # Timestamp cấp từ → khớp chính xác
        no_speech_threshold=0.3,    # Hạ threshold → ít bỏ sót đoạn hát nhỏ
        log_prob_threshold=-1.5,    # Chấp nhận xác suất thấp hơn (lời hát khó nghe)
        compression_ratio_threshold=2.8,  # Nới ngưỡng nén → ít reject segment
    )

    raw: list = []
    for seg in segments:
        text = (seg.text or "").strip()
        if not text:
            continue
        raw.append({
            "start": float(seg.start),
            "end": float(seg.end),
            "text": text,
        })

    if not raw:
        raise HTTPException(status_code=400, detail="Không nhận diện được lời thoại/lời hát trong video.")

    # Gộp segment ngắn thành câu lời bài hát tự nhiên
    merged = merge_segments_music(raw, max_gap=0.8, max_len=8.0)

    # Lấp khoảng trống timeline
    filled = fill_timeline_gaps(merged, total_duration=duration)

    rows: List[SegmentOut] = []
    for idx, seg in enumerate(filled, start=1):
        rows.append(SegmentOut(
            id=idx,
            start=seg["start"],
            end=seg["end"],
            text=seg["text"],
        ))

    return rows


def transcribe_video_speech(video_path: Path, model_size: str = DEFAULT_MODEL_SIZE) -> List[SegmentOut]:
    """
    Chế độ GIỌNG NÓI: giữ nguyên logic cũ, tối ưu cho lời thoại/thuyết trình.
    - Bật VAD filter (lọc tiếng ồn)
    - beam_size vừa phải
    """
    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 hex_to_ass_color(hex_color: str) -> str:
    """
    Chuyển đổi hex color (#RRGGBB) thành ASS color (&HBBGGRR&).
    ASS dùng format BGR ngược lại.
    """
    h = hex_color.lstrip("#")
    if len(h) != 6:
        h = "FFFFFF"  # fallback white
    r, g, b = h[0:2], h[2:4], h[4:6]
    return f"&H00{b.upper()}{g.upper()}{r.upper()}&"


def build_force_style(style: Optional["SubtitleStyle"] = None) -> str:
    """
    Tạo chuỗi force_style cho FFmpeg subtitles filter dựa trên SubtitleStyle.
    """
    if style is None:
        return "FontName=DejaVu Sans,FontSize=20,Outline=1,Shadow=0,MarginV=18,Alignment=2"

    # Font name — dùng font_name gửi từ frontend
    font_name = style.font_name or "DejaVu Sans"

    # Font size: base 20, scale theo pct
    base_size = 20
    font_size = max(10, int(base_size * style.font_size_pct / 100))

    # Colors (ASS format)
    primary_color = hex_to_ass_color(style.font_color)
    outline_color = hex_to_ass_color(style.outline_color)

    # Outline width
    outline = max(0, min(6, style.outline_width))

    # MarginV: convert position_pct (0=top, 100=bottom)
    # ASS MarginV: khoảng cách từ cạnh (lớn = xa cạnh dưới hơn = lên cao hơn)
    # position_pct 90 = gần đáy → MarginV nhỏ
    # position_pct 10 = gần đỉnh → MarginV lớn
    # Quy đổi: MarginV = (100 - position_pct) * 3, clamp 5..280
    margin_v = max(5, min(280, int((100 - style.position_pct) * 3)))

    # Alignment: 2 = bottom center (mặc định phụ đề)
    # Nếu position < 50, dùng alignment 8 (top center)
    alignment = 8 if style.position_pct < 40 else 2

    parts = [
        f"FontName={font_name}",
        f"FontSize={font_size}",
        f"PrimaryColour={primary_color}",
        f"OutlineColour={outline_color}",
        f"Outline={outline}",
        f"Shadow=0",
        f"MarginV={margin_v}",
        f"Alignment={alignment}",
        f"Bold=1",
    ]
    return ",".join(parts)


def write_ass_karaoke(job_dir: Path, segments: List["SegmentIn"], style: Optional["SubtitleStyle"] = None) -> Path:
    """
    Tạo file ASS với karaoke word-by-word highlight (\kf tags).
    Mỗi từ được highlight lần lượt theo thời gian segment.
    """
    ass_path = job_dir / "karaoke.ass"
    s = style or SubtitleStyle()

    font_name = s.font_name or "DejaVu Sans"
    base_size = 20
    font_size = max(10, int(base_size * s.font_size_pct / 100))
    primary_color = hex_to_ass_color(s.font_color)
    highlight_color = hex_to_ass_color(s.highlight_color)
    outline_color = hex_to_ass_color(s.outline_color)
    outline = max(0, min(6, s.outline_width))
    margin_v = max(5, min(280, int((100 - s.position_pct) * 3)))
    alignment = 8 if s.position_pct < 40 else 2

    header = f"""[Script Info]
Title: Viet AutoSub Karaoke
ScriptType: v4.00+
PlayResX: 1280
PlayResY: 720
ScaledBorderAndShadow: yes

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,{font_name},{font_size},{primary_color},{highlight_color},{outline_color},&H80000000&,1,0,0,0,100,100,0,0,1,{outline},0,{alignment},20,20,{margin_v},1

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
"""
    lines_out: List[str] = [header.strip()]

    cleaned = sorted(segments, key=lambda seg: parse_time_string(seg.start))
    for seg in cleaned:
        text = (seg.text or "").strip()
        if not text:
            continue

        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

        # ASS time format: H:MM:SS.cc
        def sec_to_ass(seconds: float) -> str:
            total_cs = max(0, int(round(seconds * 100)))
            h = total_cs // 360000
            total_cs %= 360000
            m = total_cs // 6000
            total_cs %= 6000
            ss = total_cs // 100
            cs = total_cs % 100
            return f"{h}:{m:02d}:{ss:02d}.{cs:02d}"

        ass_start = sec_to_ass(start_sec)
        ass_end = sec_to_ass(end_sec)

        # Split text into words, distribute time evenly
        words = text.split()
        if not words:
            continue

        duration_cs = max(1, int(round((end_sec - start_sec) * 100)))
        per_word_cs = max(1, duration_cs // len(words))

        # Build karaoke text with \kf tags
        # \kf = smooth fill karaoke effect
        karaoke_parts = []
        for word in words:
            karaoke_parts.append(f"{{\\kf{per_word_cs}}}{word}")

        karaoke_text = " ".join(karaoke_parts)
        # Override highlight color for karaoke fill: use SecondaryColour via \1c for filled portion
        # Use \K (uppercase) style coloring: {\1c&highlight&} before karaoke
        color_override = f"{{\\1c{highlight_color}}}"
        line = f"Dialogue: 0,{ass_start},{ass_end},Default,,0,0,0,,{color_override}{karaoke_text}"
        lines_out.append(line)

    ass_path.write_text("\n".join(lines_out), encoding="utf-8")
    return ass_path


def burn_subtitles(job_dir: Path, video_path: Path, srt_path: Path,
                   style: Optional["SubtitleStyle"] = None) -> Path:
    output_path = job_dir / "output_subtitled.mp4"

    # Xác định dùng karaoke ASS hay SRT thường
    if style and style.karaoke_mode:
        # Tạo file ASS karaoke
        ass_path = write_ass_karaoke(job_dir, [], style)  # placeholder, sẽ được ghi đè bên dưới
        subtitle_filter = f"ass=karaoke.ass"
    else:
        force_style = build_force_style(style)
        subtitle_filter = f"subtitles=edited.srt:force_style='{force_style}'"

    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(...),
    mode: str = Form(default="music"),
):
    """
    mode: "music" (lời bài hát) hoặc "speech" (giọng nói/thuyết trình)
    """
    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.")

    if mode not in ("music", "speech"):
        mode = "music"

    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)

        if mode == "music":
            segments = transcribe_video_music(video_path, duration=duration)
        else:
            segments = transcribe_video_speech(video_path)

        # Tính coverage: tổng thời lượng sub / tổng video
        total_sub_time = sum(s.end - s.start for s in segments)
        coverage_pct = round((total_sub_time / duration * 100), 1) if duration and duration > 0 else 0

        save_job_meta(
            job_dir,
            {
                "job_id": job_id,
                "video_path": video_path.name,
                "duration": duration,
                "mode": mode,
                "created_at": datetime.utcnow().isoformat() + "Z",
            },
        )
        return JSONResponse(
            {
                "job_id": job_id,
                "duration": duration,
                "mode": mode,
                "coverage_pct": coverage_pct,
                "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:
        # Nếu karaoke mode, tạo file ASS từ segments
        if payload.style and payload.style.karaoke_mode:
            write_ass_karaoke(job_dir, payload.segments, payload.style)
        mp4_path = burn_subtitles(job_dir, video_path, srt_path, style=payload.style)
        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)