hit-cxf
2026-06-02 6f5bcb60d0a71678c79c1450e5cc81eb8ee757bd
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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
import asyncio
import base64
import json
import logging
import math
import os
import re
import shutil
import subprocess
import sys
import tempfile
from html import escape
from pathlib import Path
from typing import Any, Iterable, Optional
 
from PIL import Image, ImageDraw, ImageFont
 
from config import BASE_DIR
 
logger = logging.getLogger(__name__)
 
TEMPLATE_FIELD_DEFAULTS = {
    "title": "山河入梦",
    "text": "心之所向,素履而往",
    "author": "HITsz-TMG",
    "describe": "开源视频生成智能体",
    "brand": "Video-Claw",
    "signature": "HITsz-TMG",
    "subtitle": "这是一个副标题",
}
CUSTOM_TEMPLATE_FIELDS = ("author", "describe", "brand", "signature", "subtitle")
 
 
def write_text(path: str, content: str) -> str:
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        f.write(content)
    return path
 
 
def write_json(path: str, data: Any) -> str:
    os.makedirs(os.path.dirname(path), exist_ok=True)
    with open(path, "w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=2)
    return path
 
 
def artifact(path: str, kind: str, name: Optional[str] = None) -> dict[str, Any]:
    return {
        "kind": kind,
        "name": name or os.path.basename(path),
        "path": path,
        "exists": os.path.exists(path),
    }
 
 
def extract_json_array(text: str) -> list[Any]:
    text = text.strip()
    try:
        data = json.loads(text)
        if isinstance(data, list):
            return data
    except Exception:
        pass
 
    match = re.search(r"```(?:json)?\s*(\[.*?\])\s*```", text, re.S)
    if not match:
        match = re.search(r"(\[.*\])", text, re.S)
    if match:
        data = json.loads(match.group(1))
        if isinstance(data, list):
            return data
    raise ValueError("Model response did not contain a JSON array.")
 
 
def split_script(text: str, split_mode: str = "paragraph") -> list[str]:
    if split_mode == "line":
        parts = [line.strip() for line in text.splitlines()]
    elif split_mode == "sentence":
        parts = [part.strip() for part in re.split(r"(?<=[。!?.!?])\s*", text)]
    else:
        parts = [part.strip() for part in re.split(r"\n\s*\n", text)]
    return [part for part in parts if part]
 
 
def copy_input_file(path: str, output_dir: str, prefix: str) -> str:
    if path.startswith(("http://", "https://", "file://", "data:")):
        return path
    src = Path(path)
    if not src.exists():
        raise FileNotFoundError(f"Input file not found: {path}")
    dst = Path(output_dir) / f"{prefix}{src.suffix.lower()}"
    shutil.copy2(src, dst)
    return str(dst)
 
 
async def run_blocking(func, *args, **kwargs):
    return await asyncio.to_thread(func, *args, **kwargs)
 
 
def concat_videos(video_paths: Iterable[str], output_path: str) -> Optional[str]:
    paths = [path for path in video_paths if path and os.path.exists(path)]
    if not paths:
        return None
 
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        logger.warning("ffmpeg not found; cannot concatenate videos")
        return None
 
    list_path = os.path.join(os.path.dirname(output_path), "concat.txt")
    with open(list_path, "w", encoding="utf-8") as f:
        for path in paths:
            f.write(f"file '{os.path.abspath(path)}'\n")
 
    cmd = [ffmpeg, "-y", "-f", "concat", "-safe", "0", "-i", list_path, "-c", "copy", output_path]
    logger.info("Concatenating %d videos -> %s", len(paths), output_path)
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    logger.info("Video concatenation complete: %s", output_path)
    return output_path
 
 
def create_static_image_clip(
    image_path: str,
    audio_path: str,
    output_path: str,
    *,
    video_ratio: str = "9:16",
    duration: Optional[float] = None,
) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to create static short-video clips.")
    if not os.path.exists(image_path):
        raise FileNotFoundError(f"Image not found: {image_path}")
    if not os.path.exists(audio_path):
        raise FileNotFoundError(f"Audio not found: {audio_path}")
 
    width, height = _resolution_from_ratio(video_ratio)
    clip_duration = duration or media_duration_seconds(audio_path) or 3.0
    vf = (
        f"scale={width}:{height}:force_original_aspect_ratio=decrease,"
        f"pad={width}:{height}:(ow-iw)/2:(oh-ih)/2:color=black,"
        "format=yuv420p"
    )
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    logger.info(
        "Creating static image clip: image=%s audio=%s duration=%.2fs -> %s",
        image_path,
        audio_path,
        clip_duration,
        output_path,
    )
    cmd = [
        ffmpeg,
        "-y",
        "-loop",
        "1",
        "-framerate",
        "30",
        "-i",
        image_path,
        "-i",
        audio_path,
        "-t",
        f"{clip_duration:.3f}",
        "-vf",
        vf,
        "-c:v",
        "libx264",
        "-preset",
        "veryfast",
        "-crf",
        "18",
        "-c:a",
        "aac",
        "-shortest",
        "-movflags",
        "+faststart",
        output_path,
    ]
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_path
 
 
def _load_font(size: int) -> ImageFont.ImageFont:
    candidates = [
        "/System/Library/Fonts/PingFang.ttc",
        "/System/Library/Fonts/STHeiti Light.ttc",
        "/System/Library/Fonts/Supplemental/Arial Unicode.ttf",
        "/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc",
        "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
    ]
    for path in candidates:
        if os.path.exists(path):
            try:
                return ImageFont.truetype(path, size)
            except Exception:
                continue
    return ImageFont.load_default()
 
 
def _text_width(draw: ImageDraw.ImageDraw, text: str, font: ImageFont.ImageFont) -> int:
    bbox = draw.textbbox((0, 0), text, font=font)
    return bbox[2] - bbox[0]
 
 
def _wrap_text(draw: ImageDraw.ImageDraw, text: str, font: ImageFont.ImageFont, max_width: int) -> list[str]:
    lines: list[str] = []
    for paragraph in str(text or "").splitlines() or [""]:
        current = ""
        for char in paragraph.strip():
            candidate = current + char
            if current and _text_width(draw, candidate, font) > max_width:
                lines.append(current)
                current = char
            else:
                current = candidate
        if current:
            lines.append(current)
    return lines or [""]
 
 
def _draw_centered_lines(
    draw: ImageDraw.ImageDraw,
    lines: list[str],
    *,
    center_x: int,
    y: int,
    font: ImageFont.ImageFont,
    fill: tuple[int, int, int, int],
    stroke_width: int,
    stroke_fill: tuple[int, int, int, int],
    line_gap: int,
) -> int:
    cursor = y
    for line in lines:
        bbox = draw.textbbox((0, 0), line, font=font, stroke_width=stroke_width)
        width = bbox[2] - bbox[0]
        height = bbox[3] - bbox[1]
        draw.text(
            (center_x - width / 2, cursor),
            line,
            font=font,
            fill=fill,
            stroke_width=stroke_width,
            stroke_fill=stroke_fill,
        )
        cursor += height + line_gap
    return cursor
 
 
def render_static_text_image(
    image_path: str,
    output_path: str,
    *,
    subtitle: str,
    title: Optional[str] = None,
    video_ratio: str = "9:16",
) -> str:
    if not os.path.exists(image_path):
        raise FileNotFoundError(f"Image not found: {image_path}")
 
    width, height = _resolution_from_ratio(video_ratio)
    with Image.open(image_path) as source:
        source = source.convert("RGB")
        source.thumbnail((width, height), Image.Resampling.LANCZOS)
        canvas = Image.new("RGB", (width, height), (0, 0, 0))
        canvas.paste(source, ((width - source.width) // 2, (height - source.height) // 2))
 
    image = canvas.convert("RGBA")
    overlay = Image.new("RGBA", image.size, (0, 0, 0, 0))
    draw = ImageDraw.Draw(overlay)
 
    title_font = _load_font(max(48, int(height * 0.06)))
    subtitle_font = _load_font(max(32, int(height * 0.035)))
    margin_x = max(48, int(width * 0.07))
    max_text_width = width - margin_x * 2
 
    if title:
        title_lines = _wrap_text(draw, title, title_font, max_text_width)
        title_line_height = max(1, draw.textbbox((0, 0), "国", font=title_font)[3])
        title_height = len(title_lines) * title_line_height + max(0, len(title_lines) - 1) * 10
        title_y = max(48, int(height * 0.055))
        _draw_centered_lines(
            draw,
            title_lines,
            center_x=width // 2,
            y=title_y,
            font=title_font,
            fill=(255, 255, 255, 255),
            stroke_width=3,
            stroke_fill=(0, 0, 0, 210),
            line_gap=10,
        )
 
    subtitle_lines = _wrap_text(draw, subtitle, subtitle_font, max_text_width)
    subtitle_line_height = max(1, draw.textbbox((0, 0), "国", font=subtitle_font)[3])
    subtitle_height = len(subtitle_lines) * subtitle_line_height + max(0, len(subtitle_lines) - 1) * 10
    subtitle_y = height - max(96, int(height * 0.08)) - subtitle_height
    _draw_centered_lines(
        draw,
        subtitle_lines,
        center_x=width // 2,
        y=subtitle_y,
        font=subtitle_font,
        fill=(255, 255, 255, 255),
        stroke_width=3,
        stroke_fill=(0, 0, 0, 230),
        line_gap=10,
    )
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    Image.alpha_composite(image, overlay).convert("RGB").save(output_path, quality=95)
    logger.info("Rendered static title/subtitle image: %s -> %s", image_path, output_path)
    return output_path
 
 
def ratio_from_size(width: int, height: int) -> str:
    if width <= 0 or height <= 0:
        return "1:1"
    divisor = max(1, math.gcd(width, height))
    return f"{width // divisor}:{height // divisor}"
 
 
def _template_size_from_id(template_id: str) -> tuple[str, int, int]:
    size = str(template_id or "").split("/", 1)[0]
    if size == "1920x1080":
        return size, 1920, 1080
    if size == "1080x1080":
        return size, 1080, 1080
    if size == "1080x1920":
        return size, 1080, 1920
    raise ValueError(f"Unsupported subtitle template size: {template_id}")
 
 
def _resolve_template_path(template_id: str, video_ratio: str) -> tuple[str, int, int]:
    if "/" in str(template_id):
        size, filename = str(template_id).split("/", 1)
        _, width, height = _template_size_from_id(template_id)
    else:
        width, height = _resolution_from_ratio(video_ratio)
        size = f"{width}x{height}"
        filename = str(template_id)
    if "/" in filename or "\\" in filename or not filename.endswith(".html"):
        raise ValueError(f"Invalid subtitle template: {template_id}")
    path = (BASE_DIR / "templates" / size / filename).resolve()
    root = (BASE_DIR / "templates" / size).resolve()
    if not str(path).startswith(str(root) + os.sep) or not path.exists():
        raise FileNotFoundError(f"Subtitle template not found: {template_id}")
    return str(path), width, height
 
 
def template_media_spec(template_id: str, video_ratio: str = "9:16") -> dict[str, Any]:
    template_path, _, _ = _resolve_template_path(template_id, video_ratio)
    with open(template_path, "r", encoding="utf-8") as f:
        raw = f.read()
    width_match = re.search(
        r'<meta\s+name=["\']template:media-width["\']\s+content=["\'](\d+)["\']',
        raw,
        re.I,
    )
    height_match = re.search(
        r'<meta\s+name=["\']template:media-height["\']\s+content=["\'](\d+)["\']',
        raw,
        re.I,
    )
    if not width_match or not height_match:
        raise ValueError(f"Subtitle template missing media size metadata: {template_id}")
    width = int(width_match.group(1))
    height = int(height_match.group(1))
    return {
        "media_width": width,
        "media_height": height,
        "media_ratio": ratio_from_size(width, height),
        "media_resolution": f"{width}*{height}",
        "supports_video": template_supports_video_media(raw),
    }
 
 
def template_supports_video_media(raw: str) -> bool:
    visible_raw = re.sub(r"<!--.*?-->", "", raw, flags=re.S)
    if re.search(r"\{\{\s*media(?:[:=][^{}]*)?\s*\}\}", visible_raw):
        return True
    return bool(
        re.search(
            r"<img\b[^>]*\bsrc=[\"']\{\{\s*image\s*\}\}[\"'][^>]*>",
            visible_raw,
            re.I,
        )
    )
 
 
def parse_template_placeholders(raw: str) -> list[dict[str, str]]:
    placeholders = []
    for match in re.finditer(r"\{\{\s*([^{}]+?)\s*\}\}", raw):
        token = match.group(1).strip()
        key = token.split(":", 1)[0].split("=", 1)[0].strip()
        field_type = token.split(":", 1)[1].split("=", 1)[0].strip() if ":" in token else "text"
        default = token.split("=", 1)[1].strip() if "=" in token else ""
        placeholders.append({
            "key": key,
            "type": field_type,
            "default": TEMPLATE_FIELD_DEFAULTS.get(key, default),
        })
    return placeholders
 
 
def template_custom_fields(template_id: str, video_ratio: str = "9:16") -> list[dict[str, str]]:
    template_path, _, _ = _resolve_template_path(template_id, video_ratio)
    with open(template_path, "r", encoding="utf-8") as f:
        placeholders = parse_template_placeholders(f.read())
 
    fields = []
    seen = set()
    for item in placeholders:
        key = item["key"]
        if key not in CUSTOM_TEMPLATE_FIELDS or key in seen:
            continue
        seen.add(key)
        fields.append(item)
    return fields
 
 
def _image_data_uri(path: str) -> str:
    if path.startswith(("http://", "https://", "data:", "file:")):
        return path
    if not os.path.exists(path):
        raise FileNotFoundError(f"Image not found: {path}")
    suffix = Path(path).suffix.lower()
    mime = {
        ".jpg": "image/jpeg",
        ".jpeg": "image/jpeg",
        ".png": "image/png",
        ".webp": "image/webp",
        ".gif": "image/gif",
    }.get(suffix, "image/png")
    with open(path, "rb") as f:
        encoded = base64.b64encode(f.read()).decode("ascii")
    return f"data:{mime};base64,{encoded}"
 
 
def _render_template_html(raw: str, values: dict[str, Any], raw_keys: Optional[set[str]] = None) -> str:
    raw_keys = raw_keys or set()
 
    def repl(match: re.Match) -> str:
        token = match.group(1).strip()
        key = token.split(":", 1)[0].split("=", 1)[0].strip()
        if key in values:
            if key in raw_keys:
                return str(values[key] or "")
            return escape(str(values[key] or ""), quote=True)
        if "=" in token:
            return escape(token.split("=", 1)[1].strip(), quote=True)
        return ""
 
    return re.sub(r"\{\{\s*([^{}]+?)\s*\}\}", repl, raw)
 
 
def _media_file_uri(path: str) -> str:
    if path.startswith(("http://", "https://", "data:", "file:")):
        return path
    if not os.path.exists(path):
        raise FileNotFoundError(f"Media not found: {path}")
    return Path(path).resolve().as_uri()
 
 
def _template_media_element(src: str, media_kind: str) -> str:
    attrs = 'class="template-media" style="width:100%;height:100%;object-fit:cover;display:block;"'
    if media_kind == "video":
        return f'<video {attrs} src="{escape(src, quote=True)}" muted playsinline preload="auto"></video>'
    return f'<img {attrs} src="{escape(src, quote=True)}" alt="">'
 
 
def _inject_template_media_css(raw: str) -> str:
    css = (
        "<style>"
        ".template-media{width:100%;height:100%;object-fit:cover;display:block;}"
        "video.template-media{background:#000;}"
        "</style>"
    )
    if "</head>" in raw:
        return raw.replace("</head>", f"{css}</head>", 1)
    return css + raw
 
 
def _prepare_template_media_html(raw: str, *, media_kind: str) -> str:
    prepared = _inject_template_media_css(raw)
    if media_kind != "video":
        return prepared
    if re.search(r"\{\{\s*media(?:[:=][^{}]*)?\s*\}\}", prepared):
        return prepared
    return re.sub(
        r"<img\b[^>]*\bsrc=[\"']\{\{\s*image\s*\}\}[\"'][^>]*>",
        "{{media}}",
        prepared,
        flags=re.I,
    )
 
 
def _template_values(
    *,
    image_path: str,
    subtitle: str,
    title: Optional[str],
    template_values: Optional[dict[str, Any]],
    index: int,
    media_kind: str = "image",
    media_path: Optional[str] = None,
) -> dict[str, Any]:
    image_uri = _image_data_uri(image_path)
    media_src = _media_file_uri(media_path) if media_kind == "video" and media_path else image_uri
    return {
        **TEMPLATE_FIELD_DEFAULTS,
        **(template_values or {}),
        "title": title or TEMPLATE_FIELD_DEFAULTS["title"],
        "text": subtitle or "",
        "image": image_uri,
        "media": _template_media_element(media_src, media_kind),
        "index": index,
    }
 
 
def _install_playwright_chromium() -> None:
    cmd = [sys.executable, "-m", "playwright", "install", "chromium"]
    logger.info("Installing Playwright Chromium for HTML subtitle templates")
    try:
        subprocess.run(cmd, check=True)
    except subprocess.CalledProcessError as exc:
        raise RuntimeError(
            "Playwright is installed, but Chromium is missing and automatic installation failed. "
            "Run `python -m playwright install chromium` manually and retry."
        ) from exc
 
 
def _launch_playwright_chromium(playwright):
    try:
        return playwright.chromium.launch(headless=True)
    except Exception as exc:
        message = str(exc)
        if "Executable doesn't exist" not in message and "playwright install" not in message:
            raise
        _install_playwright_chromium()
        return playwright.chromium.launch(headless=True)
 
 
def render_template_text_image(
    image_path: str,
    output_path: str,
    *,
    subtitle: str,
    title: Optional[str] = None,
    video_ratio: str = "9:16",
    template_id: str,
    template_values: Optional[dict[str, Any]] = None,
    index: int = 1,
) -> str:
    template_path, width, height = _resolve_template_path(template_id, video_ratio)
    with open(template_path, "r", encoding="utf-8") as f:
        raw = _prepare_template_media_html(f.read(), media_kind="image")
    html = _render_template_html(
        raw,
        _template_values(
            image_path=image_path,
            subtitle=subtitle,
            title=title,
            template_values=template_values,
            index=index,
        ),
        raw_keys={"media"},
    )
 
    try:
        from playwright.sync_api import sync_playwright
    except ImportError as exc:
        raise RuntimeError(
            "HTML subtitle templates require Playwright. "
            "Install backend dependencies again or run `pip install playwright`."
        ) from exc
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    with sync_playwright() as playwright:
        browser = _launch_playwright_chromium(playwright)
        try:
            page = browser.new_page(viewport={"width": width, "height": height}, device_scale_factor=1)
            page.set_content(html, wait_until="networkidle", timeout=30000)
            page.screenshot(path=output_path, full_page=False, type="jpeg", quality=95)
        finally:
            browser.close()
    logger.info("Rendered HTML template subtitle image: template=%s image=%s -> %s", template_id, image_path, output_path)
    return output_path
 
 
def render_template_media_video(
    media_video_path: str,
    output_path: str,
    *,
    poster_image_path: str,
    subtitle: str,
    title: Optional[str] = None,
    video_ratio: str = "9:16",
    template_id: str,
    template_values: Optional[dict[str, Any]] = None,
    index: int = 1,
    duration: Optional[float] = None,
    fps: int = 24,
) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to render HTML template videos.")
    if not os.path.exists(media_video_path):
        raise FileNotFoundError(f"Template media video not found: {media_video_path}")
 
    template_path, width, height = _resolve_template_path(template_id, video_ratio)
    with open(template_path, "r", encoding="utf-8") as f:
        raw = f.read()
    if not template_supports_video_media(raw):
        raise ValueError(f"Template does not support video media: {template_id}")
 
    raw = _prepare_template_media_html(raw, media_kind="video")
    html = _render_template_html(
        raw,
        _template_values(
            image_path=poster_image_path,
            media_kind="image",
            subtitle=subtitle,
            title=title,
            template_values=template_values,
            index=index,
        ),
        raw_keys={"media"},
    )
 
    try:
        from playwright.sync_api import sync_playwright
    except ImportError as exc:
        raise RuntimeError(
            "HTML subtitle templates require Playwright. "
            "Install backend dependencies again or run `pip install playwright`."
        ) from exc
 
    clip_duration = float(duration or media_duration_seconds(media_video_path) or 3.0)
    frame_count = max(1, int(math.ceil(clip_duration * fps)))
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    frame_dir = tempfile.mkdtemp(prefix="template_frames_", dir=os.path.dirname(output_path))
    media_frame_dir = tempfile.mkdtemp(prefix="template_media_frames_", dir=os.path.dirname(output_path))
    frame_pattern = os.path.join(frame_dir, "frame_%05d.jpg")
    media_frame_pattern = os.path.join(media_frame_dir, "media_%05d.jpg")
 
    logger.info(
        "Rendering HTML template video: template=%s media=%s duration=%.2fs fps=%d -> %s",
        template_id,
        media_video_path,
        clip_duration,
        fps,
        output_path,
    )
    try:
        extract_cmd = [
            ffmpeg,
            "-y",
            "-i",
            media_video_path,
            "-vf",
            f"fps={fps}",
            "-q:v",
            "2",
            media_frame_pattern,
        ]
        subprocess.run(extract_cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        media_frames = sorted(str(path) for path in Path(media_frame_dir).glob("media_*.jpg"))
        if not media_frames:
            media_frames = [poster_image_path]
 
        with sync_playwright() as playwright:
            browser = _launch_playwright_chromium(playwright)
            try:
                page = browser.new_page(viewport={"width": width, "height": height}, device_scale_factor=1)
                page.set_content(html, wait_until="networkidle", timeout=30000)
                for frame_index in range(frame_count):
                    media_frame_uri = _image_data_uri(media_frames[frame_index % len(media_frames)])
                    page.evaluate(
                        """async (src) => {
                            const images = Array.from(document.querySelectorAll('img.template-media'));
                            await Promise.all(images.map(async (image) => {
                                image.src = src;
                                if (image.decode) {
                                    await image.decode().catch(() => {});
                                }
                            }));
                            await new Promise(resolve => requestAnimationFrame(resolve));
                        }""",
                        media_frame_uri,
                    )
                    page.screenshot(
                        path=os.path.join(frame_dir, f"frame_{frame_index + 1:05d}.jpg"),
                        full_page=False,
                        type="jpeg",
                        quality=92,
                    )
            finally:
                browser.close()
 
        cmd = [
            ffmpeg,
            "-y",
            "-framerate",
            str(fps),
            "-i",
            frame_pattern,
            "-t",
            f"{clip_duration:.3f}",
            "-vf",
            "format=yuv420p",
            "-c:v",
            "libx264",
            "-preset",
            "veryfast",
            "-crf",
            "18",
            "-movflags",
            "+faststart",
            output_path,
        ]
        subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    finally:
        shutil.rmtree(frame_dir, ignore_errors=True)
        shutil.rmtree(media_frame_dir, ignore_errors=True)
    return output_path
 
 
def concat_audios(audio_paths: Iterable[str], output_path: str) -> Optional[str]:
    paths = [path for path in audio_paths if path and os.path.exists(path)]
    if not paths:
        return None
    if len(paths) == 1:
        return paths[0]
 
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        logger.warning("ffmpeg not found; cannot concatenate audios")
        return None
 
    list_path = os.path.join(os.path.dirname(output_path), "concat_audio.txt")
    with open(list_path, "w", encoding="utf-8") as f:
        for path in paths:
            f.write(f"file '{os.path.abspath(path)}'\n")
 
    cmd = [ffmpeg, "-y", "-f", "concat", "-safe", "0", "-i", list_path, "-c", "copy", output_path]
    logger.info("Concatenating %d audios -> %s", len(paths), output_path)
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_path
 
 
def replace_video_audio(video_path: str, audio_path: str, output_path: str) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to replace digital-human video audio.")
    if not os.path.exists(video_path):
        raise FileNotFoundError(f"Video not found: {video_path}")
    if not os.path.exists(audio_path):
        raise FileNotFoundError(f"Audio not found: {audio_path}")
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    logger.info("Replacing video audio: video=%s audio=%s -> %s", video_path, audio_path, output_path)
    cmd = [
        ffmpeg,
        "-y",
        "-i",
        video_path,
        "-i",
        audio_path,
        "-map",
        "0:v:0",
        "-map",
        "1:a:0",
        "-c:v",
        "copy",
        "-c:a",
        "aac",
        "-shortest",
        output_path,
    ]
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_path
 
 
def media_duration_seconds(path: str) -> Optional[float]:
    ffprobe = shutil.which("ffprobe")
    if not ffprobe or not os.path.exists(path):
        return None
 
    cmd = [
        ffprobe,
        "-v",
        "error",
        "-show_entries",
        "format=duration",
        "-of",
        "default=noprint_wrappers=1:nokey=1",
        path,
    ]
    try:
        result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
        duration = float(result.stdout.strip())
        logger.debug("Media duration: %s = %.3fs", path, duration)
        return duration
    except Exception as exc:
        logger.warning("Failed to probe media duration: %s (%s)", path, exc)
        return None
 
 
def _resolution_from_ratio(video_ratio: str) -> tuple[int, int]:
    ratio = (video_ratio or "9:16").strip()
    if ratio in {"16:9", "landscape"}:
        return 1920, 1080
    if ratio in {"1:1", "square"}:
        return 1080, 1080
    return 1080, 1920
 
 
def _ass_time(seconds: float) -> str:
    total_centiseconds = max(0, int(round(seconds * 100)))
    centiseconds = total_centiseconds % 100
    total_seconds = total_centiseconds // 100
    hours = total_seconds // 3600
    minutes = (total_seconds % 3600) // 60
    secs = total_seconds % 60
    return f"{hours}:{minutes:02d}:{secs:02d}.{centiseconds:02d}"
 
 
def _ass_text(text: str) -> str:
    cleaned = str(text or "").strip()
    cleaned = cleaned.replace("{", "{").replace("}", "}")
    return cleaned.replace("\r\n", "\\N").replace("\n", "\\N")
 
 
def write_ass_subtitles(
    path: str,
    *,
    subtitles: Iterable[tuple[str, float]],
    title: Optional[str] = None,
    video_ratio: str = "9:16",
) -> str:
    width, height = _resolution_from_ratio(video_ratio)
    title_size = max(36, int(height * 0.038))
    subtitle_size = max(34, int(height * 0.032))
    title_margin_v = max(70, int(height * 0.06))
    subtitle_margin_v = max(80, int(height * 0.085))
    subtitle_margin_h = max(70, int(width * 0.07))
 
    events: list[str] = []
    cursor = 0.0
    normalized: list[tuple[str, float, float]] = []
    for text, duration in subtitles:
        duration_seconds = max(0.1, float(duration or 0))
        start = cursor
        end = cursor + duration_seconds
        normalized.append((text, start, end))
        cursor = end
 
    total_duration = cursor or 0.1
    if title:
        events.append(
            f"Dialogue: 0,{_ass_time(0)},{_ass_time(total_duration)},Title,,0,0,0,,{_ass_text(title)}"
        )
    for text, start, end in normalized:
        events.append(
            f"Dialogue: 0,{_ass_time(start)},{_ass_time(end)},Subtitle,,0,0,0,,{_ass_text(text)}"
        )
 
    content = "\n".join(
        [
            "[Script Info]",
            "ScriptType: v4.00+",
            "Collisions: Normal",
            f"PlayResX: {width}",
            f"PlayResY: {height}",
            "WrapStyle: 0",
            "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",
            f"Style: Title,Arial,{title_size},&H00FFFFFF,&H000000FF,&H99000000,&H66000000,-1,0,0,0,100,100,0,0,1,4,0,8,{subtitle_margin_h},{subtitle_margin_h},{title_margin_v},1",
            f"Style: Subtitle,Arial,{subtitle_size},&H00FFFFFF,&H000000FF,&HAA000000,&H66000000,0,0,0,0,100,100,0,0,1,4,0,2,{subtitle_margin_h},{subtitle_margin_h},{subtitle_margin_v},1",
            "",
            "[Events]",
            "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text",
            *events,
            "",
        ]
    )
    logger.info("Writing ASS subtitles: %s lines=%d title=%s", path, len(normalized), bool(title))
    return write_text(path, content)
 
 
def _ffmpeg_filter_path(path: str) -> str:
    escaped = os.path.abspath(path).replace("\\", "/")
    return escaped.replace(":", "\\:").replace("'", "\\'")
 
 
def _ffmpeg_has_filter(ffmpeg: str, filter_name: str) -> bool:
    try:
        result = subprocess.run(
            [ffmpeg, "-hide_banner", "-filters"],
            check=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True,
        )
    except Exception as exc:
        logger.warning("Failed to inspect ffmpeg filters: %s", exc)
        return False
    return any(line.split()[1:2] == [filter_name] for line in result.stdout.splitlines() if line.strip())
 
 
def burn_ass_subtitles(video_path: str, subtitle_path: str, output_path: str) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to burn subtitles into video.")
    if not _ffmpeg_has_filter(ffmpeg, "ass"):
        raise RuntimeError(
            "ffmpeg was found, but its libass/ass filter is unavailable. "
            "Install an ffmpeg build with libass support before burning ASS subtitles."
        )
    if not os.path.exists(video_path):
        raise FileNotFoundError(f"Video not found: {video_path}")
    if not os.path.exists(subtitle_path):
        raise FileNotFoundError(f"Subtitle not found: {subtitle_path}")
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    logger.info("Burning ASS subtitles: video=%s subtitles=%s -> %s", video_path, subtitle_path, output_path)
    cmd = [
        ffmpeg,
        "-y",
        "-i",
        video_path,
        "-vf",
        f"ass=filename='{_ffmpeg_filter_path(subtitle_path)}'",
        "-c:v",
        "libx264",
        "-crf",
        "18",
        "-preset",
        "veryfast",
        "-c:a",
        "copy",
        output_path,
    ]
    try:
        subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    except subprocess.CalledProcessError as exc:
        logger.error("Failed to burn ASS subtitles: %s", exc.stderr)
        raise
    return output_path
 
 
def speed_audio_to_duration(audio_path: str, output_path: str, target_seconds: int) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to speed up long digital-human narration audio.")
 
    duration = media_duration_seconds(audio_path)
    if not duration:
        return audio_path
    if duration <= target_seconds:
        return audio_path
 
    speed = duration / float(target_seconds)
    filters = []
    remaining = speed
    while remaining > 2.0:
        filters.append("atempo=2.0")
        remaining /= 2.0
    filters.append(f"atempo={remaining:.6f}")
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    logger.info(
        "Speeding audio to fit video duration: %s duration=%.2fs target=%ss speed=%.3fx -> %s",
        audio_path,
        duration,
        target_seconds,
        speed,
        output_path,
    )
    cmd = [
        ffmpeg,
        "-y",
        "-i",
        audio_path,
        "-filter:a",
        ",".join(filters),
        "-vn",
        "-acodec",
        "libmp3lame",
        output_path,
    ]
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_path
 
 
def extract_last_frame(video_path: str, output_path: str) -> str:
    ffmpeg = shutil.which("ffmpeg")
    if not ffmpeg:
        raise RuntimeError("ffmpeg is required to extract the previous video tail frame.")
 
    os.makedirs(os.path.dirname(output_path), exist_ok=True)
    logger.info("Extracting tail frame: %s -> %s", video_path, output_path)
    cmd = [
        ffmpeg,
        "-y",
        "-sseof",
        "-0.1",
        "-i",
        video_path,
        "-frames:v",
        "1",
        "-q:v",
        "2",
        output_path,
    ]
    subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    return output_path