Skip to content

🎥 Make videos programmatically with React

Update extract-frame.ts

Jonny Burger6cc327349cbf
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index 5b5627ace7..10eecb116c 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -40,14 +40,21 @@ const extractFrameInternal = async ({
}: ExtractFrameParams): Promise<ExtractFrameResult> => {
const sink = await getSink(src, logLevel);
- const video = await sink.getVideo();
+ const [video, mediaDurationInSecondsRaw] = await Promise.all([
+ sink.getVideo(),
+ loop ? sink.getDuration() : Promise.resolve(null),
+ ]);
+
+ const mediaDurationInSeconds: number | null = loop
+ ? mediaDurationInSecondsRaw
+ : null;
if (video === 'no-video-track') {
throw new Error(`No video track found for ${src}`);
}
if (video === 'cannot-decode') {
- return {type: 'cannot-decode', durationInSeconds: await sink.getDuration()};
+ return {type: 'cannot-decode', durationInSeconds: mediaDurationInSeconds};
}
if (video === 'unknown-container-format') {
@@ -58,12 +65,6 @@ const extractFrameInternal = async ({
return {type: 'network-error'};
}
- let mediaDurationInSeconds: number | null = null;
-
- if (loop) {
- mediaDurationInSeconds = await sink.getDuration();
- }
-
const timeInSeconds = getTimeInSeconds({
loop,
mediaDurationInSeconds,