import { NextResponse } from "next/server"; import { compactSavedVideosAction } from "../../../saved-videos/compactActions"; import { OpsInputError, ops, opsFail, optBool, reqSlugs, reqVideoItems, type OpsBody } from "../_lib"; export const dynamic = "force-dynamic"; // POST { items: [{ slug, id }, …] } | { select: true, slugs? }, // dryRun?, retryFailed?, reverify?, belowThreshold?, crf?, preset?, maxFps?, chunkMinutes? // dryRun -> { ok: true, dryRun: true, plan: { items, willCompact, totals, lines } } // else -> { ok: true, dryRun: false, plan, jobs: [{ slug, jobId }], jobIds, // jobId (one job only), skipped: [{ slug, reason }] } // // Compact saved videos (controller/compactSavedVideos.ts): each one re-encoded // to AV1 (settings.savedVideoCompaction: crf, preset, the frame-rate cap), // verified, made the active copy, the original kept beside it as // `keptAs: "original"` — dropped later in a step of its own when // `dropOriginal` is "after-verify" and the video is still on its platform; // otherwise it waits for a person (`get compaction-review`). `items` names the // videos (deliberately stored only); `select` asks the selector (platform, // height, video bitrate; `slugs` narrows it to those channels). The plan's // `lines` say, per video, its size, bitrate, height, fps, the estimate (size, // hours), the disk it needs against what is free, or why it is skipped. One // job per channel on the `compact` queue; a re-run resumes. // // `{"select": true}` IS A DRY RUN unless the body says `"dryRun": false`; an // explicit list keeps the usual default (a real run). An explicit item the // selector would not pick (YouTube, below the bitrate or height floors) is // refused — the refusal and the dry run say how little it would save — unless // `"belowThreshold": true`. The deliberately-stored rule has no override. // // A video whose last run FAILED A CHECK keeps its encode (the chunks and the // joined copy, in its scratch, never active): `"reverify": true` checks it // again without encoding (a chunk whose own content failed is encoded again), // `"retryFailed": true` encodes it all again; `kept-failed-encodes` lists and // clears them. function optNumber(body: OpsBody, key: string, lo: number, hi: number): number | undefined { const v = body[key]; if (v === undefined) return undefined; if (typeof v !== "number" || !Number.isFinite(v) || v < lo || v > hi) { throw new OpsInputError(`"${key}" must be a number from ${lo} to ${hi}`); } return v; } export async function POST(request: Request) { return ops( request, ["items", "select", "slugs", "dryRun", "retryFailed", "reverify", "belowThreshold", "crf", "preset", "maxFps", "chunkMinutes"], async (body) => { const select = optBool(body, "select") === true; const crf = optNumber(body, "crf", 20, 55); const preset = optNumber(body, "preset", 4, 12); const maxFps = optNumber(body, "maxFps", 0, 240); const chunkMinutes = optNumber(body, "chunkMinutes", 1, 60); const result = await compactSavedVideosAction({ ...(select ? { select: true } : { items: reqVideoItems(body, "items") }), ...(body.slugs !== undefined ? { slugs: reqSlugs(body, "slugs") } : {}), // The selector's default is a dry run: a real one is asked by name. dryRun: optBool(body, "dryRun") ?? select, retryFailed: optBool(body, "retryFailed") ?? false, reverify: optBool(body, "reverify") ?? false, belowThreshold: optBool(body, "belowThreshold") ?? false, overrides: { ...(crf !== undefined ? { crf } : {}), ...(preset !== undefined ? { preset } : {}), ...(maxFps !== undefined ? { maxFps } : {}), ...(chunkMinutes !== undefined ? { chunkMinutes } : {}), }, }); if (!result.ok) return opsFail(result.error); if (result.dryRun) return NextResponse.json(result); return NextResponse.json({ ...result, ...(result.jobIds.length === 1 ? { jobId: result.jobIds[0] } : {}), }); }, ); }