Repository Public
1import { execFile } from "node:child_process";2import path from "node:path";3import { promisify } from "node:util";4import { sql } from "@/lib/db";5import { env } from "@/lib/env";6import { ApiError } from "@/lib/http";7import { audit } from "@/lib/audit";8import type { UserContext } from "@/lib/auth";9import { clean, norm } from "@/lib/text";10import { storage } from "./storage";1112const run = promisify(execFile);1314/** One displayed line. `w` (word timings) exists on transcribed lines; a line typed by hand has none. */15export interface LyricLine { t: number; e: number; text: string; w?: [number, number, string][] }16interface Segment { start: number; end: number; text: string; no_speech_prob: number; avg_logprob: number; compression_ratio: number; words: { start: number; end: number; word: string }[] }1718export const LANGUAGES = ["fr", "en", "es", "it", "de", "pt"] as const;19export const lyricsEnabled = () => env.lyrics !== "off" && !!env.lyricsPython;2021// Sentences speech models invent over music or silence (subtitle credits from their training data).22const HALLUCINATIONS = [/sous-?titr/i, /soci[ée]t[ée] radio-canada/i, /amara\.org/i, /merci d'avoir regard/i, /thanks? for watching/i, /subtitles? by/i, /^\s*(music|musique|applaud\w*|♪+)\s*$/i, /abonnez-vous/i, /subscribe/i];23// Stock phrases the model emits, with full confidence, over music or a plain tone where nobody sings.24const FILLERS = new Set(["thank you", "thank you very much", "thanks", "thanks for watching", "you", "bye", "bye bye", "merci", "merci beaucoup", "a bientot", "oh", "ah", "hmm", "okay", "ok"]);25const isFiller = (text: string) => FILLERS.has(norm(text).replace(/[^a-z ]/g, "").trim());26const MAX_LINE_CHARS = 46;2728/** Model output → display lines: hallucinations and low-confidence noise dropped, long segments cut at word boundaries. */29export function toLines(segments: Segment[]): LyricLine[] {30 const lines: LyricLine[] = [];31 let prevText = "", repeats = 0;32 for (const s of segments) {33 const text = clean(s.text, 500);34 if (!text || s.words.length === 0) continue;35 if (HALLUCINATIONS.some((h) => h.test(text))) continue;36 if (s.no_speech_prob > 0.6 && s.avg_logprob < -1) continue; // the model itself doubts anyone is singing37 if (s.compression_ratio > 2.8) continue; // degenerate repetition inside one segment38 // The same sentence more than four times in a row is a decoding loop far more often than a chorus.39 const n = norm(text);40 repeats = n === prevText ? repeats + 1 : 0; prevText = n;41 if (repeats >= 4) continue;42 let cur: [number, number, string][] = [], len = 0;43 const flush = () => {44 if (!cur.length) return;45 lines.push({ t: cur[0][0], e: cur[cur.length - 1][1], text: cur.map((w) => w[2]).join("").trim(), w: cur.map((w, i) => [w[0], w[1], i === 0 ? w[2].trimStart() : w[2]]) });46 cur = []; len = 0;47 };48 for (const w of s.words) {49 const word = w.word.replace(/[\u0000-\u001f]/g, ""); // eslint-disable-line no-control-regex50 if (!word.trim()) continue;51 const startsNew = len + word.length > MAX_LINE_CHARS && len > 12 && word.startsWith(" ");52 if (startsNew) flush();53 const start = Math.max(0, Math.round(w.start * 1000)), end = Math.max(start + 40, Math.round(w.end * 1000));54 cur.push([start, end, word]); len += word.length;55 if (/[.!?…]\s*$/.test(word) && len > 18) flush();56 }57 flush();58 }59 const kept = lines.filter((l) => l.text.length > 0);60 // A filler standing alone (more than 8 s of nothing on both sides) is noise, not a lyric; a transcript made of61 // nothing but fillers means the track is instrumental.62 const out = kept.filter((l, i) => !isFiller(l.text) || (i > 0 && l.t - kept[i - 1].e < 8000) || (i < kept.length - 1 && kept[i + 1].t - l.e < 8000));63 return out.every((l) => isFiller(l.text)) ? [] : out;64}6566/** Language hint when none was requested: the genre tag sometimes says it; otherwise the model detects. */67function languageFromGenre(genre: string | null): string | null {68 const g = norm(genre ?? "");69 if (/franco|chanson|quebec|french|francais/.test(g)) return "fr";70 return null;71}7273/** Worker side: transcribes one queued track. Never throws; the outcome is written on the row. */74export async function transcribeTrack(trackId: string): Promise<void> {75 const fail = (error: string) => sql`UPDATE lyrics SET state = 'error', error = ${error.slice(0, 200)}, updated_at = now() WHERE track_id = ${trackId}`;76 try {77 if (!lyricsEnabled()) return void (await fail("lyrics_disabled"));78 const [t] = await sql<{ storage_key: string; requested_language: string | null; genre: string | null }[]>`79 SELECT m.storage_key, l.requested_language, g.name AS genre FROM lyrics l JOIN tracks t ON t.id = l.track_id80 JOIN media_assets m ON m.track_id = t.id AND m.kind = 'original' LEFT JOIN genres g ON g.id = t.genre_id WHERE l.track_id = ${trackId}`;81 if (!t) return;82 const file = await storage().localPath(t.storage_key);83 const language = t.requested_language ?? languageFromGenre(t.genre) ?? "auto";84 const script = path.join(process.cwd(), "scripts", "transcribe.py");85 let stdout: string;86 try {87 ({ stdout } = await run(env.lyricsPython!, [script, file, env.lyricsModel, language], { timeout: 30 * 60_000, maxBuffer: 128 * 1024 * 1024, env: { ...process.env, HF_HUB_DISABLE_TELEMETRY: "1", TOKENIZERS_PARALLELISM: "false" } }));88 } catch (e) {89 const msg = String((e as { stderr?: string }).stderr ?? "").split("\n").filter((l) => l.startsWith("transcribe failed")).pop();90 return void (await fail(msg ?? "transcription_failed"));91 }92 const out = JSON.parse(stdout) as { language: string | null; segments: Segment[] };93 const lines = toLines(out.segments);94 await sql`UPDATE lyrics SET state = ${lines.length ? "ready" : "empty"}, lines = ${sql.json(lines as never)}, language = ${out.language}, source = 'whisper',95 model = ${env.lyricsModel}, edited = false, error = NULL, updated_at = now() WHERE track_id = ${trackId}`;96 } catch (e) {97 console.error("[lyrics]", trackId, e instanceof Error ? `${e.name}: ${e.message}` : String(e));98 await fail("internal").catch(() => {});99 }100}101102export async function claimLyrics(): Promise<string | null> {103 const [l] = await sql<{ track_id: string }[]>`104 UPDATE lyrics SET state = 'working', updated_at = now() WHERE track_id = (SELECT track_id FROM lyrics WHERE state = 'queued' ORDER BY updated_at FOR UPDATE SKIP LOCKED LIMIT 1) RETURNING track_id`;105 return l?.track_id ?? null;106}107108/** Queues a transcription. Lines corrected by hand are kept unless `overwrite` is explicit. */109export async function queueLyrics(trackIds: string[], language: string | null, overwrite: boolean): Promise<number> {110 const r = await sql`111 INSERT INTO lyrics (track_id, state, requested_language) SELECT id, 'queued', ${language} FROM tracks WHERE id = ANY(${trackIds}::uuid[])112 ON CONFLICT (track_id) DO UPDATE SET state = 'queued', requested_language = EXCLUDED.requested_language, error = NULL, updated_at = now()113 WHERE lyrics.state NOT IN ('queued', 'working') AND (${overwrite} OR NOT lyrics.edited)114 RETURNING track_id`;115 return r.length;116}117118// ── API ─────────────────────────────────────────────────────────────────────────────────────────────119120export async function getLyrics(trackId: string) {121 const [l] = await sql<{ state: string; language: string | null; source: string; edited: boolean; lines: LyricLine[]; error: string | null }[]>`122 SELECT state, language, source, edited, lines, error FROM lyrics WHERE track_id = ${trackId}`;123 if (!l) { const [t] = await sql`SELECT 1 FROM tracks WHERE id = ${trackId}`; if (!t) throw new ApiError(404, "not_found"); return { state: "none", lines: [], enabled: lyricsEnabled() }; }124 return { state: l.state, language: l.language, source: l.source, edited: l.edited, lines: l.state === "ready" ? l.lines : [], enabled: lyricsEnabled() };125}126127export async function requestLyrics(user: UserContext, ids: string[], language: string | null, overwrite: boolean) {128 if (!lyricsEnabled()) throw new ApiError(409, "lyrics_disabled", "Lyrics transcription is not configured on this server");129 const queued = await queueLyrics(ids, language, overwrite);130 await audit({ userId: user.userId, actor: "admin", action: "lyrics.requested", meta: { count: queued, language: language ?? "auto" } });131 return { queued, skipped: ids.length - queued };132}133134/** Manual correction: line timings are kept; a line whose text is unchanged keeps its word timings too. */135export async function saveLyrics(user: UserContext, trackId: string, input: { t: number; e: number; text: string }[], language: string | null) {136 const [t] = await sql`SELECT 1 FROM tracks WHERE id = ${trackId}`;137 if (!t) throw new ApiError(404, "not_found", "Track not found");138 const [old] = await sql<{ lines: LyricLine[] }[]>`SELECT lines FROM lyrics WHERE track_id = ${trackId}`;139 const prev = new Map((old?.lines ?? []).map((l) => [`${l.t}:${l.text}`, l]));140 const lines: LyricLine[] = input.map((l) => ({ t: l.t, e: Math.max(l.e, l.t + 200), text: clean(l.text, 300) })).filter((l) => l.text).sort((a, b) => a.t - b.t)141 .map((l) => { const same = prev.get(`${l.t}:${l.text}`); return same?.w ? { ...l, w: same.w } : l; });142 await sql`INSERT INTO lyrics (track_id, state, lines, source, edited, language) VALUES (${trackId}, ${lines.length ? "ready" : "empty"}, ${sql.json(lines as never)}, 'manual', true, ${language})143 ON CONFLICT (track_id) DO UPDATE SET state = EXCLUDED.state, lines = EXCLUDED.lines, edited = true, language = coalesce(EXCLUDED.language, lyrics.language), error = NULL, updated_at = now()`;144 await audit({ userId: user.userId, actor: "admin", action: "lyrics.edited", entity: "track", entityId: trackId, meta: { lines: lines.length } });145 return { lines: lines.length };146}147148export async function deleteLyrics(user: UserContext, trackId: string) {149 const r = await sql`DELETE FROM lyrics WHERE track_id = ${trackId} RETURNING track_id`;150 if (r.length === 0) throw new ApiError(404, "not_found");151 await audit({ userId: user.userId, actor: "admin", action: "lyrics.deleted", entity: "track", entityId: trackId });152 return { ok: true };153}154