Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

fix(studio): sample preview video on the exact source frame

kashorinb778509d9e25
diff --git a/packages/studio/src/preview/runtime-shim.ts b/packages/studio/src/preview/runtime-shim.ts
index 877da92b..9f038e35 100644
--- a/packages/studio/src/preview/runtime-shim.ts
+++ b/packages/studio/src/preview/runtime-shim.ts
@@ -47,7 +47,12 @@ function shim(): string {
start: parseFloat(ownStart || (present && present.getAttribute('data-start')) || '0') || 0,
duration: parseFloat(ownDuration || (present && present.getAttribute('data-duration')) || '0') || 0,
mediaStart: parseFloat(element.getAttribute('data-media-start') || '0') || 0,
- rate: parseFloat(element.getAttribute('data-playback-rate') || '1') || 1
+ rate: parseFloat(element.getAttribute('data-playback-rate') || '1') || 1,
+ startFrame: Number(element.getAttribute('data-hypit-start-frame')),
+ endFrame: Number(element.getAttribute('data-hypit-end-frame')),
+ sourceFrame: element.getAttribute('data-hypit-source-frame').split('/').map(BigInt),
+ sourceRate: element.getAttribute('data-hypit-source-rate').split('/').map(BigInt),
+ sourceFps: element.getAttribute('data-hypit-source-fps').split('/').map(Number)
});
}
var audioContext;
@@ -149,7 +154,8 @@ function shim(): string {
}
for (var record of media) {
var local = currentSeconds - record.start;
- var inside = local >= 0 && local < record.duration;
+ var programFrame = Math.round(currentSeconds * fps);
+ var inside = programFrame >= record.startFrame && programFrame < record.endFrame;
var element = record.element;
element.style.visibility = inside ? 'visible' : 'hidden';
// Normalized picture Artifacts are deliberately silent. Programme sound
@@ -157,10 +163,18 @@ function shim(): string {
element.muted = true;
if (!inside) { element.pause(); continue; }
var target = record.mediaStart + (local + frameSeconds / 2) * record.rate;
+ var discrete = scrubbing || programFrame === record.endFrame - 1;
+ if (discrete) {
+ // The renderer floors exact source-frame coordinates. The midpoint of a
+ // programme frame can cross that source boundary at fractional speeds.
+ var a = record.sourceFrame, r = record.sourceRate;
+ var sourceFrame = Number((a[0] * r[1] + BigInt(programFrame - record.startFrame) * r[0] * a[1]) / (a[1] * r[1]));
+ target = (sourceFrame + 0.5) * record.sourceFps[1] / record.sourceFps[0];
+ }
if (Number.isFinite(element.duration) && element.duration > 0) {
target = Math.min(target, Math.max(0, element.duration - 0.001));
}
- if (scrubbing || local >= record.duration - frameSeconds - 0.000001) {
+ if (discrete) {
element.pause();
waits.push(seekDecoded(element, target));
} else {
diff --git a/packages/studio/test/video-preview.test.ts b/packages/studio/test/video-preview.test.ts
new file mode 100644
index 00000000..3c49088d
--- /dev/null
+++ b/packages/studio/test/video-preview.test.ts
@@ -0,0 +1,36 @@
+import assert from "node:assert/strict";
+import test from "node:test";
+import vm from "node:vm";
+import { injectRuntimeShim } from "../src/preview/runtime-shim.js";
+
+test("preview scrubs to the same source frame as rendering at fractional sample positions", async () => {
+ const attrs: Record<string, string> = {
+ "data-start": "0", "data-duration": "1", "data-media-start": String(1.75 / 30), "data-playback-rate": "0.5",
+ "data-hypit-start-frame": "0", "data-hypit-end-frame": "30", "data-hypit-source-frame": "7/4",
+ "data-hypit-source-rate": "1/2", "data-hypit-source-fps": "30/1",
+ };
+ const listeners = new Map<string, () => void>();
+ let currentTime = 0;
+ const video = { style: {}, readyState: 4, duration: 10, paused: true,
+ getAttribute: (name: string) => attrs[name] ?? null, closest: () => null,
+ get currentTime() { return currentTime; },
+ set currentTime(value: number) { currentTime = value; queueMicrotask(() => listeners.get("seeked")?.()); },
+ pause() {}, play() { return Promise.resolve(); },
+ addEventListener(name: string, listener: () => void) { listeners.set(name, listener); },
+ removeEventListener(name: string) { listeners.delete(name); },
+ };
+ const root = { style: {}, getAttribute: (name: string) => ({ "data-fps": "30", "data-composition-id": "test" })[name] ?? null };
+ const window: Record<string, any> = { addEventListener() {}, dispatchEvent() {} };
+ const html = injectRuntimeShim("").trim();
+ vm.runInNewContext(html.slice("<script>".length, -"</script>".length), {
+ window, document: { querySelector: () => root,
+ querySelectorAll: (selector: string) => selector === ".hypit-visual-present video" ? [video] : [] },
+ CustomEvent: class {},
+ });
+ await window.__hypitFrameReady;
+ // Source positions are 1.75, 2.25, 2.75, 3.25. Each decoded frame owns [n,n+1).
+ for (const [frame, sourceFrame] of [[0, 1], [1, 2], [2, 2], [3, 3], [0, 1]] as const) {
+ await window.__hypitSeekFrame(frame);
+ assert.equal(video.currentTime, (sourceFrame + 0.5) / 30, `preview frame ${frame}`);
+ }
+});