From 2daed2a44d97d77939fc6e515f020a380808930b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alves?= Date: Wed, 21 Oct 2026 18:13:19 +0100 Subject: [PATCH] fix(archive): skip videos with no stream before ffmpeg map (BIT-31622) ffprobe can succeed on audio-only or broken containers while webCopyArgs still maps 0:v:0, which exits 234. Refuse those sources as VideoUnsupported and mark FAILED without useless retries. Paperclip-Issue: BIT-31622 Paperclip-Run: 46c787aa-ef6c-4036-be34-af93b4bfd23d --- apps/api/src/archive/service.ts | 6 +++-- apps/api/src/archive/video.test.ts | 40 +++++++++++++++++++++++++++--- apps/api/src/archive/video.ts | 19 ++++++++++++++ 3 files changed, 60 insertions(+), 5 deletions(-) diff --git a/apps/api/src/archive/service.ts b/apps/api/src/archive/service.ts index 5e9e802..b762e2a 100644 --- a/apps/api/src/archive/service.ts +++ b/apps/api/src/archive/service.ts @@ -15,7 +15,7 @@ import { ingestWindow } from "../library/events.js"; import { waitUntil } from "../lib/idle.js"; import type { LocalStorage } from "../storage/storage.js"; import { CloudClient, cleanBase, pollLogin, startLogin, type CloudEntry } from "./nextcloud.js"; -import { makeWebCopy, VIDEO, VIDEO_FILES, VideoToolMissing, type VideoTools } from "./video.js"; +import { makeWebCopy, VIDEO, VIDEO_FILES, VideoToolMissing, VideoUnsupported, type VideoTools } from "./video.js"; import { teamSideRows } from "./directory.js"; import { galleryAssignments } from "./gallery.js"; import { canonicalTeam, competitionLabel, describeFolder, exhibitionName, type FolderGame, isExhibition, isInitials, isTeamName, knownVersus, lisbonDay, parseFolder, pastOrToday, pickFixture, seasonOf, slashSides, teamMatcher } from "./match.js"; @@ -1170,7 +1170,9 @@ export class ArchiveService { const transient = err instanceof NextcloudError && err.transient; const gone = err instanceof NextcloudError && err.status === 404; const missing = err instanceof VideoToolMissing; - const retry = !gone && !missing && (transient || video.attempts < MAX_ATTEMPTS); + const unsupported = err instanceof VideoUnsupported; + // No video stream / missing tools / gone from cloud: mark FAILED once; retries cannot help. + const retry = !gone && !missing && !unsupported && (transient || video.attempts < MAX_ATTEMPTS); await db.cloudVideo.update({ where: { id: videoId }, data: { status: retry ? "PENDING" : "FAILED", error: message, ...(transient && { attempts: { decrement: 1 } }) } }); if (gone) await db.cloudFolder.update({ where: { id: video.folderId }, data: { etag: null } }); log.warn({ err, videoId, path: video.path }, "archive: video not indexed"); diff --git a/apps/api/src/archive/video.test.ts b/apps/api/src/archive/video.test.ts index 2799bf5..bbf697f 100644 --- a/apps/api/src/archive/video.test.ts +++ b/apps/api/src/archive/video.test.ts @@ -4,7 +4,7 @@ import { tmpdir } from "node:os"; import path from "node:path"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { UrlSigner } from "../storage/storage.js"; -import { findVideoTools, makeWebCopy, parseRange, posterAt, readProbe, VIDEO, VideoToolMissing, webCopyArgs, probe } from "./video.js"; +import { findVideoTools, makeWebCopy, parseRange, posterAt, readProbe, requireVideoStream, VIDEO, VideoToolMissing, VideoUnsupported, webCopyArgs, probe } from "./video.js"; describe("archive videos", () => { it("knows a video by its name", () => { @@ -35,10 +35,30 @@ describe("archive videos", () => { { codec_type: "audio" }, ], }); - expect(info).toEqual({ durationMs: 12480, width: 1920, height: 1080, capturedAt: new Date("2025-09-13T18:42:10.000Z"), hasAudio: true }); + expect(info).toEqual({ + durationMs: 12480, + width: 1920, + height: 1080, + capturedAt: new Date("2025-09-13T18:42:10.000Z"), + hasAudio: true, + hasVideo: true, + }); // A camera that never had its clock set. expect(readProbe({ format: { tags: { creation_time: "1970-01-01T00:00:00Z" } }, streams: [{ codec_type: "video" }] }).capturedAt).toBeNull(); - expect(readProbe({ streams: [] })).toMatchObject({ durationMs: null, width: null, hasAudio: false }); + expect(readProbe({ streams: [] })).toMatchObject({ durationMs: null, width: null, hasAudio: false, hasVideo: false }); + // Audio-only container (wrong extension or silent movie stripped to sound). + expect(readProbe({ format: { duration: "3.0" }, streams: [{ codec_type: "audio" }] })).toMatchObject({ + hasAudio: true, + hasVideo: false, + width: null, + height: null, + }); + }); + + it("refuses indexing when there is no video stream", () => { + expect(() => requireVideoStream({ hasVideo: true })).not.toThrow(); + expect(() => requireVideoStream({ hasVideo: false })).toThrow(VideoUnsupported); + expect(() => requireVideoStream({ hasVideo: false })).toThrow("no_video_stream"); }); it("makes an H.264 web copy that starts at once, with or without sound", () => { @@ -139,4 +159,18 @@ describe.skipIf(!hasFfmpeg)("archive videos with ffmpeg", () => { expect(await makeWebCopy(tools, clip("tall.mp4", "1440x2560", false), portrait)).toMatchObject({ width: 1080, height: 1920, hasAudio: false }); expect(await makeWebCopy(tools, clip("small.mp4", "640x360", false), small)).toMatchObject({ width: 640, height: 360 }); }, 60_000); + + it("marks audio-only files unsupported instead of failing on -map 0:v:0", async () => { + const audioOnly = path.join(dir, "sound.mp4"); + execFileSync("ffmpeg", [ + "-hide_banner", "-loglevel", "error", "-y", + "-f", "lavfi", "-i", "sine=frequency=440:duration=1", + "-c:a", "aac", + audioOnly, + ]); + const out = path.join(dir, "audio-only-out"); + await import("node:fs/promises").then((fs) => fs.mkdir(out)); + await expect(makeWebCopy(tools, audioOnly, out)).rejects.toBeInstanceOf(VideoUnsupported); + await expect(makeWebCopy(tools, audioOnly, out)).rejects.toThrow("no_video_stream"); + }, 30_000); }); diff --git a/apps/api/src/archive/video.ts b/apps/api/src/archive/video.ts index 8798d78..74a3863 100644 --- a/apps/api/src/archive/video.ts +++ b/apps/api/src/archive/video.ts @@ -29,6 +29,17 @@ export class VideoToolMissing extends Error { } } +/** + * Source has no usable video stream (audio-only, corrupt container, wrong extension). + * Indexing should mark FAILED without retry — ffmpeg `-map 0:v:0` cannot succeed. + */ +export class VideoUnsupported extends Error { + constructor(reason = "no_video_stream") { + super(reason); + this.name = "VideoUnsupported"; + } +} + export interface VideoTools { ffmpeg: string; ffprobe: string; @@ -40,6 +51,7 @@ export interface VideoInfo { height: number | null; capturedAt: Date | null; hasAudio: boolean; + hasVideo: boolean; } /** @@ -137,15 +149,22 @@ export function readProbe(json: ProbeJson): VideoInfo { height: video?.height ?? null, capturedAt, hasAudio: streams.some((s) => s.codec_type === "audio"), + hasVideo: !!video, }; } +/** Refuse sources with no video stream before ffmpeg `-map 0:v:0` (cryptic exit 234). */ +export function requireVideoStream(info: Pick): void { + if (!info.hasVideo) throw new VideoUnsupported(); +} + /** * The web copy, poster and thumbnail of `input`, written into `dir`. What it answers is read from the copy (its * size is the one shown, upright), with the original's camera time. */ export async function makeWebCopy(tools: VideoTools, input: string, dir: string): Promise { const original = await probe(tools, input); + requireVideoStream(original); const web = path.join(dir, VIDEO_FILES.web); await run(tools.ffmpeg, webCopyArgs(input, web, { audio: original.hasAudio }), "ffmpeg"); const copy = await probe(tools, web); -- 2.47.3