|
| 1 | +import { spawnSync } from "child_process"; |
| 2 | +import { mkdtempSync, rmSync } from "fs"; |
| 3 | +import { tmpdir } from "os"; |
| 4 | +import { join } from "path"; |
| 5 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 6 | +import { getFfmpegBinary, getFfprobeBinary } from "../utils/ffmpegBinaries.js"; |
| 7 | +import { |
| 8 | + buildChunkStreamingArgs, |
| 9 | + spawnChunkStreamingEncoder, |
| 10 | + BT709_LIMITED, |
| 11 | +} from "./chunkStreamingEncoder.js"; |
| 12 | + |
| 13 | +const fps = { num: 30, den: 1 }; |
| 14 | + |
| 15 | +describe("buildChunkStreamingArgs", () => { |
| 16 | + it("copies an Annex-B stream, tags it BT.709 and runs no filter", () => { |
| 17 | + const args = buildChunkStreamingArgs({ fps }, "/tmp/out.mp4"); |
| 18 | + |
| 19 | + expect(args.slice(0, 6)).toEqual(["-f", "h264", "-framerate", "30", "-i", "-"]); |
| 20 | + expect(args).toContain("copy"); |
| 21 | + expect(args).not.toContain("-vf"); |
| 22 | + expect(args[args.indexOf("-bsf:v") + 1]).toBe( |
| 23 | + "h264_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1:video_full_range_flag=0", |
| 24 | + ); |
| 25 | + expect(args[args.indexOf("-color_range") + 1]).toBe("tv"); |
| 26 | + expect(args.at(-1)).toBe("/tmp/out.mp4"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("writes the range the page encoder reported", () => { |
| 30 | + const args = buildChunkStreamingArgs( |
| 31 | + { fps, colour: { ...BT709_LIMITED, fullRange: true } }, |
| 32 | + "o.mp4", |
| 33 | + ); |
| 34 | + |
| 35 | + expect(args[args.indexOf("-bsf:v") + 1]).toContain("video_full_range_flag=1"); |
| 36 | + expect(args[args.indexOf("-color_range") + 1]).toBe("pc"); |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +describe("spawnChunkStreamingEncoder", () => { |
| 41 | + let dir = ""; |
| 42 | + beforeEach(() => { |
| 43 | + dir = mkdtempSync(join(tmpdir(), "hf-chunk-enc-")); |
| 44 | + }); |
| 45 | + afterEach(() => rmSync(dir, { recursive: true, force: true })); |
| 46 | + |
| 47 | + it("muxes encoded chunks into an mp4 with one frame per tick and BT.709 tags", async () => { |
| 48 | + const source = spawnSync( |
| 49 | + getFfmpegBinary(), |
| 50 | + [ |
| 51 | + "-hide_banner", |
| 52 | + "-loglevel", |
| 53 | + "error", |
| 54 | + "-f", |
| 55 | + "lavfi", |
| 56 | + "-i", |
| 57 | + "testsrc=d=1:r=30:s=160x90", |
| 58 | + ].concat(["-c:v", "libx264", "-bf", "0", "-pix_fmt", "yuv420p", "-f", "h264", "-"]), |
| 59 | + { maxBuffer: 64 * 1024 * 1024 }, |
| 60 | + ); |
| 61 | + expect(source.status).toBe(0); |
| 62 | + const stream = source.stdout; |
| 63 | + const out = join(dir, "out.mp4"); |
| 64 | + |
| 65 | + const encoder = spawnChunkStreamingEncoder(out, { fps }); |
| 66 | + const third = Math.ceil(stream.length / 3); |
| 67 | + for (let at = 0; at < stream.length; at += third) { |
| 68 | + expect(await encoder.writeFrame(stream.subarray(at, at + third))).toBe(true); |
| 69 | + } |
| 70 | + const result = await encoder.close(); |
| 71 | + |
| 72 | + expect(result.success).toBe(true); |
| 73 | + const probe = spawnSync( |
| 74 | + getFfprobeBinary(), |
| 75 | + ["-v", "error", "-count_frames", "-select_streams", "v:0"] |
| 76 | + .concat([ |
| 77 | + "-show_entries", |
| 78 | + "stream=nb_read_frames,r_frame_rate,color_space,color_primaries,color_transfer,color_range", |
| 79 | + ]) |
| 80 | + .concat(["-of", "json", out]), |
| 81 | + { encoding: "utf-8" }, |
| 82 | + ); |
| 83 | + const stream0 = JSON.parse(probe.stdout).streams[0]; |
| 84 | + expect(stream0).toMatchObject({ |
| 85 | + nb_read_frames: "30", |
| 86 | + r_frame_rate: "30/1", |
| 87 | + color_space: "bt709", |
| 88 | + color_primaries: "bt709", |
| 89 | + color_transfer: "bt709", |
| 90 | + color_range: "tv", |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments