Skip to content

The open source AI video agent: turns any coding agent into a video agent, with an editor to change anything it makes.

Studio: a typed timecode lands on the frame that shows it at 23.976, 29.97 and 59.94 (#53)

HEOJUNFOb138595d4c2d
diff --git a/.changeset/ntsc-typed-timecode.md b/.changeset/ntsc-typed-timecode.md
new file mode 100644
index 0000000..5b1417b
--- /dev/null
+++ b/.changeset/ntsc-typed-timecode.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+A timecode typed into the inspector at 23.976, 29.97 or 59.94 fps lands on the frame that shows that timecode, instead of the frame before it, and a typed `12f` counts frames at the real rate.
diff --git a/packages/openfilm/studio/ui/src/lib/field-input.test.ts b/packages/openfilm/studio/ui/src/lib/field-input.test.ts
index 02d1ee4..9dcf97b 100644
--- a/packages/openfilm/studio/ui/src/lib/field-input.test.ts
+++ b/packages/openfilm/studio/ui/src/lib/field-input.test.ts
@@ -1,6 +1,7 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { applyFieldInput, parseFieldInput } from './field-input.ts';
+import { formatTimecode, frameAt } from './timecode.ts';
test('a typed number, arithmetic and px', () => {
assert.deepEqual(parseFieldInput('240'), { set: 240 });
@@ -50,6 +51,20 @@ test('time: seconds, frames, ms and timecodes', () => {
assert.equal(parseFieldInput('00:02:15'), null, 'a timecode only in a time field');
});
+test('a typed timecode lands on the frame that shows it, at every rate', () => {
+ /* at 29.97 the 17th second starts 17 ms after 17 s: 17 + 0 / 29.97 was the frame shown as 00:00:16:29 */
+ const at = (raw: string, fps: number) => (parseFieldInput(raw, { time: true, fps }) as { set: number }).set;
+ assert.equal(formatTimecode(at('00:00:17:00', 29.97) * 1000, { fps: 29.97 }), '00:00:17:00');
+ for (const fps of [23.976, 24, 25, 29.97, 30, 59.94, 60]) {
+ for (let frame = 0; frame < 120 * fps; frame += 1) {
+ const shown = formatTimecode((frame * 1000) / (fps === Math.round(fps) ? fps : (Math.round(fps) * 1000) / 1001), { fps });
+ assert.equal(formatTimecode(at(shown, fps) * 1000, { fps }), shown, `${shown} at ${fps}`);
+ }
+ }
+ /* frames count at the real rate too: 30 frames at 29.97 are a whole frame past 1 s */
+ assert.equal(frameAt(at('30f', 29.97) * 1000, 29.97), 30);
+});
+
test('the field\'s own unit marks, and what is not understood', () => {
assert.deepEqual(parseFieldInput('45°', { units: ['°', 'deg'] }), { set: 45 });
assert.deepEqual(parseFieldInput('90deg', { units: ['°', 'deg'] }), { set: 90 });
diff --git a/packages/openfilm/studio/ui/src/lib/field-input.ts b/packages/openfilm/studio/ui/src/lib/field-input.ts
index 3d23541..3092a8b 100644
--- a/packages/openfilm/studio/ui/src/lib/field-input.ts
+++ b/packages/openfilm/studio/ui/src/lib/field-input.ts
@@ -2,12 +2,14 @@
* What a person types into a number field of the inspector, read as Figma and the editors read it: arithmetic
* (`1440/2`), a change of the value there (`+20`, `-20`, `*2`, `/2`), and units — `px` is dropped, `50%` is half of
* what the field measures against (the stage, for a place or a size), and a time takes seconds (`2.5s`), frames
- * (`12f`, at the timecode's 30 a second), milliseconds and timecodes (`00:00:02:15`).
+ * (`12f`, at the timecode's rate), milliseconds and timecodes (`00:00:02:15`, read as the timecode shows them).
*
* A leading `-` is a change, like `+`: "20 to the left". A negative value itself is typed in brackets, `(-20)`, or
* worked out, `0-20`.
*/
+import { exactRate, timecodeSeconds } from './timecode.ts';
+
/** A value to set, or a change of the value there (each of several selected things changes from its own). */
export type FieldInput = { set: number } | { rel: '+' | '-' | '*' | '/'; by: number };
@@ -56,7 +58,7 @@ function tokenize(raw: string, ctx: FieldContext, inScale: boolean): Token[] | n
if (parts.length > 4) return null;
const frames = parts.pop()!;
const [sec = 0, min = 0, hour = 0] = parts.reverse();
- value = hour * 3600 + min * 60 + sec + frames / fps;
+ value = timecodeSeconds(hour * 3600 + min * 60 + sec, frames, fps);
} else {
const n = Number(m[0]);
if (unit === 'px' || (unit && units.includes(unit))) value = n;
@@ -68,7 +70,7 @@ function tokenize(raw: string, ctx: FieldContext, inScale: boolean): Token[] | n
else return null;
} else if (unit === 's' || unit === 'ms' || unit === 'f') {
if (!ctx.time) return null;
- value = unit === 's' ? n : unit === 'ms' ? n / 1000 : n / fps;
+ value = unit === 's' ? n : unit === 'ms' ? n / 1000 : n / exactRate(fps);
} else value = n;
}
out.push({ kind: 'num', value });
diff --git a/packages/openfilm/studio/ui/src/lib/timecode.ts b/packages/openfilm/studio/ui/src/lib/timecode.ts
index d789ee6..cc75b5e 100644
--- a/packages/openfilm/studio/ui/src/lib/timecode.ts
+++ b/packages/openfilm/studio/ui/src/lib/timecode.ts
@@ -127,6 +127,16 @@ function clockOf(frame: number, fps: number): { secs: number; frames: number } {
return { secs, frames: frame - first };
}
+/**
+ * The time, in seconds, a timecode's second and frame name: clockOf's way back, so a typed `00:00:17:00` is the frame
+ * that shows as it. At 29.97 the 17th second's first frame starts 17 ms after 17 s; `17 + 0 / 29.97` lands a frame
+ * early, on the one that reads `00:00:16:29`.
+ */
+export function timecodeSeconds(secs: number, frames: number, fps = currentFps): number {
+ const rate = exactRate(fps);
+ return (Math.ceil(secs * rate - 1e-9) + frames) / rate;
+}
+
export function formatTimecode(ms: number, opts?: { hours?: boolean; fps?: number }): string {
const { secs, frames } = clockOf(frameAt(ms, opts?.fps), opts?.fps ?? currentFps);
const tail = `${pad(Math.floor((secs % 3600) / 60))}:${pad(secs % 60)}:${pad(frames)}`;