Skip to content

🎥 Make videos programmatically with React

fix two bugs in looping

JonnyBurgerfc0515cc2d30
diff --git a/packages/media/src/get-time-in-seconds.ts b/packages/media/src/get-time-in-seconds.ts
index af31a09445..6c021df61a 100644
--- a/packages/media/src/get-time-in-seconds.ts
+++ b/packages/media/src/get-time-in-seconds.ts
@@ -35,7 +35,7 @@ export const getTimeInSeconds = ({
}) / fps
: Infinity;
- const timeInSeconds = (unloopedTimeInSeconds / playbackRate) % loopDuration;
+ const timeInSeconds = (unloopedTimeInSeconds * playbackRate) % loopDuration;
return timeInSeconds + (trimBefore ?? 0) / fps;
};
diff --git a/packages/media/src/test/browser.test.ts b/packages/media/src/test/browser.test.ts
index 80489c11d2..c7a5fc7c8a 100644
--- a/packages/media/src/test/browser.test.ts
+++ b/packages/media/src/test/browser.test.ts
@@ -4,7 +4,7 @@ import {applyVolume} from '../convert-audiodata/apply-volume';
import {extractFrameAndAudio} from '../extract-frame-and-audio';
test('Should be able to extract a frame', async () => {
- await keyframeManager.clearAll();
+ await keyframeManager.clearAll('info');
const result = await extractFrameAndAudio({
src: '/bigbuckbunny.mp4',
@@ -54,7 +54,7 @@ test('Should be able to extract a frame', async () => {
});
test('Should be able to extract the last frame', async () => {
- await keyframeManager.clearAll();
+ await keyframeManager.clearAll('info');
const result = await extractFrameAndAudio({
src: '/bigbuckbunny.mp4',
@@ -95,7 +95,8 @@ test('Should be able to extract the last frame', async () => {
});
test('Should manage the cache', async () => {
- await keyframeManager.clearAll();
+ await keyframeManager.clearAll('info');
+
for (let i = 0; i < 50; i++) {
await extractFrameAndAudio({
src: `/bigbuckbunny.mp4?i=${i}`,
@@ -119,7 +120,7 @@ test('Should manage the cache', async () => {
});
test('Should be apply volume correctly', async () => {
- await keyframeManager.clearAll();
+ await keyframeManager.clearAll('info');
const result = await extractFrameAndAudio({
src: '/bigbuckbunny.mp4',
@@ -168,7 +169,8 @@ test('Should be apply volume correctly', async () => {
});
test('Should be able to loop', async () => {
- await keyframeManager.clearAll();
+ await keyframeManager.clearAll('info');
+
const result = await extractFrameAndAudio({
src: `/bigbuckbunny.mp4`,
timeInSeconds: 10000001,
diff --git a/packages/media/src/test/looping.test.ts b/packages/media/src/test/looping.test.ts
index 5cd81c3b86..e97a72b56a 100644
--- a/packages/media/src/test/looping.test.ts
+++ b/packages/media/src/test/looping.test.ts
@@ -9,7 +9,7 @@ const playbackRate = 2;
const loop = true;
test('when looping with a lot of concurrency, it must be frame-accurate', async () => {
- const letInputTimestamps = [198, 199, 200, 201, 202];
+ const letInputTimestamps = [197, 198, 199, 200, 201, 202];
const realTimestamps = [];
const outputTimestamps = [];
@@ -29,7 +29,7 @@ test('when looping with a lot of concurrency, it must be frame-accurate', async
const result = await extractFrame({
src: 'https://remotion.media/video.mp4',
timeInSeconds: timeInFrames / fps,
- logLevel: 'verbose',
+ logLevel: 'info',
loop,
trimAfter,
trimBefore,
@@ -41,6 +41,8 @@ test('when looping with a lot of concurrency, it must be frame-accurate', async
outputTimestamps.push(result.frame?.timestamp ?? 0);
}
- expect(realTimestamps).toEqual([7.96, 7.98, 4, 4.02, 4.04]);
- expect(outputTimestamps).toEqual([7.96, 7.98, 4, 4.02, 4.04]);
+ expect(realTimestamps).toEqual([
+ 7.76, 7.84, 7.92, 4, 4.079999999999998, 4.16,
+ ]);
+ expect(outputTimestamps).toEqual([7.76, 7.84, 7.92, 4, 4.08, 4.16]);
});
diff --git a/packages/media/src/video-extraction/get-frames-since-keyframe.ts b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
index 5b8046d80f..bff8194475 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -9,6 +9,7 @@ import {
VideoSampleSink,
WEBM,
} from 'mediabunny';
+import type {LogLevel} from 'remotion';
import {makeKeyframeBank} from './keyframe-bank';
import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';
@@ -142,10 +143,12 @@ export const getFramesSinceKeyframe = async ({
packetSink,
videoSampleSink,
startPacket,
+ logLevel,
}: {
packetSink: EncodedPacketSink;
videoSampleSink: VideoSampleSink;
startPacket: EncodedPacket;
+ logLevel: LogLevel;
}) => {
const nextKeyPacket = await packetSink.getNextKeyPacket(startPacket, {
verifyKeyPackets: true,
@@ -160,6 +163,7 @@ export const getFramesSinceKeyframe = async ({
startTimestampInSeconds: startPacket.timestamp,
endTimestampInSeconds: nextKeyPacket ? nextKeyPacket.timestamp : Infinity,
sampleIterator,
+ logLevel,
});
return keyframeBank;
diff --git a/packages/media/src/video-extraction/keyframe-bank.ts b/packages/media/src/video-extraction/keyframe-bank.ts
index f815b69999..e70d3e9d44 100644
--- a/packages/media/src/video-extraction/keyframe-bank.ts
+++ b/packages/media/src/video-extraction/keyframe-bank.ts
@@ -5,7 +5,7 @@ export type KeyframeBank = {
startTimestampInSeconds: number;
endTimestampInSeconds: number;
getFrameFromTimestamp: (timestamp: number) => Promise<VideoSample | null>;
- prepareForDeletion: () => Promise<void>;
+ prepareForDeletion: (logLevel: LogLevel) => Promise<void>;
deleteFramesBeforeTimestamp: ({
logLevel,
src,
@@ -33,11 +33,17 @@ export const makeKeyframeBank = ({
startTimestampInSeconds,
endTimestampInSeconds,
sampleIterator,
+ logLevel: parentLogLevel,
}: {
startTimestampInSeconds: number;
endTimestampInSeconds: number;
sampleIterator: AsyncGenerator<VideoSample, void, unknown>;
+ logLevel: LogLevel;
}) => {
+ Internals.Log.verbose(
+ {logLevel: parentLogLevel, tag: '@remotion/media'},
+ `Creating keyframe bank from ${startTimestampInSeconds}sec to ${endTimestampInSeconds}sec`,
+ );
const frames: Record<number, VideoSample> = {};
const frameTimestamps: number[] = [];
@@ -134,7 +140,11 @@ export const makeKeyframeBank = ({
return (await getFrameFromTimestamp(timestamp)) !== null;
};
- const prepareForDeletion = async () => {
+ const prepareForDeletion = async (logLevel: LogLevel) => {
+ Internals.Log.verbose(
+ {logLevel, tag: '@remotion/media'},
+ `Preparing for deletion of keyframe bank from ${startTimestampInSeconds}sec to ${endTimestampInSeconds}sec`,
+ );
// Cleanup frames that have been extracted that might not have been retrieved yet
const {value} = await sampleIterator.return();
if (value) {
@@ -163,7 +173,8 @@ export const makeKeyframeBank = ({
logLevel: LogLevel;
src: string;
}) => {
- for (const frameTimestamp of frameTimestamps) {
+ const deletedTimestamps = [];
+ for (const frameTimestamp of frameTimestamps.slice()) {
const isLast =
frameTimestamp === frameTimestamps[frameTimestamps.length - 1];
// Don't delete the last frame, since it may be the last one in the video!
@@ -181,13 +192,16 @@ export const makeKeyframeBank = ({
frameTimestamps.splice(frameTimestamps.indexOf(frameTimestamp), 1);
frames[frameTimestamp].close();
delete frames[frameTimestamp];
-
- Internals.Log.verbose(
- {logLevel, tag: '@remotion/media'},
- `Deleted frame ${frameTimestamp} for src ${src}`,
- );
+ deletedTimestamps.push(frameTimestamp);
}
}
+
+ if (deletedTimestamps.length > 0) {
+ Internals.Log.verbose(
+ {logLevel, tag: '@remotion/media'},
+ `Deleted frames ${deletedTimestamps.join(', ')} for src ${src} because it is lower than ${timestampInSeconds}. Remaining: ${frameTimestamps}`,
+ );
+ }
};
const getOpenFrameCount = () => {
diff --git a/packages/media/src/video-extraction/keyframe-manager.ts b/packages/media/src/video-extraction/keyframe-manager.ts
index 0ec97ae51c..00e3468914 100644
--- a/packages/media/src/video-extraction/keyframe-manager.ts
+++ b/packages/media/src/video-extraction/keyframe-manager.ts
@@ -99,7 +99,7 @@ export const makeKeyframeManager = () => {
await getTheKeyframeBankMostInThePast();
if (mostInThePastBank) {
- await mostInThePastBank.prepareForDeletion();
+ await mostInThePastBank.prepareForDeletion(logLevel);
delete sources[mostInThePastSrc][
mostInThePastBank.startTimestampInSeconds
];
@@ -142,7 +142,7 @@ export const makeKeyframeManager = () => {
const {endTimestampInSeconds, startTimestampInSeconds} = bank;
if (endTimestampInSeconds < threshold) {
- await bank.prepareForDeletion();
+ await bank.prepareForDeletion(logLevel);
Internals.Log.verbose(
{logLevel, tag: '@remotion/media'},
`[Video] Cleared frames for src ${src} from ${startTimestampInSeconds}sec to ${endTimestampInSeconds}sec`,
@@ -193,6 +193,7 @@ export const makeKeyframeManager = () => {
packetSink,
videoSampleSink,
startPacket,
+ logLevel,
});
addKeyframeBank({src, bank: newKeyframeBank, startTimestampInSeconds});
@@ -207,14 +208,12 @@ export const makeKeyframeManager = () => {
Internals.Log.verbose(
{logLevel, tag: '@remotion/media'},
- `Keyframe bank exists but frame at time ${timestamp} does not exist anymore. Only the following times are available:`,
- (await existingBank).endTimestampInSeconds,
- (await existingBank).startTimestampInSeconds,
+ `Keyframe bank exists but frame at time ${timestamp} does not exist anymore.`,
);
// Bank exists but frames have already been evicted!
// First delete it entirely
- await (await existingBank).prepareForDeletion();
+ await (await existingBank).prepareForDeletion(logLevel);
delete sources[src][startTimestampInSeconds];
// Then refetch
@@ -222,6 +221,7 @@ export const makeKeyframeManager = () => {
packetSink,
videoSampleSink,
startPacket,
+ logLevel,
});
addKeyframeBank({src, bank: replacementKeybank, startTimestampInSeconds});
@@ -261,7 +261,7 @@ export const makeKeyframeManager = () => {
return keyframeBank;
};
- const clearAll = async () => {
+ const clearAll = async (logLevel: LogLevel) => {
const srcs = Object.keys(sources);
for (const src of srcs) {
const banks = Object.keys(sources[src]);
@@ -270,7 +270,7 @@ export const makeKeyframeManager = () => {
const bank =
await sources[src][startTimeInSeconds as unknown as number];
- await bank.prepareForDeletion();
+ await bank.prepareForDeletion(logLevel);
delete sources[src][startTimeInSeconds as unknown as number];
}
}