Hacky demo changes
This commit is contained in:
parent
7cba369308
commit
ea7afd618c
39 changed files with 2041 additions and 745 deletions
58
video/src/video.ts
Normal file
58
video/src/video.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { execSync } from 'node:child_process';
|
||||
import { renameSync, statSync } from 'node:fs';
|
||||
import {
|
||||
MAX_DURATION_S,
|
||||
OUTPUT_FPS,
|
||||
RECORD_SCALE,
|
||||
VIDEO_SIZE,
|
||||
WEBM_BITRATE,
|
||||
} from './config.js';
|
||||
|
||||
const LEAD_IN_S = 0.12;
|
||||
|
||||
export function trimRecording(
|
||||
rawPath: string,
|
||||
trimmedPath: string,
|
||||
times: { recordStartMs: number; sceneStartMs: number; sceneEndMs: number }
|
||||
) {
|
||||
const sceneSpan = (times.sceneEndMs - times.sceneStartMs) / 1000;
|
||||
const trimStart = Math.max(
|
||||
0,
|
||||
(times.sceneStartMs - times.recordStartMs) / 1000 - LEAD_IN_S * RECORD_SCALE
|
||||
);
|
||||
const trimEnd = (times.sceneEndMs - times.recordStartMs) / 1000;
|
||||
const wallDuration = trimEnd - trimStart;
|
||||
const finalDuration = wallDuration / RECORD_SCALE;
|
||||
|
||||
if (finalDuration > MAX_DURATION_S) {
|
||||
console.log(
|
||||
`Scene output duration is ${finalDuration.toFixed(2)}s (guard ${MAX_DURATION_S.toFixed(2)}s); keeping the full take.`
|
||||
);
|
||||
}
|
||||
|
||||
const filter =
|
||||
`trim=start=${trimStart.toFixed(3)}:duration=${wallDuration.toFixed(3)},` +
|
||||
`setpts=(PTS-STARTPTS)/${RECORD_SCALE},fps=${OUTPUT_FPS},` +
|
||||
`trim=duration=${finalDuration.toFixed(3)},setpts=PTS-STARTPTS`;
|
||||
|
||||
// Keep trimming inside the filter graph: it is frame-accurate for WebM
|
||||
// without the keyframe leakage of input seeking or the post-speedup timing
|
||||
// ambiguity of output seeking.
|
||||
execSync(
|
||||
`ffmpeg -y -i "${rawPath}" -vf "${filter}" ` +
|
||||
`-fps_mode cfr -r ${OUTPUT_FPS} -c:v libvpx -b:v ${WEBM_BITRATE} -deadline good -cpu-used 5 ` +
|
||||
`"${trimmedPath}"`,
|
||||
{ stdio: 'inherit' }
|
||||
);
|
||||
|
||||
try {
|
||||
statSync(rawPath);
|
||||
renameSync(rawPath, rawPath + '.untrimmed');
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Wrote ${trimmedPath} (${finalDuration.toFixed(2)}s, scene=${(sceneSpan / RECORD_SCALE).toFixed(2)}s, scale=${RECORD_SCALE}, capture=${VIDEO_SIZE.width}x${VIDEO_SIZE.height})`
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue