From b8773b8fc230bbb758c4754c95f308628e0116e9 Mon Sep 17 00:00:00 2001 From: Lap Tran Date: Sun, 25 Oct 2026 16:02:44 -0400 Subject: [PATCH] feat(patches): vendor reclip transcript+history patch (base 1d161d1) Full patched app.py/index.html plus rebaseable diff. Deployed via bind-mounts from the share (documents/reclip/); this repo is the versioned source of truth so container/image updates and new machines cannot lose the patch. Rebase instructions in patches/reclip/README.md. --- patches/reclip/README.md | 44 ++ patches/reclip/app.py | 362 ++++++++++++ patches/reclip/index.html | 784 +++++++++++++++++++++++++ patches/reclip/reclip-transcript.patch | 353 +++++++++++ 4 files changed, 1543 insertions(+) create mode 100644 patches/reclip/README.md create mode 100644 patches/reclip/app.py create mode 100644 patches/reclip/index.html create mode 100644 patches/reclip/reclip-transcript.patch diff --git a/patches/reclip/README.md b/patches/reclip/README.md new file mode 100644 index 0000000..7d11934 --- /dev/null +++ b/patches/reclip/README.md @@ -0,0 +1,44 @@ +# Patched reclip — source of truth + +Our transcription + history additions to [averygan/reclip](https://github.com/averygan/reclip), +kept here so container updates and new machines can't lose them. + +- `app.py`, `index.html` — the full patched files (deployed via bind-mounts + from `documents/reclip/` on the Unraid share; see that dir's DEPLOY.md) +- `reclip-transcript.patch` — the same delta as a diff against upstream, + for rebasing onto future upstream versions + +Patch base: upstream `1d161d1` (cloned 2026-10-25). + +## What the patch adds + +1. `fetch_transcript()` — after each download, pull yt-dlp captions + (YouTube subs; X auto-captions) → `.txt` sidecar, never fails the job +2. `/api/transcript/` + Transcript button + auto-save in the UI +3. Persisted download history (`history.json` in the downloads volume) + + `/api/history` + History section with a live transcript-available flag + (manual `yt-transcribe` sidecars are detected at read time) +4. `/api/file` + `/api/transcript` fall back to history, so old links + survive restarts + +## Updating reclip (when upstream changes) + + git clone --depth 1 https://github.com/averygan/reclip.git /tmp/reclip-new + cd /tmp/reclip-new + git apply --3way ~/Documents/dotfiles/patches/reclip/reclip-transcript.patch + +Resolve any conflicts (the additions are mostly self-contained: one new +function, two routes, one status field, UI button + History section), then +copy the updated `app.py` + `templates/index.html`: + +- into this directory (commit — new source of truth) +- to the share: `/Volumes/data/documents/reclip/{app.py,index.html}` +- restart the reclip container (gunicorn reloads at start; the entrypoint + also pip-updates yt-dlp at every container start) + +If `git apply` fails on a drifted file, the fastest path is re-porting by +hand from the conflict markers — the four additions are small and greppable +(`fetch_transcript`, `history.json`, `resolve_download`, `saveTranscript`). + +If an image update happens WITHOUT re-porting: the old patched files keep +working (bind-mounts win), you just may miss upstream improvements. diff --git a/patches/reclip/app.py b/patches/reclip/app.py new file mode 100644 index 0000000..3c3c9c8 --- /dev/null +++ b/patches/reclip/app.py @@ -0,0 +1,362 @@ +import os +import uuid +import glob +import json +import re +import subprocess +import threading +import time +from flask import Flask, request, jsonify, send_file, render_template + +app = Flask(__name__) +DOWNLOAD_DIR = os.path.join(os.path.dirname(__file__), "downloads") +os.makedirs(DOWNLOAD_DIR, exist_ok=True) + +jobs = {} + +HISTORY_FILE = os.path.join(DOWNLOAD_DIR, "history.json") +_history_lock = threading.Lock() + + +def load_history(): + try: + with open(HISTORY_FILE, encoding="utf-8") as fh: + data = json.load(fh) + return data if isinstance(data, list) else [] + except Exception: + return [] + + +def save_history(history): + try: + tmp = HISTORY_FILE + ".tmp" + with open(tmp, "w", encoding="utf-8") as fh: + json.dump(history, fh, indent=2) + os.replace(tmp, HISTORY_FILE) + except OSError: + pass + + +def sidecar_transcript(file_path): + """Manual-fallback transcripts (yt-transcribe ) live beside the + media as .txt — recognize them at read time so history stays + truthful without re-downloading anything.""" + if not file_path: + return None + stem = os.path.splitext(file_path)[0] + ".txt" + return stem if os.path.exists(stem) else None + + +def record_history(job): + """Persist the completed job so history + file/transcript downloads + survive process restarts (downloads dir is the mapped volume).""" + with _history_lock: + history = [h for h in load_history() if h.get("job_id") != job.get("job_id")] + history.insert(0, { + "job_id": job.get("job_id"), + "url": job.get("url", ""), + "title": job.get("title", ""), + "filename": job.get("filename", ""), + "file": job.get("file", ""), + "transcript": job.get("transcript") or "", + "transcript_name": job.get("transcript_name", ""), + "has_transcript": bool(job.get("transcript")), + "size": os.path.getsize(job["file"]) if job.get("file") and os.path.exists(job["file"]) else 0, + "date": time.strftime("%Y-%m-%d %H:%M:%S"), + }) + save_history(history[:200]) + + +def parse_ytdlp_json(stdout): + """Parse yt-dlp JSON output. + + With ``-j`` yt-dlp prints one JSON object per line. Some extractors + emit multiple videos even with ``--no-playlist``, so stdout contains + several objects and a plain ``json.loads`` raises "Extra data". + Return the first valid object. + """ + for line in stdout.splitlines(): + line = line.strip() + if not line: + continue + return json.loads(line) + raise ValueError("yt-dlp returned no data") + + +def fetch_transcript(url, chosen_path): + """Best-effort transcript via yt-dlp captions (manual or auto-generated; + includes X's auto-captions). Writes