forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiBca831ceTahinli6 days ago3966 commits

gen-bitmap-sub-fixtures.py93 lines, 3.4 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Writes a small synthetic PGS .sup (720x576, four cues with known times) to argv[1].
3
4It is the *source* the DVD/DVB subtitle fixtures are encoded from with ffmpeg
5(scripts/gen-bitmap-sub-fixtures.sh): ffmpeg's dvdsub and dvbsub encoders take bitmaps only.
6No third-party media is involved; the text is rendered with PIL's default font.
7"""
8import struct, sys
9from PIL import Image, ImageDraw, ImageFilter, ImageFont
10
11W, H = 720, 576
12CUES = [ # start s, end s, text lines, y
13 (0.5, 1.5, ["Hello DVD"], 480),
14 (2.0, 3.2, ["Second cue", "line two"], 440),
15 (3.5, 4.5, ["Third — éèü"], 60),
16 (5.0, 6.0, ["End"], 500),
17]
18try:
19 FONT = ImageFont.load_default(size=36)
20except TypeError:
21 FONT = ImageFont.load_default()
22
23
24def seg(kind, pts, body):
25 return b"PG" + struct.pack(">IIBH", int(pts * 90000), 0, kind, len(body)) + body
26
27
28def rle(rows):
29 out = bytearray()
30 for row in rows:
31 x = 0
32 while x < len(row):
33 c = row[x]
34 n = 1
35 while x + n < len(row) and row[x + n] == c and n < 16383:
36 n += 1
37 if c != 0 and n < 3:
38 out += bytes([c]) * n
39 elif c == 0:
40 out += bytes([0, 0x40 | (n >> 8), n & 255]) if n > 63 else bytes([0, n])
41 else:
42 out += bytes([0, 0xC0 | (n >> 8), n & 255, c]) if n > 63 else bytes([0, 0x80 | n, c])
43 x += n
44 out += b"\0\0"
45 return bytes(out)
46
47
48def render(lines):
49 d = ImageDraw.Draw(Image.new("L", (1, 1)))
50 ws = [d.textbbox((0, 0), t, font=FONT)[2] for t in lines]
51 w, h = max(ws) + 12, 44 * len(lines) + 8
52 fg = Image.new("L", (w, h), 0)
53 dr = ImageDraw.Draw(fg)
54 for i, t in enumerate(lines):
55 dr.text(((w - ws[i]) // 2, 4 + 44 * i), t, fill=255, font=FONT)
56 ol = fg.filter(ImageFilter.MaxFilter(5)).filter(ImageFilter.GaussianBlur(0.7))
57 px = []
58 for y in range(h):
59 row = []
60 for x in range(w):
61 f, o = fg.getpixel((x, y)) // 32, ol.getpixel((x, y)) // 32 # 0..7
62 row.append(1 + f if f >= 4 else (9 + o if o else 0)) if False else row.append(
63 (1 + f) if f >= 3 else ((9 + o) if o else 0))
64 px.append(row)
65 return w, h, px
66
67
68def palette():
69 ents = [(0, 16, 128, 128, 0)]
70 for i in range(1, 9): # white ramp, indices 1..8
71 ents.append((i, 235, 128, 128, 255 if i > 4 else 128))
72 for i in range(9, 17): # black outline, indices 9..16
73 ents.append((i, 16, 128, 128, min(255, 40 * (i - 8))))
74 return b"".join(struct.pack("BBBBB", i, y, cr, cb, a) for i, y, cr, cb, a in ents)
75
76
77out = bytearray()
78num = 0
79for start, end, lines, y in CUES:
80 w, h, px = render(lines)
81 x = (W - w) // 2
82 data = rle(px)
83 pcs = struct.pack(">HHBHBBBB", W, H, 0x10, num, 0x80, 0, 0, 1) + struct.pack(">HBBHH", 0, 0, 0, x, y)
84 wds = struct.pack(">BBHHHH", 1, 0, x, y, w, h)
85 pds = bytes([0, 0]) + palette()
86 ods = struct.pack(">HBB", 0, 0, 0xC0) + (len(data) + 4).to_bytes(3, "big") + struct.pack(">HH", w, h) + data
87 for k, b in ((0x16, pcs), (0x17, wds), (0x14, pds), (0x15, ods), (0x80, b"")):
88 out += seg(k, start, b)
89 num += 1
90 pcs = struct.pack(">HHBHBBBB", W, H, 0x10, num, 0x00, 0, 0, 0)
91 out += seg(0x16, end, pcs) + seg(0x17, end, struct.pack(">BBHHHH", 1, 0, x, y, w, h)) + seg(0x80, end, b"")
92 num += 1
93open(sys.argv[1], "wb").write(out)