/** * @file Vérifie la fabrication des pochettes avec le vrai ffmpeg (image envoyée, pochette intégrée à * un MP3) et le refus des fichiers qui ne sont pas des images ; ignoré sans ffmpeg. */ import test from "node:test"; import assert from "node:assert/strict"; import { spawnSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { createCoverImage, detectImageFormat } from "../src/media/coverArt.js"; const hasFfmpeg = spawnSync("ffmpeg", ["-version"]).status === 0 && spawnSync("ffprobe", ["-version"]).status === 0; const skip = hasFfmpeg ? false : "ffmpeg et ffprobe sont nécessaires"; const directory = fs.mkdtempSync(path.join(os.tmpdir(), "palico-cover-")); test.after(() => fs.rmSync(directory, { recursive: true, force: true })); function ffmpeg(...args) { const result = spawnSync("ffmpeg", ["-v", "error", "-y", ...args]); assert.equal(result.status, 0, result.stderr?.toString()); } function imageSize(file) { const result = spawnSync("ffprobe", [ "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height,codec_name", "-of", "json", file, ]); return JSON.parse(result.stdout.toString()).streams[0]; } test("image signatures are recognised, other files are not", async () => { const samples = { "a.png": Buffer.from("89504e470d0a1a0a0000000d", "hex"), "a.jpg": Buffer.from("ffd8ffe000104a4649460001", "hex"), "a.gif": Buffer.from("GIF89a\x01\x00\x01\x00\x00\x00", "latin1"), "a.webp": Buffer.from("RIFF\x24\x00\x00\x00WEBPVP8 ", "latin1"), "playlist.png": Buffer.from("#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:0\n"), "short.png": Buffer.from("89504e47", "hex"), }; const formats = {}; for (const [name, bytes] of Object.entries(samples)) { const file = path.join(directory, name); fs.writeFileSync(file, bytes); formats[name] = await detectImageFormat(file); } assert.deepEqual(formats, { "a.png": "png_pipe", "a.jpg": "jpeg_pipe", "a.gif": "gif", "a.webp": "webp_pipe", "playlist.png": null, "short.png": null, }); }); test("an uploaded image becomes a JPEG of at most 512 px", { skip }, async () => { const input = path.join(directory, "wide.png"); ffmpeg("-f", "lavfi", "-i", "color=c=purple:s=1024x256", "-frames:v", "1", input); const output = path.join(directory, "wide.jpg"); await createCoverImage(input, output, { inputFormat: await detectImageFormat(input) }); assert.deepEqual(imageSize(output), { codec_name: "mjpeg", width: 512, height: 128 }); }); test("the cover embedded in an MP3 is extracted, and its absence is an error", { skip }, async () => { const picture = path.join(directory, "picture.jpg"); ffmpeg("-f", "lavfi", "-i", "color=c=orange:s=300x300", "-frames:v", "1", picture); const withCover = path.join(directory, "with-cover.mp3"); ffmpeg( "-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-i", picture, "-map", "0:a", "-map", "1:v", "-c:a", "libmp3lame", "-c:v", "copy", "-disposition:v", "attached_pic", withCover ); const withoutCover = path.join(directory, "without-cover.mp3"); ffmpeg("-f", "lavfi", "-i", "sine=frequency=440:duration=1", "-c:a", "libmp3lame", withoutCover); const output = path.join(directory, "extracted.jpg"); await createCoverImage(withCover, output); assert.deepEqual(imageSize(output), { codec_name: "mjpeg", width: 300, height: 300 }); await assert.rejects(createCoverImage(withoutCover, path.join(directory, "none.jpg"))); });