Python
"""Shared helpers: paths, tool lookup, yt-dlp and ffmpeg wrappers."""
import hashlib, json, os, re, shutil, subprocess, sys
from pathlib import Path
ROOT = Path(os.environ.get("CLIP_DESK_HOME", Path.cwd()))
JOBS = ROOT / "jobs"
def tool(name):
p = shutil.which(name)
if not p:
sys.exit(f"{name} not found. Run the setup skill first (it installs ffmpeg and yt-dlp).")
return p
def run(cmd, check=True, capture=False):
r = subprocess.run(cmd, check=False, text=True, encoding="utf-8", errors="replace",
stdout=subprocess.PIPE if capture else None, stderr=subprocess.PIPE if capture else None)
if check and r.returncode != 0:
msg = (r.stderr or "")[-1500:] if capture else ""
sys.exit(f"command failed ({r.returncode}): {' '.join(map(str, cmd))[:200]}\n{msg}")
return r
def ytdlp_base():
import importlib.util
exe = [sys.executable, "-m", "yt_dlp"] if importlib.util.find_spec("yt_dlp") else [tool("yt-dlp")]
base = exe + ["--no-warnings", "--no-progress"]
if sys.platform == "win32":
base += ["--compat-options", "no-certifi"] # use the Windows certificate store
return base
def ytdlp_json(url, extra=()):
r = run(ytdlp_base() + ["-J", *extra, url], capture=True)
return json.loads(r.stdout)
def is_url(s):
return bool(re.match(r"https?://", s))
def job_dir(src):
key = hashlib.sha1(src.encode()).hexdigest()[:8]
stem = re.sub(r"[^a-z0-9]+", "-", Path(src).stem.lower() if not is_url(src) else src.split("v=")[-1].split("/")[-1].lower())[:24].strip("-")
d = JOBS / f"{stem or 'job'}-{key}"
d.mkdir(parents=True, exist_ok=True)
return d
def ffprobe(path):
r = run([tool("ffprobe"), "-v", "error", "-show_entries", "format=duration:stream=codec_type,width,height,r_frame_rate",
"-of", "json", str(path)], capture=True)
return json.loads(r.stdout)
def save_json(path, obj):
Path(path).write_text(json.dumps(obj, ensure_ascii=False, indent=1), encoding="utf-8")
def load_json(path):
return json.loads(Path(path).read_text(encoding="utf-8"))