#!/usr/bin/env python3 """Lee un .ts con SEI GA94 y muestra el texto 608 del campo pedido (1=CC1, 2=CC3) y los bytes de texto del 708 por servicio. Solo para verificar la salida.""" import sys from orbisubs_srt import PAC_FILA, partir, pts_de # Fila de cada PAC (082): para ver dónde quedó cada subtítulo. PAC_INV = {v: k for k, v in PAC_FILA.items()} EXT = {0x12: "ÁÉÓÚÜü‘¡*’—©℠•“”ÀÂÇÈÊËëÎÏïÔÙùÛ«»", 0x13: "ÃãÍÌìÒòÕõ{}\\^_|~ÄäÖöߥ¤│Å娸┌┐└┘"} ESP = "®°½¿™¢£♪à èâêîôû" BAS = {0x2A: "á", 0x5C: "é", 0x5E: "í", 0x5F: "ó", 0x60: "ú", 0x7B: "ç", 0x7C: "÷", 0x7D: "Ñ", 0x7E: "ñ"} ts, campo = open(sys.argv[1], "rb").read(), int(sys.argv[2]) vpid, aus, cur = 256, [], None for i in range(0, len(ts) - 187, 188): pid, pusi, af, pay = partir(ts[i:i + 188]) if pid != vpid: continue if pusi: cur = bytearray(pay); aus.append(cur) elif cur is not None: cur += pay frames = [] for pes in aus: pts = pts_de(bytes(pes)); j = pes.find(b"GA94") if j < 0: continue n = pes[j + 5] & 0x1F tri = [pes[j + 7 + 3 * k: j + 10 + 3 * k] for k in range(n)] frames.append((pts, tri)) frames.sort() buf, txt, prev, filas = "", [], None, set() d708 = {1: bytearray(), 2: bytearray()} pkt = bytearray() for pts, tri in frames: for t in tri: typ = t[0] & 3 if typ == campo - 1 and t[0] & 4: a, b = t[1] & 0x7F, t[2] & 0x7F if (a, b) == prev and a < 0x20: continue prev = (a, b) if 0x10 <= a <= 0x17 and 0x40 <= b <= 0x7F: f = PAC_INV.get((a, 0x40 if b < 0x60 else 0x60)) if f: filas.add(f) buf += " " elif a in (0x14, 0x15) and b == 0x2F: fs = "-".join(str(x) for x in sorted(filas)) txt.append(f"{pts/90000:9.2f} [filas {fs}] {buf.strip()}"); buf = ""; filas = set() elif a in (0x14, 0x15): pass elif a == 0x11 and 0x30 <= b <= 0x3F: buf += ESP[b - 0x30] elif a in (0x12, 0x13) and 0x20 <= b <= 0x3F: buf = buf[:-1] + EXT[a][b - 0x20] elif 0x10 <= a <= 0x17: buf += " " else: for c in (a, b): if c >= 0x20: buf += BAS.get(c, chr(c)) elif typ in (2, 3) and t[0] & 4: if typ == 3 and pkt: k = 1 while k < len(pkt): svc, sz = pkt[k] >> 5, pkt[k] & 31 if svc == 0: break d708.setdefault(svc, bytearray()).extend(pkt[k + 1:k + 1 + sz]); k += 1 + sz pkt = bytearray() pkt += t[1:3] for l in txt[:12]: print(l) ANCLA = {0: "arriba-izq", 1: "arriba-centro", 2: "arriba-der", 3: "medio-izq", 4: "centro", 5: "medio-der", 6: "abajo-izq", 7: "abajo-centro", 8: "abajo-der"} for s in (1, 2): b = d708[s] # DefineWindow (0x98-0x9F + 6 bytes): anclaje y posición vertical/horizontal (%). ventanas = set() for i, c in enumerate(b): if 0x98 <= c <= 0x9F and i + 6 < len(b): p = b[i + 1:i + 7] ventanas.add((ANCLA.get(p[3] >> 4, p[3] >> 4), p[1] & 0x7F, p[2])) print(f"708 svc{s} ventanas (anclaje, vertical %, horizontal %):", sorted(ventanas)) print(f"708 svc{s}:", "".join(chr(c) if 0x20 <= c < 0x7F or 0xA0 <= c else "|" for c in b)[:400])