|
1 | 1 | import { spawnSync } from "node:child_process"; |
2 | | -import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; |
| 2 | +import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync } from "node:fs"; |
3 | 3 | import { tmpdir } from "node:os"; |
4 | 4 | import { join } from "node:path"; |
5 | 5 | import { afterAll, beforeAll, describe, expect, it } from "bun:test"; |
6 | | -import type { ExtractedFrames, VideoElement } from "@hyperframes/engine"; |
7 | | -import { extractDeferredFrames } from "./deferredVideoFrames.js"; |
| 6 | +import { |
| 7 | + extractAllVideoFrames, |
| 8 | + type ExtractedFrames, |
| 9 | + type VideoElement, |
| 10 | +} from "@hyperframes/engine"; |
| 11 | +import { extractAllDeferredFrames, extractDeferredFrames } from "./deferredVideoFrames.js"; |
8 | 12 |
|
9 | 13 | const metadata = { |
10 | 14 | durationSeconds: 4, |
@@ -32,6 +36,11 @@ const deferredVideo = (videoId: string, totalFrames: number): ExtractedFrames => |
32 | 36 | deferredRange: { startTime: 1.5, durationSeconds: totalFrames / 30, format: "jpg" }, |
33 | 37 | }); |
34 | 38 |
|
| 39 | +const fullyExtracted = (videoId: string): ExtractedFrames => { |
| 40 | + const { deferredRange: _deferred, ...rest } = deferredVideo(videoId, 90); |
| 41 | + return rest; |
| 42 | +}; |
| 43 | + |
35 | 44 | const element = (id: string, start: number, end: number): VideoElement => ({ |
36 | 45 | id, |
37 | 46 | src: `${id}.mp4`, |
@@ -95,4 +104,48 @@ describe("extractDeferredFrames", () => { |
95 | 104 | await expect(extraction).rejects.toBe(missingError); |
96 | 105 | expect(reported).toEqual([{ videoId: "clip", index: 45 }]); |
97 | 106 | }); |
| 107 | + describe("extractAllDeferredFrames", () => { |
| 108 | + const framesOf = (dir: string) => |
| 109 | + new Map(readdirSync(dir).map((name) => [name, readFileSync(join(dir, name))] as const)); |
| 110 | + |
| 111 | + it("fills a deferred video in place with the frames a full extraction writes, byte for byte", async () => { |
| 112 | + const outputDir = join(root, "all", "clip"); |
| 113 | + mkdirSync(outputDir, { recursive: true }); |
| 114 | + const video: ExtractedFrames = { |
| 115 | + ...deferredVideo("clip", 90), |
| 116 | + srcPath: sourcePath, |
| 117 | + outputDir, |
| 118 | + deferredRange: { startTime: 0, durationSeconds: 3, format: "jpg" }, |
| 119 | + }; |
| 120 | + const untouched = fullyExtracted("whole"); |
| 121 | + untouched.framePaths.set(0, "/kept/frame_00001.jpg"); |
| 122 | + const entries = [video, untouched]; |
| 123 | + |
| 124 | + const progress: Array<[number, number]> = []; |
| 125 | + await extractAllDeferredFrames(entries, { |
| 126 | + fps, |
| 127 | + cfg: {}, |
| 128 | + onProgress: (done, total) => progress.push([done, total]), |
| 129 | + }); |
| 130 | + expect(progress).toEqual([[90, 90]]); |
| 131 | + |
| 132 | + expect(entries[0]).toBe(video); |
| 133 | + expect(video.deferredRange).toBeUndefined(); |
| 134 | + expect(video.outputDir).toBe(outputDir); |
| 135 | + expect(video.framePaths.size).toBe(90); |
| 136 | + expect([...video.framePaths.values()].every((path) => existsSync(path))).toBe(true); |
| 137 | + expect(untouched.framePaths.get(0)).toBe("/kept/frame_00001.jpg"); |
| 138 | + |
| 139 | + const reference = await extractAllVideoFrames( |
| 140 | + [{ ...element("ref", 0, 3), src: sourcePath, mediaStart: 0 }], |
| 141 | + root, |
| 142 | + { fps, outputDir: join(root, "all-reference") }, |
| 143 | + ); |
| 144 | + const expected = framesOf(reference.extracted[0]!.outputDir); |
| 145 | + const actual = framesOf(outputDir); |
| 146 | + expect([...actual.keys()]).toEqual([...expected.keys()]); |
| 147 | + for (const [name, bytes] of expected) |
| 148 | + expect(actual.get(name)?.equals(bytes), name).toBe(true); |
| 149 | + }, 60_000); |
| 150 | + }); |
98 | 151 | }); |
0 commit comments