Repository Public
1import { execFile, spawn, type ChildProcess } from "node:child_process";2import fs from "node:fs";3import os from "node:os";4import path from "node:path";5import { all, bumpTitles, getNum, log, now, run } from "./db";6import { env } from "./env";7import { enrichment, setMedia, upsertEpisode, upsertTitle } from "./vod";89// Personal library: folders on this machine or on a mounted NAS share. Files are only ever read by this process and10// served to authenticated sessions by media id — a path never appears in a URL and nothing outside a declared folder11// can be reached.1213export const VIDEO_EXT = /\.(mp4|m4v|mov|mkv|webm|avi|ts|m2ts|mpg|mpeg|wmv|flv)$/i;14const BIN = ["/opt/homebrew/bin", "/usr/local/bin", "/usr/bin"];15const find = (name: string) => BIN.map((d) => path.join(/*turbopackIgnore: true*/ d, name)).find((p) => fs.existsSync(p)) ?? null;16export const ffmpegPath = () => find("ffmpeg");17export const ffprobePath = () => find("ffprobe");1819/** Roots under which a library folder may be declared. */20export function libraryRoots(): string[] {21 const home = os.homedir();22 return env.libraryRoots.length ? env.libraryRoots : [path.join(home, "media"), path.join(home, "nas_clustr"), path.join(home, "nas2_clustr"), "/Volumes"];23}24const inside = (child: string, root: string) => child === root || child.startsWith(root.endsWith(path.sep) ? root : root + path.sep);2526/** Resolves and validates a folder: absolute, existing, a directory, inside an allowed root (after resolving symlinks), no hidden component. */27export function checkDir(raw: string): { ok: true; real: string } | { ok: false; error: string } {28 const p = raw.trim().replace(/^~(?=\/|$)/, os.homedir());29 if (!path.isAbsolute(p)) return { ok: false, error: "Le chemin doit être absolu" };30 if (p.split(path.sep).some((s) => s.startsWith(".") && s.length > 1)) return { ok: false, error: "Les dossiers cachés ne peuvent pas être indexés" };31 let real: string;32 try { real = fs.realpathSync(p); } catch { return { ok: false, error: "Dossier introuvable ou partage non monté" }; }33 try { if (!fs.statSync(real).isDirectory()) return { ok: false, error: "Ce chemin n'est pas un dossier" }; } catch { return { ok: false, error: "Dossier illisible" }; }34 const roots = libraryRoots().map((r) => { try { return fs.realpathSync(r); } catch { return r; } });35 if (!roots.some((r) => inside(real, r))) return { ok: false, error: `Dossier hors des emplacements autorisés (${libraryRoots().join(", ")})` };36 return { ok: true, real };37}3839// --- probing --------------------------------------------------------------------------------------------------------40const exec = (bin: string, args: string[], timeoutMs: number) => new Promise<string>((resolve, reject) =>41 execFile(bin, args, { timeout: timeoutMs, maxBuffer: 16 * 1024 * 1024 }, (err, out, errOut) => (err ? reject(new Error((errOut || err.message).toString().slice(-400))) : resolve(out.toString()))));4243export interface Probe {44 container: string; duration: number | null; size: number | null; vcodec: string | null; vtag: string | null; width: number | null; height: number | null; pixfmt: string | null;45 acodec: string | null; audio: { index: number; codec: string; lang: string; label: string; channels: number }[]; subs: { index: number; codec: string; lang: string; label: string; text: boolean }[];46}47const LANG3: Record<string, string> = { fre: "fr", fra: "fr", eng: "en", spa: "es", ger: "de", deu: "de", ita: "it", por: "pt", jpn: "ja", rus: "ru", chi: "zh", zho: "zh", ara: "ar", dut: "nl", nld: "nl" };48const LABEL: Record<string, string> = { fr: "Français", en: "English", es: "Español", de: "Deutsch", it: "Italiano", pt: "Português", ja: "日本語", ru: "Русский", zh: "中文", ar: "العربية", nl: "Nederlands" };49const lang = (t: string | undefined) => { const l = (t ?? "").toLowerCase(); return LANG3[l] ?? (l.length === 2 ? l : "und"); };5051export async function probe(file: string): Promise<Probe> {52 const bin = ffprobePath(); if (!bin) throw new Error("ffprobe n'est pas installé sur ce nœud");53 const j = JSON.parse(await exec(bin, ["-v", "error", "-print_format", "json", "-show_format", "-show_streams", file], 60_000)) as {54 format?: { format_name?: string; duration?: string; size?: string }; streams?: { index: number; codec_type: string; codec_name?: string; codec_tag_string?: string; width?: number; height?: number; pix_fmt?: string; channels?: number; tags?: { language?: string; title?: string }; disposition?: { attached_pic?: number } }[];55 };56 const st = j.streams ?? [];57 const v = st.find((s) => s.codec_type === "video" && !s.disposition?.attached_pic);58 const a = st.filter((s) => s.codec_type === "audio");59 const ext = path.extname(file).slice(1).toLowerCase();60 return {61 container: ext === "m4v" || ext === "mov" ? "mp4" : ext, duration: j.format?.duration ? Math.round(Number(j.format.duration)) : null, size: j.format?.size ? Number(j.format.size) : null,62 vcodec: v?.codec_name ?? null, vtag: v?.codec_tag_string ?? null, width: v?.width ?? null, height: v?.height ?? null, pixfmt: v?.pix_fmt ?? null, acodec: a[0]?.codec_name ?? null,63 audio: a.map((s, i) => { const l = lang(s.tags?.language); return { index: i, codec: s.codec_name ?? "?", lang: l, channels: s.channels ?? 2, label: s.tags?.title || LABEL[l] || `Piste ${i + 1}` }; }),64 subs: st.filter((s) => s.codec_type === "subtitle").map((s, i) => { const l = lang(s.tags?.language); return { index: i, codec: s.codec_name ?? "?", lang: l, label: s.tags?.title || LABEL[l] || `Sous-titres ${i + 1}`, text: ["subrip", "ass", "ssa", "mov_text", "webvtt", "text"].includes(s.codec_name ?? "") }; }),65 };66}6768/** How a file can reach a browser: as is, or through an HLS conversion (stream copy when the codecs are fine, VideoToolbox encode otherwise). */69export function playbackMode(p: Probe): "direct" | "direct-hevc" | "remux" | "transcode" | "audio-only" {70 if (!p.vcodec) return "audio-only";71 const aOk = !p.acodec || ["aac", "mp3"].includes(p.acodec);72 const eight = !p.pixfmt || !/10|12/.test(p.pixfmt);73 if (p.container === "mp4" && p.vcodec === "h264" && eight && aOk) return "direct";74 if (p.container === "webm" && ["vp8", "vp9", "av1"].includes(p.vcodec) && (!p.acodec || ["opus", "vorbis"].includes(p.acodec))) return "direct";75 if (p.container === "mp4" && p.vcodec === "hevc" && p.vtag === "hvc1" && aOk) return "direct-hevc";76 if (p.vcodec === "h264" && eight) return "remux";77 return "transcode";78}7980// --- naming ---------------------------------------------------------------------------------------------------------81const JUNK = /\b(2160p|1080p|720p|480p|4k|uhd|hdr10?|dv|bluray|blu-ray|brrip|bdrip|web-?dl|webrip|hdtv|dvdrip|x26[45]|h\.?26[45]|hevc|avc|aac|ac3|dts|truehd|atmos|multi|vff|vfq|vostfr|french|english|remux|proper|repack|extended|unrated|10bit)\b.*$/i;82const tidy = (s: string) => s.replace(/[._]+/g, " ").replace(/\s{2,}/g, " ").replace(/[\s-]+$/, "").replace(/^[\s-]+/, "").trim();83export function parseName(file: string, parentDir: string): { kind: "movie" | "series"; title: string; year: number | null; season?: number; episode?: number; episodeName?: string | null } {84 const stem = path.basename(file).replace(/\.[a-z0-9]{2,4}$/i, "");85 const se = /^(.*?)[ ._-]*(?:s(\d{1,2})[ ._-]?e(\d{1,3})|(\d{1,2})x(\d{1,3}))(.*)$/i.exec(stem);86 if (se) {87 const parent = path.basename(parentDir);88 const fromFolder = /^(season|saison|s)\s*\d+$/i.test(parent) ? path.basename(path.dirname(parentDir)) : parent;89 const y = /\((\d{4})\)/.exec(se[1] || fromFolder);90 const title = tidy((se[1] || fromFolder).replace(/\(\d{4}\)/, "")) || tidy(fromFolder);91 const rest = tidy((se[6] ?? "").replace(JUNK, "").replace(/^[\s-]+/, ""));92 return { kind: "series", title, year: y ? +y[1] : null, season: +(se[2] ?? se[4]), episode: +(se[3] ?? se[5]), episodeName: rest || null };93 }94 const y = /[ ._(\[]((?:19|20)\d{2})[ ._)\]]/.exec(stem + " ");95 const title = tidy((y ? stem.slice(0, y.index) : stem.replace(JUNK, "")).replace(/[([]+$/, ""));96 return { kind: "movie", title: title || stem, year: y ? +y[1] : null };97}9899// --- scan -----------------------------------------------------------------------------------------------------------100export interface Dir { id: number; path: string; kind: "movies" | "series" | "documentaries" | "videos"; label: string; enabled: number }101function walk(root: string, out: string[], depth = 0) {102 if (depth > 8) return;103 for (const e of fs.readdirSync(root, { withFileTypes: true })) {104 if (e.name.startsWith(".") || e.name.startsWith("@") || e.name === "#recycle" || e.name === "lost+found") continue;105 const p = path.join(root, e.name);106 if (e.isDirectory()) walk(p, out, depth + 1); else if (e.isFile() && VIDEO_EXT.test(e.name)) out.push(p);107 }108}109function sidecars(file: string): { label: string; lang: string; path: string }[] {110 const dir = path.dirname(file), stem = path.basename(file).replace(/\.[a-z0-9]{2,4}$/i, "");111 try {112 return fs.readdirSync(dir).filter((n) => n.startsWith(stem) && /\.(srt|vtt)$/i.test(n)).map((n) => {113 const tag = n.slice(stem.length).replace(/\.(srt|vtt)$/i, "").replace(/^[._-]+/, ""); const l = lang(tag);114 return { label: LABEL[l] || tag.toUpperCase() || "Sous-titres", lang: l, path: path.join(dir, n) };115 });116 } catch { return []; }117}118119export interface ScanResult { dirs: { id: number; label: string; ok: boolean; files: number; added: number; removed: number; errors: number; error?: string }[]; durationMs: number }120121/** Index every declared folder: new and changed files are probed and catalogued, vanished files are removed. An unreachable folder (NAS offline) is reported and nothing of it is deleted. */122export async function scanLibrary(only?: number): Promise<ScanResult> {123 const t0 = now(); const res: ScanResult = { dirs: [], durationMs: 0 };124 const dirs = all<Dir>(`SELECT id, path, kind, label, enabled FROM library_dirs WHERE enabled = 1 ${only ? "AND id = ?" : ""} ORDER BY id`, ...(only ? [only] : []));125 for (const d of dirs) {126 const r = { id: d.id, label: d.label, ok: true, files: 0, added: 0, removed: 0, errors: 0 } as ScanResult["dirs"][number];127 const chk = checkDir(d.path);128 if (!chk.ok) {129 r.ok = false; r.error = chk.error; res.dirs.push(r);130 run("UPDATE library_dirs SET last_scan_at = ?, last_status = 'error', last_error = ? WHERE id = ?", now(), chk.error, d.id);131 continue;132 }133 try {134 const files: string[] = []; walk(chk.real, files); r.files = files.length;135 const known = new Map(all<{ id: number; path: string; mtime: number | null; size_bytes: number | null }>("SELECT m.id, m.path, m.mtime, m.size_bytes FROM media m JOIN titles t ON t.id = m.title_id WHERE t.library_dir = ?", d.id).map((m) => [m.path, m]));136 for (const f of files) {137 const st = fs.statSync(f); const prev = known.get(f); known.delete(f);138 if (prev && prev.mtime === Math.round(st.mtimeMs) && prev.size_bytes === st.size) continue;139 // A file still being copied changes size between two looks: leave it for the next scan.140 if (now() - st.mtimeMs < 20_000) continue;141 try {142 const p = await probe(f);143 if (!p.vcodec) throw new Error("Aucune piste vidéo");144 const n = d.kind === "videos" ? { kind: "movie" as const, title: path.basename(f).replace(/\.[a-z0-9]{2,4}$/i, "").replace(/[._]+/g, " "), year: null } : parseName(f, path.dirname(f));145 const meta = d.kind === "videos" ? null : await enrichment(n.title, n.year, n.kind).catch(() => null);146 const t = upsertTitle({147 kind: n.kind, origin: "library", originId: `${d.id}:${n.kind}:${n.title.toLowerCase()}:${n.year ?? ""}`, title: meta?.title ?? n.title, original: meta?.original ?? null, year: meta?.year ?? n.year,148 synopsis: meta?.synopsis ?? null, synopsisSource: meta?.synopsisSource ?? null, runtime: n.kind === "movie" ? p.duration : null, genres: meta?.genres ?? [], directors: meta?.directors ?? [], cast: meta?.cast ?? [],149 languages: meta?.languages ?? [...new Set(p.audio.map((a) => a.lang).filter((l) => l !== "und"))], countries: meta?.countries ?? [], poster: meta?.poster ?? null, backdrop: meta?.backdrop ?? null,150 documentary: d.kind === "documentaries" || !!meta?.documentary, animation: !!meta?.animation, short: n.kind === "movie" && !!p.duration && p.duration < 40 * 60,151 rightsStatus: "personal", rightsBasis: "Fichier personnel de votre bibliothèque", sourceName: d.label, imdb: meta?.imdb ?? null, wikidata: meta?.wikidata ?? null, libraryDir: d.id,152 });153 const ep = n.kind === "series" ? upsertEpisode(t.id, n.season!, n.episode!, n.episodeName ?? null) : null;154 const mid = setMedia(t.id, ep, {155 kind: "file", path: f, container: p.container, vcodec: p.vcodec, acodec: p.acodec, width: p.width, height: p.height, duration: p.duration, size: st.size, mtime: Math.round(st.mtimeMs), playback: playbackMode(p),156 subtitles: [...sidecars(f).map((s) => ({ ...s, source: "file" })), ...p.subs.filter((s) => s.text).map((s) => ({ label: s.label, lang: s.lang, stream: s.index, source: "embedded" }))], audio: p.audio,157 });158 if (!prev) r.added++;159 if (!meta?.poster) await thumbnail(mid, f, p.duration).catch(() => {});160 } catch (e) {161 r.errors++; log("warn", "library", `Fichier non indexé : ${path.basename(f)}`, { dir: d.label, error: (e as Error).message });162 }163 }164 // Files that disappeared from a reachable folder.165 for (const gone of known.values()) { run("DELETE FROM media WHERE id = ?", gone.id); r.removed++; }166 run("DELETE FROM episodes WHERE title_id IN (SELECT id FROM titles WHERE library_dir = ?) AND id NOT IN (SELECT episode_id FROM media WHERE episode_id IS NOT NULL)", d.id);167 run("DELETE FROM titles WHERE library_dir = ? AND id NOT IN (SELECT title_id FROM media)", d.id);168 run("UPDATE library_dirs SET last_scan_at = ?, last_status = 'ok', last_error = NULL, file_count = ?, new_count = ?, error_count = ? WHERE id = ?", now(), r.files, r.added, r.errors, d.id);169 } catch (e) {170 r.ok = false; r.error = (e as Error).message;171 run("UPDATE library_dirs SET last_scan_at = ?, last_status = 'error', last_error = ? WHERE id = ?", now(), r.error, d.id);172 }173 res.dirs.push(r);174 }175 res.durationMs = now() - t0;176 if (res.dirs.some((d) => d.added || d.removed)) bumpTitles();177 if (res.dirs.some((d) => d.added || d.removed || d.errors || !d.ok)) log(res.dirs.some((d) => !d.ok) ? "warn" : "info", "library", "Bibliothèque analysée", res);178 return res;179}180181export const thumbPath = (mediaId: number) => path.join(env.dataDir, "thumbs", `${mediaId}.jpg`);182export async function thumbnail(mediaId: number, file: string, duration: number | null) {183 const bin = ffmpegPath(); if (!bin) return;184 fs.mkdirSync(path.dirname(thumbPath(mediaId)), { recursive: true });185 await exec(bin, ["-hide_banner", "-loglevel", "error", "-y", "-ss", String(Math.max(1, Math.round((duration ?? 60) * 0.12))), "-i", file, "-frames:v", "1", "-vf", "scale=640:-2", "-q:v", "4", thumbPath(mediaId)], 60_000);186}187188/** State of a folder for the administration panel: reachable, free space, share type. */189export function dirStatus(p: string) {190 const chk = checkDir(p);191 if (!chk.ok) return { reachable: false, error: chk.error, totalBytes: null, freeBytes: null };192 try { const s = fs.statfsSync(chk.real); return { reachable: true, error: null, totalBytes: s.blocks * s.bsize, freeBytes: s.bavail * s.bsize }; }193 catch { return { reachable: true, error: null, totalBytes: null, freeBytes: null }; }194}195196// --- on-demand HLS conversion ---------------------------------------------------------------------------------------197interface Job { proc: ChildProcess | null; dir: string; done: boolean; error: string | null; startedAt: number }198type G = typeof globalThis & { __sptvTranscodes?: Map<string, Job> };199const jobs = () => ((globalThis as G).__sptvTranscodes ??= new Map());200const cacheRoot = () => path.join(env.dataDir, "transcode");201202function evict(keep: string) {203 const limit = getNum("transcode_cache_gb") * 1024 ** 3;204 let entries: { dir: string; size: number; atime: number }[] = [];205 try {206 entries = fs.readdirSync(cacheRoot()).map((n) => {207 const dir = path.join(cacheRoot(), n); let size = 0, atime = 0;208 for (const f of fs.readdirSync(dir)) { const s = fs.statSync(path.join(dir, f)); size += s.size; atime = Math.max(atime, s.atimeMs, s.mtimeMs); }209 return { dir, size, atime };210 });211 } catch { return; }212 let total = entries.reduce((a, e) => a + e.size, 0);213 for (const e of entries.sort((a, b) => a.atime - b.atime)) {214 if (total <= limit) break;215 if (e.dir === keep || [...jobs().values()].some((j) => j.dir === e.dir && !j.done)) continue;216 fs.rmSync(e.dir, { recursive: true, force: true }); total -= e.size;217 }218}219220/** Starts (or reuses) the HLS rendition of a local file and returns its folder. */221export function ensureHls(m: { id: number; path: string; vcodec: string | null; acodec: string | null; playback: string; height?: number | null }, audioIndex: number): Job {222 const key = `${m.id}-a${audioIndex}`; const dir = path.join(cacheRoot(), key);223 const cur = jobs().get(key); if (cur && (cur.done || cur.proc)) return cur;224 if (fs.existsSync(path.join(dir, "done"))) { const j = { proc: null, dir, done: true, error: null, startedAt: now() }; jobs().set(key, j); return j; }225 const bin = ffmpegPath();226 const job: Job = { proc: null, dir, done: false, error: bin ? null : "ffmpeg n'est pas installé sur ce nœud", startedAt: now() };227 jobs().set(key, job); if (!bin) return job;228 if ([...jobs().values()].filter((j) => j.proc).length >= 2) { job.error = "Deux conversions sont déjà en cours ; réessayez dans un instant"; jobs().delete(key); return job; }229 fs.rmSync(dir, { recursive: true, force: true }); fs.mkdirSync(dir, { recursive: true }); evict(dir);230 const copyV = m.playback === "remux" || m.playback === "direct";231 // Target bitrate follows the picture size: no point in 6 Mb/s for a 480p source.232 const h = Math.min(1080, m.height || 1080); const rate = h <= 360 ? 900 : h <= 480 ? 1600 : h <= 720 ? 3500 : 6000;233 const args = ["-hide_banner", "-loglevel", "error", "-y", "-i", m.path, "-map", "0:v:0", "-map", `0:a:${audioIndex}?`, "-sn", "-dn",234 ...(copyV ? ["-c:v", "copy"] : ["-c:v", "h264_videotoolbox", "-b:v", `${rate}k`, "-maxrate", `${Math.round(rate * 1.4)}k`, "-bufsize", `${rate * 2}k`, "-vf", "scale='min(1920,iw)':-2,format=yuv420p", "-g", "144", "-force_key_frames", "expr:gte(t,n_forced*6)"]),235 ...(m.acodec === "aac" ? ["-c:a", "copy"] : ["-c:a", "aac", "-b:a", "192k", "-ac", "2"]),236 "-f", "hls", "-hls_time", "6", "-hls_list_size", "0", "-hls_playlist_type", "event", "-hls_flags", "independent_segments", "-hls_segment_filename", path.join(dir, "seg%05d.ts"), path.join(dir, "index.m3u8")];237 let errOut = "";238 const proc = spawn(/*turbopackIgnore: true*/ bin, args, { stdio: ["ignore", "ignore", "pipe"] });239 job.proc = proc; proc.stderr!.on("data", (d: Buffer) => { errOut = (errOut + d.toString()).slice(-600); });240 proc.on("exit", (code) => {241 job.proc = null;242 if (code === 0) { job.done = true; fs.writeFileSync(path.join(dir, "done"), new Date().toISOString()); log("info", "library", `Conversion terminée (${copyV ? "copie de flux" : "VideoToolbox"}) : ${path.basename(m.path)}`, { seconds: Math.round((now() - job.startedAt) / 1000) }); }243 else { job.error = errOut.trim() || `ffmpeg a quitté avec le code ${code}`; log("error", "library", `Conversion en échec : ${path.basename(m.path)}`, { error: job.error }); }244 });245 proc.on("error", (e) => { job.proc = null; job.error = e.message; });246 return job;247}248export async function waitFor(file: string, job: Job, ms: number): Promise<boolean> {249 const end = now() + ms;250 while (now() < end) { if (fs.existsSync(file)) return true; if (job.error || (job.done && !fs.existsSync(file))) return false; await new Promise((r) => setTimeout(r, 200)); }251 return fs.existsSync(file);252}253254export function srtToVtt(srt: string): string {255 const body = srt.replace(/^/, "").replace(/\r/g, "").replace(/(\d{2}:\d{2}:\d{2}),(\d{3})/g, "$1.$2").replace(/\{\\[^}]*\}/g, "");256 return body.trimStart().startsWith("WEBVTT") ? body : `WEBVTT\n\n${body}`;257}258export async function embeddedSubtitle(file: string, stream: number): Promise<string> {259 const bin = ffmpegPath(); if (!bin) throw new Error("ffmpeg absent");260 return exec(bin, ["-hide_banner", "-loglevel", "error", "-i", file, "-map", `0:s:${stream}`, "-f", "webvtt", "-"], 120_000);261}262