|
| 1 | +import { mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +vi.mock("../utils/runFfmpeg.js", async (importOriginal) => { |
| 7 | + const real = await importOriginal<typeof import("../utils/runFfmpeg.js")>(); |
| 8 | + return { ...real, runFfmpeg: vi.fn(real.runFfmpeg) }; |
| 9 | +}); |
| 10 | +const probe = vi.hoisted(() => ({ hasVideoToolbox: true })); |
| 11 | +vi.mock("../utils/psnrFilterAvailability.js", async (importOriginal) => ({ |
| 12 | + ...(await importOriginal<typeof import("../utils/psnrFilterAvailability.js")>()), |
| 13 | + isFfmpegHwaccelAvailable: async () => probe.hasVideoToolbox, |
| 14 | +})); |
| 15 | + |
| 16 | +const { runFfmpeg } = await import("../utils/runFfmpeg.js"); |
| 17 | +const { extractVideoFramesRange } = await import("./videoFrameExtractor.js"); |
| 18 | + |
| 19 | +describe.skipIf(process.platform === "win32")("extraction decode on the Mac's media engine", () => { |
| 20 | + const dir = mkdtempSync(join(tmpdir(), "hf-extract-media-engine-")); |
| 21 | + const realPlatform = process.platform; |
| 22 | + const platform = Object.getOwnPropertyDescriptor(process, "platform")!; |
| 23 | + const clips = { h264: join(dir, "h264.mp4"), vp9: join(dir, "vp9.webm") }; |
| 24 | + |
| 25 | + beforeAll(async () => { |
| 26 | + for (const [clip, codec] of [ |
| 27 | + [clips.h264, ["-c:v", "libx264", "-preset", "ultrafast", "-pix_fmt", "yuv420p"]], |
| 28 | + [clips.vp9, ["-c:v", "libvpx-vp9", "-deadline", "realtime", "-pix_fmt", "yuv420p"]], |
| 29 | + ] as const) { |
| 30 | + const made = await runFfmpeg([ |
| 31 | + ...["-y", "-v", "error", "-f", "lavfi", "-i", "testsrc2=s=64x64:d=1:r=10", ...codec, clip], |
| 32 | + ]); |
| 33 | + expect(made.success, made.stderr).toBe(true); |
| 34 | + } |
| 35 | + }); |
| 36 | + |
| 37 | + beforeEach(() => { |
| 38 | + vi.mocked(runFfmpeg).mockClear(); |
| 39 | + probe.hasVideoToolbox = true; |
| 40 | + }); |
| 41 | + |
| 42 | + afterAll(() => rmSync(dir, { recursive: true, force: true })); |
| 43 | + |
| 44 | + /** Extracts on `os`, returning the extraction's ffmpeg args and frames. */ |
| 45 | + async function extractOn(os: NodeJS.Platform, clip: string, id: string) { |
| 46 | + Object.defineProperty(process, "platform", { ...platform, value: os }); |
| 47 | + let frames: string[] = []; |
| 48 | + try { |
| 49 | + const out = join(dir, id); |
| 50 | + await extractVideoFramesRange(clip, id, 0, 1, { fps: 10, outputDir: dir }, undefined, {}, out) |
| 51 | + .then( |
| 52 | + () => |
| 53 | + (frames = readdirSync(out) |
| 54 | + .sort() |
| 55 | + .map((name) => join(out, name))), |
| 56 | + ) |
| 57 | + .catch((error: unknown) => { |
| 58 | + // Off a Mac, an ffmpeg asked for VideoToolbox fails; the args are what is checked there. |
| 59 | + if (realPlatform === "darwin" || os !== "darwin") throw error; |
| 60 | + }); |
| 61 | + } finally { |
| 62 | + Object.defineProperty(process, "platform", platform); |
| 63 | + } |
| 64 | + const args = vi |
| 65 | + .mocked(runFfmpeg) |
| 66 | + .mock.calls.map(([callArgs]) => callArgs) |
| 67 | + .find((callArgs) => callArgs.includes(clip) && callArgs.includes("-vf"))!; |
| 68 | + return { args, frames }; |
| 69 | + } |
| 70 | + |
| 71 | + const hwaccel = (args: string[]) => |
| 72 | + args.includes("-hwaccel") ? args[args.indexOf("-hwaccel") + 1] : null; |
| 73 | + |
| 74 | + it("decodes on the media engine on macOS when this ffmpeg has VideoToolbox", async () => { |
| 75 | + const { args } = await extractOn("darwin", clips.h264, "mac"); |
| 76 | + expect(hwaccel(args)).toBe("videotoolbox"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("decodes in software when this ffmpeg has no VideoToolbox", async () => { |
| 80 | + probe.hasVideoToolbox = false; |
| 81 | + const { args } = await extractOn("darwin", clips.h264, "mac-no-vt"); |
| 82 | + expect(hwaccel(args)).toBeNull(); |
| 83 | + }); |
| 84 | + |
| 85 | + it("keeps the software decoder for a codec that may carry alpha", async () => { |
| 86 | + const { args } = await extractOn("darwin", clips.vp9, "mac-vp9"); |
| 87 | + expect(hwaccel(args)).toBeNull(); |
| 88 | + }); |
| 89 | + |
| 90 | + it("never asks for VideoToolbox off macOS", async () => { |
| 91 | + const { args } = await extractOn("linux", clips.h264, "linux"); |
| 92 | + expect(hwaccel(args)).toBeNull(); |
| 93 | + }); |
| 94 | + |
| 95 | + it.runIf(realPlatform === "darwin")("writes the same frames as software decode", async () => { |
| 96 | + const engine = await extractOn("darwin", clips.h264, "same-engine"); |
| 97 | + probe.hasVideoToolbox = false; |
| 98 | + const software = await extractOn("darwin", clips.h264, "same-software"); |
| 99 | + expect(hwaccel(engine.args)).toBe("videotoolbox"); |
| 100 | + expect(engine.frames).toHaveLength(10); |
| 101 | + expect(engine.frames.map((f) => readFileSync(f))).toEqual( |
| 102 | + software.frames.map((f) => readFileSync(f)), |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments