Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Loop the fallback of `<OffthreadVideo>`

JonnyBurger129211b96ade
diff --git a/packages/core/src/audio/Audio.tsx b/packages/core/src/audio/Audio.tsx
index 7c6e63dc81..72efcd2de5 100644
--- a/packages/core/src/audio/Audio.tsx
+++ b/packages/core/src/audio/Audio.tsx
@@ -121,7 +121,7 @@ const AudioRefForwardingFunction: React.ForwardRefRenderFunction<
layout="none"
durationInFrames={calculateLoopDuration({
endAt: trimAfterValue,
- mediaDuration: duration,
+ mediaDurationInFrames: duration,
playbackRate: props.playbackRate ?? 1,
startFrom: trimBeforeValue,
})}
diff --git a/packages/core/src/calculate-loop.ts b/packages/core/src/calculate-loop.ts
index 53313b3df8..313ed5ab69 100644
--- a/packages/core/src/calculate-loop.ts
+++ b/packages/core/src/calculate-loop.ts
@@ -1,15 +1,15 @@
export const calculateLoopDuration = ({
endAt,
- mediaDuration,
+ mediaDurationInFrames,
playbackRate,
startFrom,
}: {
- mediaDuration: number;
+ mediaDurationInFrames: number;
playbackRate: number;
startFrom: number | undefined;
endAt: number | undefined;
}) => {
- let duration = mediaDuration;
+ let duration = mediaDurationInFrames;
// Account for endAt
if (typeof endAt !== 'undefined') {
diff --git a/packages/core/src/video/Video.tsx b/packages/core/src/video/Video.tsx
index f107dac04d..fe3f89226e 100644
--- a/packages/core/src/video/Video.tsx
+++ b/packages/core/src/video/Video.tsx
@@ -99,7 +99,7 @@ const VideoForwardingFunction: React.ForwardRefRenderFunction<
<Loop
durationInFrames={calculateLoopDuration({
endAt: trimAfterValue,
- mediaDuration,
+ mediaDurationInFrames: mediaDuration,
playbackRate: props.playbackRate ?? 1,
startFrom: trimBeforeValue,
})}
diff --git a/packages/media/src/extract-frame-and-audio.ts b/packages/media/src/extract-frame-and-audio.ts
index aa23828624..a61dee9020 100644
--- a/packages/media/src/extract-frame-and-audio.ts
+++ b/packages/media/src/extract-frame-and-audio.ts
@@ -47,33 +47,40 @@ export const extractFrameAndAudio = async ({
: null,
]);
- if (frame === 'cannot-decode') {
- return {type: 'cannot-decode', durationInSeconds};
+ if (frame?.type === 'cannot-decode') {
+ return {
+ type: 'cannot-decode',
+ durationInSeconds: frame.durationInSeconds,
+ };
}
- if (frame === 'unknown-container-format') {
+ if (frame?.type === 'unknown-container-format') {
return {type: 'unknown-container-format'};
}
if (audio === 'unknown-container-format') {
if (frame !== null) {
- frame?.close();
+ frame?.frame?.close();
}
return {type: 'unknown-container-format'};
}
if (audio === 'cannot-decode') {
- if (frame !== null) {
- frame?.close();
+ if (frame?.type === 'success' && frame.frame !== null) {
+ frame?.frame.close();
}
- return {type: 'cannot-decode', durationInSeconds};
+ return {
+ type: 'cannot-decode',
+ durationInSeconds:
+ frame?.type === 'success' ? frame.durationInSeconds : null,
+ };
}
return {
type: 'success',
- frame: frame?.toVideoFrame() ?? null,
+ frame: frame?.frame?.toVideoFrame() ?? null,
audio: audio?.data ?? null,
durationInSeconds: audio?.durationInSeconds ?? null,
};
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index 65ca001e7c..ae80f45605 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -3,6 +3,15 @@ import {type LogLevel} from 'remotion';
import {keyframeManager} from '../caches';
import {getSinkWeak} from '../get-sink-weak';
+type ExtractFrameResult =
+ | {
+ type: 'success';
+ frame: VideoSample | null;
+ durationInSeconds: number | null;
+ }
+ | {type: 'cannot-decode'; durationInSeconds: number | null}
+ | {type: 'unknown-container-format'};
+
export const extractFrame = async ({
src,
timeInSeconds: unloopedTimeinSeconds,
@@ -13,9 +22,7 @@ export const extractFrame = async ({
timeInSeconds: number;
logLevel: LogLevel;
loop: boolean;
-}): Promise<
- VideoSample | 'cannot-decode' | 'unknown-container-format' | null
-> => {
+}): Promise<ExtractFrameResult> => {
const sink = await getSinkWeak(src, logLevel);
const video = await sink.getVideo();
@@ -25,15 +32,23 @@ export const extractFrame = async ({
}
if (video === 'cannot-decode') {
- return 'cannot-decode';
+ return {type: 'cannot-decode', durationInSeconds: await sink.getDuration()};
}
if (video === 'unknown-container-format') {
- return 'unknown-container-format';
+ return {type: 'unknown-container-format'};
+ }
+
+ const durationInSeconds = await sink.getDuration();
+
+ if (loop && durationInSeconds === null) {
+ throw new Error(
+ `Could not determine duration of ${src}, but "loop" was set.`,
+ );
}
const timeInSeconds = loop
- ? unloopedTimeinSeconds % (await sink.getDuration())
+ ? unloopedTimeinSeconds % durationInSeconds!
: unloopedTimeinSeconds;
const keyframeBank = await keyframeManager.requestKeyframeBank({
@@ -45,10 +60,14 @@ export const extractFrame = async ({
});
if (!keyframeBank) {
- return null;
+ return {
+ type: 'success',
+ frame: null,
+ durationInSeconds: await sink.getDuration(),
+ };
}
const frame = await keyframeBank.getFrameFromTimestamp(timeInSeconds);
- return frame;
+ return {type: 'success', frame, durationInSeconds: await sink.getDuration()};
};
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 70d85de826..3dd7cd0d23 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -130,7 +130,14 @@ export const getSinks = async (src: string) => {
getAudio: (index: number) => getAudioSinksPromise(index),
actualMatroskaTimestamps: rememberActualMatroskaTimestamps(isMatroska),
isMatroska,
- getDuration: () => input.computeDuration(),
+ getDuration: async () => {
+ try {
+ const duration = await input.computeDuration();
+ return duration;
+ } catch {
+ return null;
+ }
+ },
});
};
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index 262b05c326..935153bf1d 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -357,7 +357,7 @@ const VideoForPreviewWithDuration: React.FC<
<Loop
durationInFrames={Internals.calculateLoopDuration({
endAt: trimAfter,
- mediaDuration,
+ mediaDurationInFrames: mediaDuration,
playbackRate: playbackRate ?? 1,
startFrom: trimBefore,
})}
diff --git a/packages/media/src/video/video-for-rendering.tsx b/packages/media/src/video/video-for-rendering.tsx
index 7507af5cc7..ea684be3e7 100644
--- a/packages/media/src/video/video-for-rendering.tsx
+++ b/packages/media/src/video/video-for-rendering.tsx
@@ -390,7 +390,8 @@ export const VideoForRendering: React.FC<InnerVideoProps> = ({
layout="none"
durationInFrames={calculateLoopDuration({
endAt: trimAfterValue,
- mediaDuration: replaceWithOffthreadVideo.durationInSeconds,
+ mediaDurationInFrames:
+ replaceWithOffthreadVideo.durationInSeconds * fps,
playbackRate,
startFrom: trimBeforeValue,
})}