| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | import struct, sys |
| 9 | from PIL import Image, ImageDraw, ImageFilter, ImageFont |
| 10 | |
| 11 | W, H = 720, 576 |
| 12 | CUES = [ |
| 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 | ] |
| 18 | try: |
| 19 | FONT = ImageFont.load_default(size=36) |
| 20 | except TypeError: |
| 21 | FONT = ImageFont.load_default() |
| 22 | |
| 23 | |
| 24 | def seg(kind, pts, body): |
| 25 | return b"PG" + struct.pack(">IIBH", int(pts * 90000), 0, kind, len(body)) + body |
| 26 | |
| 27 | |
| 28 | def 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 | |
| 48 | def 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 |
| 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 | |
| 68 | def palette(): |
| 69 | ents = [(0, 16, 128, 128, 0)] |
| 70 | for i in range(1, 9): |
| 71 | ents.append((i, 235, 128, 128, 255 if i > 4 else 128)) |
| 72 | for i in range(9, 17): |
| 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 | |
| 77 | out = bytearray() |
| 78 | num = 0 |
| 79 | for 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 |
| 93 | open(sys.argv[1], "wb").write(out) |