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: exclude trailing WAV metadata from voice-over duration (#27)

Xinran Xiong2ddba704a4b3
diff --git a/.changeset/wav-data-duration.md b/.changeset/wav-data-duration.md
new file mode 100644
index 0000000..9f06ebc
--- /dev/null
+++ b/.changeset/wav-data-duration.md
@@ -0,0 +1,5 @@
+---
+"openfilm": patch
+---
+
+Studio: voice-over WAV durations no longer count metadata after the audio as extra playback time. Streamed and truncated files still use only the audio bytes available.
diff --git a/packages/openfilm/studio/server/providers/common.mjs b/packages/openfilm/studio/server/providers/common.mjs
index 860f3f5..0b8185c 100644
--- a/packages/openfilm/studio/server/providers/common.mjs
+++ b/packages/openfilm/studio/server/providers/common.mjs
@@ -179,7 +179,7 @@ export function wav(/** @type {Uint8Array} */ pcm, rate = 24000) {
return new Uint8Array(Buffer.concat([head, pcm]));
}
-/** How long a PCM WAV plays, in seconds, from its own header (the data that is there, whatever the header claims). */
+/** How long a PCM WAV plays, in seconds, excluding trailing chunks and capped by the data that is there. */
export function wavSeconds(/** @type {Uint8Array} */ bytes) {
const b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
if (b.length < 44 || b.toString('ascii', 0, 4) !== 'RIFF') return null;
@@ -188,7 +188,10 @@ export function wavSeconds(/** @type {Uint8Array} */ bytes) {
const id = b.toString('ascii', at, at + 4);
const size = b.readUInt32LE(at + 4);
if (id === 'fmt ') rate = b.readUInt32LE(at + 16);
- if (id === 'data') return rate ? round((b.length - at - 8) / rate) : null;
+ if (id === 'data') {
+ const there = b.length - at - 8;
+ return rate ? round((size && size < there ? size : there) / rate) : null;
+ }
at += 8 + size + (size & 1);
}
return null;
diff --git a/packages/openfilm/studio/server/providers/common.test.mjs b/packages/openfilm/studio/server/providers/common.test.mjs
index 5b7f7e6..85172b9 100644
--- a/packages/openfilm/studio/server/providers/common.test.mjs
+++ b/packages/openfilm/studio/server/providers/common.test.mjs
@@ -17,12 +17,28 @@ test('words from character timings: spaces split, punctuation stays, each Chines
test('a WAV of PCM plays as long as its samples, whatever its header claims', () => {
const file = wav(new Uint8Array(48000), 24000);
assert.equal(wavSeconds(file), 1);
- /* a streamed WAV says its data is endless */
- Buffer.from(file.buffer).writeUInt32LE(0xffffffff, 40);
- assert.equal(wavSeconds(file), 1);
+ /* streamed WAV writers use either size to mean unknown */
+ for (const size of [0, 0xffffffff]) {
+ Buffer.from(file.buffer).writeUInt32LE(size, 40);
+ assert.equal(wavSeconds(file), 1);
+ }
assert.equal(wavSeconds(new Uint8Array(10)), null);
});
+test('WAV duration excludes trailing metadata and is capped by the available samples', () => {
+ const audio = Buffer.from(wav(new Uint8Array(48000), 24000));
+ const metadata = Buffer.alloc(4808);
+ metadata.write('LIST', 0);
+ metadata.writeUInt32LE(4800, 4);
+ metadata.write('INFO', 8);
+ metadata.write('ICMT', 12);
+ metadata.writeUInt32LE(4788, 16);
+ const file = Buffer.concat([audio, metadata]);
+ file.writeUInt32LE(file.length - 8, 4);
+ assert.equal(wavSeconds(file), 1);
+ assert.equal(wavSeconds(audio.subarray(0, 44 + 24000)), 0.5);
+});
+
test('picture sizes from PNG and JPEG headers', () => {
const png = Buffer.alloc(33);
png.writeUInt32BE(0x89504e47, 0);