Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/engine/src/services/streamingEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ export function buildStreamingArgs(
);
args.push(
"-filter_complex",
`${video};[0:v]format=rgba[o];[v][o]overlay=format=gbrp,${tail}[vout]`,
`${video};[0:v]format=rgba[o];[v][o]overlay=eof_action=endall:format=gbrp,${tail}[vout]`,
Comment thread
heygengenesis[bot] marked this conversation as resolved.
Resolved
"-map",
"[vout]",
);
Expand Down
8 changes: 5 additions & 3 deletions packages/engine/src/services/videoFrameExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,8 @@ export interface VideoLayerInput {
filters: string[];
}

const VIDEO_LAYER_TAIL_SLACK_SECONDS = 0.5;

/** ffmpeg input and filters showing the same frames and colour as extraction; undefined if unsupported. */
export async function buildVideoLayerInput(
videoPath: string,
Expand All @@ -1219,7 +1221,8 @@ export async function buildVideoLayerInput(
): Promise<VideoLayerInput | undefined> {
const metadata = await extractMediaMetadata(videoPath);
if (
startTime + duration > resolvePlayableVideoDuration(metadata) + 1e-3 ||
startTime + duration >
resolvePlayableVideoDuration(metadata) + VIDEO_LAYER_TAIL_SLACK_SECONDS ||
isHdrColorSpaceUtil(metadata.colorSpace) ||
metadata.isVFR ||
metadata.hasAlpha ||
Expand All @@ -1243,6 +1246,7 @@ export async function buildVideoLayerInput(
: `scale=${canvas.width}:${canvas.height}:flags=bicubic`,
);
}
filters.push(`tpad=stop_mode=clone:stop_duration=${VIDEO_LAYER_TAIL_SLACK_SECONDS * 2}`);
return {
inputArgs: [
"-noaccurate_seek",
Expand All @@ -1251,8 +1255,6 @@ export async function buildVideoLayerInput(
...urlInputArgs(videoPath),
"-i",
videoPath,
"-t",
formatFfmpegSeconds(duration),
],
filters,
};
Expand Down
69 changes: 69 additions & 0 deletions packages/engine/src/services/videoLayerInput.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { mkdtempSync, readFileSync, readdirSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";
import { toFps } from "@hyperframes/core";
import { buildVideoLayerInput } from "./videoFrameExtractor.js";
import { spawnStreamingEncoder } from "./streamingEncoder.js";

const HAS_FFMPEG = spawnSync("ffmpeg", ["-version"]).status === 0;

describe.skipIf(!HAS_FFMPEG)(
"buildVideoLayerInput on a film whose audio outlasts its video",
() => {
let dir = "";
let film = "";
beforeAll(() => {
dir = mkdtempSync(join(tmpdir(), "video-layer-input-"));
film = join(dir, "film.mp4");
const made = spawnSync("ffmpeg", [
...["-f", "lavfi", "-i", "testsrc=d=2:s=64x64:r=30"],
...["-f", "lavfi", "-i", "sine=d=2.4"],
...["-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", "-y", film],
]);
expect(made.status).toBe(0);
});
afterAll(() => rmSync(dir, { recursive: true, force: true }));

it("accepts it, holds the last frame and leaves the output length to the page frames", async () => {
const layer = await buildVideoLayerInput(film, 0, 2.4, toFps(30), { width: 64, height: 64 });
expect(layer).toBeDefined();
expect(layer?.inputArgs).not.toContain("-t");
expect(layer?.filters.at(-1)).toMatch(/^tpad=stop_mode=clone:stop_duration=/);
});
it("encodes exactly one output frame per page frame when the video is shorter", async () => {
const pageFrames = 74;
const framesDir = join(dir, "page");
spawnSync("mkdir", ["-p", framesDir]);
const drew = spawnSync("ffmpeg", [
...["-f", "lavfi", "-i", `color=c=red@0.5:s=64x64:r=30,format=rgba`],
...["-frames:v", String(pageFrames), "-y", join(framesDir, "p%03d.png")],
]);
expect(drew.status).toBe(0);
const layer = await buildVideoLayerInput(film, 0, 2.4387, toFps(30), {
width: 64,
height: 64,
});
expect(layer).toBeDefined();
const out = join(dir, "out.mp4");
const encoder = await spawnStreamingEncoder(out, {
fps: toFps(30),
width: 64,
height: 64,
codec: "h264",
imageFormat: "png",
videoLayer: layer,
});
for (const name of readdirSync(framesDir).sort()) {
expect(await encoder.writeFrame(readFileSync(join(framesDir, name)))).toBe(true);
}
await encoder.close();
const counted = spawnSync("ffprobe", [
...["-v", "error", "-count_frames", "-select_streams", "v:0"],
...["-show_entries", "stream=nb_read_frames", "-of", "csv=p=0", "--", out],
]);
expect(Number(counted.stdout.toString().trim())).toBe(pageFrames);
});
},
);
Loading