Resume interrupted tracks where they stopped

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>
This commit is contained in:
Shinuwa 2026-12-01 23:11:05 +01:00
parent 11ee972636
commit 793765fb79
8 changed files with 141 additions and 24 deletions

View file

@ -1,6 +1,7 @@
/**
* @file Vérifie avec ffprobe qu'un fichier est de l'audio (durée, titre, artiste), et le convertit
* avec ffmpeg en Ogg Opus au volume normalisé, prêt pour Discord.
* avec ffmpeg en Ogg Opus au volume normalisé, prêt pour Discord (depuis le début ou depuis une
* position donnée).
*
* Utilisé par : music/ingest.js et storage/libraryMetadata.js (probeAudioFile), music/queue.js
* (createOpusStream).
@ -81,13 +82,16 @@ function invalidAudioError(cause) {
// Décode le fichier, normalise le volume et encode en Ogg Opus avec le ffmpeg natif : Discord
// reçoit l'Opus tel quel, sans réencodage en JavaScript. Fermer le flux arrête ffmpeg.
function createOpusStream(filePath, { spawnProcess = spawn } = {}) {
// startAt : position de départ en secondes, pour reprendre un morceau interrompu.
function createOpusStream(filePath, { spawnProcess = spawn, startAt = 0 } = {}) {
const ffmpeg = spawnProcess(
"ffmpeg",
[
"-hide_banner",
"-loglevel",
"error",
// Placé avant -i : ffmpeg saute directement à la position au lieu de tout décoder.
...(startAt > 0 ? ["-ss", String(startAt)] : []),
"-i",
filePath,
"-map",