File size: 2,636 Bytes
fa2d87b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
from __future__ import annotations

import argparse
from contextlib import contextmanager
import html
import os
import subprocess
from pathlib import Path
from typing import Iterator
import uuid


@contextmanager
def atomic_media_output(output: Path) -> Iterator[Path]:
    output.parent.mkdir(parents=True, exist_ok=True)
    partial = output.with_name(
        f"{output.stem}.partial-{os.getpid()}-{uuid.uuid4().hex}{output.suffix}"
    )
    try:
        yield partial
        with partial.open("rb") as handle:
            os.fsync(handle.fileno())
        os.replace(partial, output)
    finally:
        partial.unlink(missing_ok=True)


def hevc_command(source: Path, output: Path) -> list[str]:
    return [
        "ffmpeg",
        "-y",
        "-i",
        str(source),
        "-map",
        "0:v:0",
        "-map",
        "0:a:0?",
        "-c:v",
        "libx265",
        "-tag:v",
        "hvc1",
        "-pix_fmt",
        "yuv420p",
        "-preset",
        "medium",
        "-crf",
        "24",
        "-c:a",
        "copy",
        "-movflags",
        "+faststart",
        str(output),
    ]


def h264_command(source: Path, output: Path) -> list[str]:
    return [
        "ffmpeg",
        "-y",
        "-i",
        str(source),
        "-map",
        "0:v:0",
        "-map",
        "0:a:0?",
        "-c:v",
        "libx264",
        "-pix_fmt",
        "yuv420p",
        "-preset",
        "medium",
        "-crf",
        "20",
        "-c:a",
        "copy",
        "-movflags",
        "+faststart",
        str(output),
    ]


def embedded_video_html(*, repo_id: str, stem: str, title: str) -> str:
    base = f"https://huggingface.co/{repo_id}/resolve/main/examples"
    label = html.escape(title, quote=True)
    hevc = html.escape(f"{base}/hevc/{stem}.mp4", quote=True)
    h264 = html.escape(f"{base}/h264/{stem}.mp4", quote=True)
    return (
        f'<video controls playsinline preload="metadata" width="608" aria-label="{label}">\n'
        f'  <source src="{hevc}" type="video/mp4; codecs=&quot;hvc1&quot;">\n'
        f'  <source src="{h264}" type="video/mp4">\n'
        f'  <a href="{h264}">{label} MP4</a>\n'
        "</video>"
    )


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--source", type=Path, required=True)
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args()
    with atomic_media_output(args.output) as partial:
        subprocess.run(hevc_command(args.source, partial), check=True)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())