132 lines
3.9 KiB
Python
132 lines
3.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Validate YT deliverables in a batch root (SRT / cover / titles).
|
|
|
|
Usage:
|
|
python validate_deliverables.py
|
|
python validate_deliverables.py "D:\\MyBatch"
|
|
|
|
Exit code 0 = all OK; 1 = problems found.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
|
|
def to_sec(ts: str) -> float:
|
|
h, m, rest = ts.split(":")
|
|
s, ms = rest.split(",")
|
|
return int(h) * 3600 + int(m) * 60 + int(s) + int(ms) / 1000
|
|
|
|
|
|
def check_srt(path: Path) -> list[str]:
|
|
errs = []
|
|
text = path.read_text(encoding="utf-8", errors="replace")
|
|
if not text.strip():
|
|
return ["SRT empty"]
|
|
blocks = re.split(r"\n\s*\n", text.strip())
|
|
prev_end = None
|
|
cues = 0
|
|
for b in blocks:
|
|
lines = [ln for ln in b.splitlines() if ln.strip() != ""]
|
|
if len(lines) < 3:
|
|
continue
|
|
cues += 1
|
|
m = re.match(r"(\d{2}:\d{2}:\d{2},\d{3})\s*-->\s*(\d{2}:\d{2}:\d{2},\d{3})", lines[1])
|
|
if not m:
|
|
errs.append(f"bad timing line: {lines[1][:40]}")
|
|
continue
|
|
start, end = to_sec(m.group(1)), to_sec(m.group(2))
|
|
if end <= start:
|
|
errs.append(f"end<=start cue#{cues}")
|
|
if prev_end is not None and start + 1e-6 < prev_end:
|
|
errs.append(f"overlap before cue#{cues}")
|
|
prev_end = end
|
|
body = lines[2:]
|
|
if len(body) != 1:
|
|
errs.append(f"multiline cue#{cues} lines={len(body)}")
|
|
elif "\n" in body[0]:
|
|
errs.append(f"newline in cue#{cues}")
|
|
if cues < 8:
|
|
errs.append(f"too few cues ({cues}) — consider denser narration")
|
|
return errs
|
|
|
|
|
|
def check_cover(path: Path) -> list[str]:
|
|
errs = []
|
|
try:
|
|
im = Image.open(path)
|
|
w, h = im.size
|
|
if (w, h) != (1280, 720):
|
|
errs.append(f"cover size {w}x{h} (want 1280x720)")
|
|
except Exception as e:
|
|
errs.append(f"cover unreadable: {e}")
|
|
# filename-side heuristic only; OCR optional / skipped
|
|
return errs
|
|
|
|
|
|
def check_titles(path: Path) -> list[str]:
|
|
errs = []
|
|
t = path.read_text(encoding="utf-8", errors="replace")
|
|
if "YOUTUBE TITLES" not in t.upper() and "=== YOUTUBE TITLES" not in t:
|
|
errs.append("titles missing TITLES section")
|
|
if t.count("\n") < 8:
|
|
errs.append("titles file looks too short")
|
|
# meta words should not appear as title copy
|
|
up = t.upper()
|
|
for bad in ("FACE + FULL BODY", "CHINA GIRL"):
|
|
if bad in up:
|
|
errs.append(f"titles contain forbidden meta: {bad}")
|
|
return errs
|
|
|
|
|
|
def main() -> None:
|
|
root = Path(sys.argv[1]).resolve() if len(sys.argv) > 1 else Path.cwd().resolve()
|
|
folders = [
|
|
d
|
|
for d in sorted(root.iterdir(), key=lambda p: p.name.lower())
|
|
if d.is_dir() and not d.name.startswith("_") and not d.name.startswith(".")
|
|
and list(d.glob("*.mp4"))
|
|
]
|
|
if not folders:
|
|
print("No media folders found under", root)
|
|
sys.exit(1)
|
|
|
|
bad = 0
|
|
print(f"ROOT {root}")
|
|
for d in folders:
|
|
issues = []
|
|
srt = d / "YT_CAPTIONS_EN.srt"
|
|
cover = d / "YT_COVER.jpg"
|
|
titles = d / "YT_TITLES.txt"
|
|
if not srt.exists():
|
|
issues.append("missing YT_CAPTIONS_EN.srt")
|
|
else:
|
|
issues += [f"SRT: {e}" for e in check_srt(srt)]
|
|
if not cover.exists():
|
|
issues.append("missing YT_COVER.jpg")
|
|
else:
|
|
issues += [f"COVER: {e}" for e in check_cover(cover)]
|
|
if not titles.exists():
|
|
issues.append("missing YT_TITLES.txt")
|
|
else:
|
|
issues += [f"TITLES: {e}" for e in check_titles(titles)]
|
|
|
|
if issues:
|
|
bad += 1
|
|
print(f"FAIL {d.name}")
|
|
for e in issues:
|
|
print(f" - {e}")
|
|
else:
|
|
print(f"OK {d.name}")
|
|
|
|
print(f"\n{len(folders) - bad}/{len(folders)} folders OK")
|
|
sys.exit(1 if bad else 0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|