Skip to content

🎥 Make videos programmatically with React

threshold of 1.5 seconds

JonnyBurgerbdfae6532f37
diff --git a/packages/video/src/extract-frame.ts b/packages/video/src/extract-frame.ts
index 42c2c4c65e..e30dd09522 100644
--- a/packages/video/src/extract-frame.ts
+++ b/packages/video/src/extract-frame.ts
@@ -42,11 +42,13 @@ export const extractAudio = async ({
src,
timeInSeconds,
durationInSeconds,
+ fps,
}: {
src: string;
timeInSeconds: number;
logLevel: LogLevel;
durationInSeconds: number;
+ fps: number;
}): Promise<PcmS16AudioData | null> => {
if (!sinkPromise[src]) {
sinkPromise[src] = getSinks(src);
@@ -58,21 +60,21 @@ export const extractAudio = async ({
return null;
}
- // TODO: Hardcodec
- const fps = 30;
-
// https://discord.com/channels/@me/1409810025844838481/1415028953093111870
// Audio frames might have dependencies on previous and next frames so we need to decode a bit more
- // and then discard it,
- const extraThreshold = 1 / fps;
+ // and then discard it.
+ // The worst case seems to be FLAC files with a 65'535 sample window, which would be 1486.0ms at 44.1Khz.
+ // So let's set a threshold of 1.5 seconds.
+
+ const extraThreshold = 1.5;
// Matroska timestamps are not accurate unless we start from the beginning
// So for matroska, we need to decode all samples :(
// https://github.com/Vanilagy/mediabunny/issues/105
const sampleIterator = audio.sampleSink.samples(
- isMatroska ? 0 : timeInSeconds - extraThreshold,
- timeInSeconds + durationInSeconds + extraThreshold,
+ isMatroska ? 0 : Math.max(0, timeInSeconds - extraThreshold),
+ timeInSeconds + durationInSeconds,
);
const samples: AudioSample[] = [];