Repository Public
1import { mkdir } from "node:fs/promises";2import path from "node:path";3import { ff, probe, run, type Probe } from "./ffmpeg";45/**6 * Validation réelle d'un média téléversé (ffprobe + décodage d'essai) : tout fichier qui n'est pas un média décodable du type annoncé7 * est rejeté par une erreur explicite. Fabrique un proxy H.264 540p (vidéo) et une miniature JPEG (vidéo, image).8 */9const even = (n: number) => Math.max(2, Math.round(n / 2) * 2);1011export async function ingestMedia(o: { file: string; kind: "video" | "audio" | "image" | "music"; workDir: string }): Promise<{ probe: Probe; proxy?: string; thumb?: string }> {12 let p: Probe;13 try { p = await probe(o.file); } catch (e) { throw new Error(`Fichier illisible : ce n'est pas un média reconnu (${(e as Error).message})`); }14 const reject = (why: string): never => { throw new Error(`Média refusé (${o.kind}) : ${why}`); };15 if (o.kind === "video") {16 if (!p.hasVideo || p.isStill) reject("aucune piste vidéo");17 if (!(p.duration > 0.1)) reject("durée nulle");18 } else if (o.kind === "image") {19 if (!p.hasVideo) reject("aucune image décodable");20 if (!p.isStill && (p.duration > 0.2 || p.hasAudio)) reject("le fichier est une vidéo ou une animation, pas une image fixe");21 } else {22 if (!p.hasAudio) reject("aucune piste audio");23 if (p.hasVideo && !p.isStill && (p.frames ?? 2) > 1) reject("le fichier contient de la vidéo, pas seulement de l'audio");24 if (!(p.duration > 0.1)) reject("durée nulle");25 }26 if (p.hasVideo && (p.width < 16 || p.height < 16 || p.width > 16384 || p.height > 16384)) reject(`dimensions hors limites (${p.width}×${p.height})`);2728 // décodage d'essai : la première seconde (ou l'image) doit se décoder sans erreur29 try {30 await run("ffmpeg", ["-hide_banner", "-nostdin", "-v", "error", "-xerror", "-t", "1", "-i", o.file, ...(o.kind === "image" ? ["-frames:v", "1"] : []), "-f", "null", "-"], 120_000);31 } catch (e) { reject(`décodage impossible (${(e as Error).message})`); }3233 await mkdir(o.workDir, { recursive: true });34 const rotated = Math.abs(p.rotation ?? 0) % 180 === 90;35 const dw = rotated ? p.height : p.width, dh = rotated ? p.width : p.height;36 if (o.kind === "video") {37 const proxy = path.join(o.workDir, "proxy.mp4"), thumb = path.join(o.workDir, "thumb.jpg");38 const h = Math.min(540, even(dh)), w = even((dw * h) / dh);39 await ff(["-i", o.file, "-map", "0:v:0", ...(p.hasAudio ? ["-map", "0:a:0"] : []), "-vf", `scale=${w}:${h}:flags=bicubic,setsar=1,format=yuv420p`,40 "-c:v", "libx264", "-preset", "veryfast", "-crf", "26", "-profile:v", "high", "-g", "60", ...(p.hasAudio ? ["-c:a", "aac", "-b:a", "96k", "-ar", "48000", "-ac", "2"] : []),41 "-movflags", "+faststart", "-map_metadata", "-1", proxy], 3_600_000);42 const at = Math.min(Math.max(p.duration * 0.2, 0), Math.max(p.duration - 0.2, 0));43 await ff(["-ss", at.toFixed(3), "-i", o.file, "-frames:v", "1", "-vf", `scale=${even((dw * 360) / dh)}:360:flags=bicubic`, "-q:v", "4", "-update", "1", thumb]);44 return { probe: p, proxy, thumb };45 }46 if (o.kind === "image") {47 const thumb = path.join(o.workDir, "thumb.jpg");48 const h = Math.min(360, even(dh));49 await ff(["-i", o.file, "-frames:v", "1", "-vf", `scale=${even((dw * h) / dh)}:${h}:flags=bicubic`, "-q:v", "4", "-update", "1", thumb]);50 return { probe: p, thumb };51 }52 return { probe: p };53}54