import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { AUDIO_AUDIT_BODY_KEYS, AudioAuditInputError, enqueueAudioAudit, } from "yt-dlp-transcript-common/controller/audioAudit"; import { OpsInputError, opsFail, ops } from "../_lib"; export const dynamic = "force-dynamic"; // POST { slug | platform, scope?: "untranscribed" | "all", max?, force? } // -> { ok: true, jobIds, channels, refused: [{slug, error}] } // // The audio audit (release 31, lane A): every audio file the channel holds // (or every channel on the platform holds) decoded with error tolerance, one // at a time machine-wide at idle priority, its verdict written beside it in // `audio-integrity.json` — clean, damaged or undecodable. One `audio-audit` // job per channel on the `audio-audit` queue. `scope` "untranscribed" (the // default) audits only videos with no transcript yet; "all" every video // holding audio. `max` caps the files decoded across the run; `force` // decodes again a file whose record is still its own. The same body again // resumes. A channel whose media is unreachable is refused (in `refused`); // none started at all is a 409. Each job's log ends `@@audio-audit {json}`. // // AN ADAPTER: the rules are the controller's (controller/audioAudit.ts). export async function POST(request: Request) { return ops(request, AUDIO_AUDIT_BODY_KEYS, async (body) => { let out; try { out = await enqueueAudioAudit(body, { paths: getPaths() }); } catch (e) { if (e instanceof AudioAuditInputError) throw new OpsInputError(e.message); throw e; } // The jobs run to completion on disk; nobody reads these streams. for (const s of out.started) void s.result.stream.cancel(); if (out.started.length === 0) { return opsFail( `No audit started: ${out.refused.map((r) => `${r.slug}: ${r.error}`).join("; ")}`, 409, ); } return NextResponse.json({ ok: true, jobIds: out.started.map((s) => s.result.jobId), channels: out.started.map((s) => s.slug), refused: out.refused, }); }); }