A track cut off without anyone asking (voice connection lost, bot leaving an empty channel, restart) used to start over from the beginning; on an hour-long mix, a drop near the end replayed it all. - The requeued track keeps resumeAt and ffmpeg seeks to it (-ss before -i, so it jumps there without decoding the start). It resumes 2 s before the cut; a track cut in its first 2 s starts over. - The player keeps playing without a connection (NoSubscriberBehavior.Play), so the position of a voice drop is taken when the connection drops, not 5 s later when it is given up. - The saved queue holds the current position, and it is saved every 30 s while playing: after a crash the track resumes at most 30 s earlier. - Loop mode replays a resumed track from the beginning; skip and stop are unchanged. - The web page and /queue show "Reprise à 49:15" for such a track, and the progress bar counts from the resume point. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
3.2 KiB
JavaScript
86 lines
3.2 KiB
JavaScript
/**
|
|
* @file Vérifie la lecture des tags et la conversion en Opus avec les vrais ffprobe et ffmpeg ;
|
|
* ignoré s'ils ne sont pas installés.
|
|
*/
|
|
|
|
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { spawn, spawnSync } from "node:child_process";
|
|
import fs from "node:fs";
|
|
import os from "node:os";
|
|
import path from "node:path";
|
|
import { StreamType, createAudioResource } from "@discordjs/voice";
|
|
import { createOpusStream, probeAudioFile } from "../src/media/validateAudioFile.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-audio-"));
|
|
test.after(() => fs.rmSync(directory, { recursive: true, force: true }));
|
|
|
|
// Un MP3 de `seconds` secondes (un la à 440 Hz) avec des tags ID3.
|
|
function makeTone(name, seconds, { title = "Chanson", artist = "Palico" } = {}) {
|
|
const file = path.join(directory, name);
|
|
const result = spawnSync("ffmpeg", [
|
|
"-v", "error", "-y",
|
|
"-f", "lavfi", "-i", `sine=frequency=440:duration=${seconds}`,
|
|
"-metadata", `title=${title}`, "-metadata", `artist=${artist}`,
|
|
"-c:a", "libmp3lame", "-b:a", "64k", file,
|
|
]);
|
|
assert.equal(result.status, 0, result.stderr?.toString());
|
|
return file;
|
|
}
|
|
|
|
test("probeAudioFile reads the duration and the tags", { skip }, async () => {
|
|
const metadata = await probeAudioFile(makeTone("tagged.mp3", 2));
|
|
assert.equal(metadata.duration, 2);
|
|
assert.equal(metadata.title, "Chanson");
|
|
assert.equal(metadata.artist, "Palico");
|
|
});
|
|
|
|
test("probeAudioFile rejects a file without audio", { skip }, async () => {
|
|
const file = path.join(directory, "not-audio.mp3");
|
|
fs.writeFileSync(file, "pas du son");
|
|
await assert.rejects(probeAudioFile(file), { status: 400 });
|
|
});
|
|
|
|
test("createOpusStream yields Opus packets that Discord can play as is", { skip }, async () => {
|
|
const resource = createAudioResource(createOpusStream(makeTone("short.mp3", 1)), {
|
|
inputType: StreamType.OggOpus,
|
|
});
|
|
|
|
let packets = 0;
|
|
for await (const packet of resource.playStream) {
|
|
assert.ok(Buffer.isBuffer(packet) && packet.length > 0);
|
|
packets += 1;
|
|
}
|
|
// 1 s d'audio en trames Opus de 20 ms : une cinquantaine de paquets.
|
|
assert.ok(packets >= 45, `${packets} paquets`);
|
|
});
|
|
|
|
test("createOpusStream can start in the middle of a track", { skip }, async () => {
|
|
const resource = createAudioResource(createOpusStream(makeTone("three.mp3", 3), { startAt: 2 }), {
|
|
inputType: StreamType.OggOpus,
|
|
});
|
|
|
|
let packets = 0;
|
|
for await (const _packet of resource.playStream) {
|
|
packets += 1;
|
|
}
|
|
// Seule la dernière seconde est jouée : une cinquantaine de paquets de 20 ms, pas 150.
|
|
assert.ok(packets >= 40 && packets <= 70, `${packets} paquets`);
|
|
});
|
|
|
|
test("closing the Opus stream stops ffmpeg", { skip }, async () => {
|
|
let child;
|
|
const output = createOpusStream(makeTone("long.mp3", 120), {
|
|
spawnProcess: (...args) => (child = spawn(...args)),
|
|
});
|
|
|
|
await new Promise((resolve) => output.once("data", resolve));
|
|
const exited = new Promise((resolve) => child.once("exit", (_code, signal) => resolve(signal)));
|
|
output.destroy();
|
|
assert.equal(await exited, "SIGKILL");
|
|
});
|