Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: propagate not being able to decode differently

JonnyBurger8c5e6ca51fa9
diff --git a/packages/media/src/audio-extraction/extract-audio.ts b/packages/media/src/audio-extraction/extract-audio.ts
index bf74319ec3..c45f013b82 100644
--- a/packages/media/src/audio-extraction/extract-audio.ts
+++ b/packages/media/src/audio-extraction/extract-audio.ts
@@ -27,7 +27,7 @@ export const extractAudio = async ({
data: PcmS16AudioData | null;
durationInSeconds: number | null;
}> => {
- const {audio, actualMatroskaTimestamps, isMatroska, getDuration} =
+ const {getAudio, actualMatroskaTimestamps, isMatroska, getDuration} =
await getSinkWeak(src, logLevel);
let duration: number | null = null;
@@ -35,10 +35,16 @@ export const extractAudio = async ({
duration = await getDuration();
}
- if (audio === null) {
+ const audio = await getAudio();
+
+ if (audio === 'no-audio-track') {
return {data: null, durationInSeconds: null};
}
+ if (audio === 'cannot-decode-audio') {
+ throw new Error(`Cannot decode audio track for ${src}`);
+ }
+
const timeInSeconds = loop
? unloopedTimeInSeconds % duration!
: unloopedTimeInSeconds;
diff --git a/packages/media/src/video-extraction/extract-frame.ts b/packages/media/src/video-extraction/extract-frame.ts
index 58145598eb..76c7e4ac6d 100644
--- a/packages/media/src/video-extraction/extract-frame.ts
+++ b/packages/media/src/video-extraction/extract-frame.ts
@@ -15,14 +15,18 @@ export const extractFrame = async ({
}) => {
const sink = await getSinkWeak(src, logLevel);
- const {video, getDuration} = sink;
+ const video = await sink.getVideo();
- if (video === null) {
+ if (video === 'no-video-track') {
throw new Error(`No video track found for ${src}`);
}
+ if (video === 'cannot-decode') {
+ throw new Error(`Cannot decode video track for ${src}`);
+ }
+
const timeInSeconds = loop
- ? unloopedTimeinSeconds % (await getDuration())
+ ? unloopedTimeinSeconds % (await sink.getDuration())
: unloopedTimeinSeconds;
const keyframeBank = await keyframeManager.requestKeyframeBank({
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 2ac2f1bc4c..1cc880ab57 100644
--- a/packages/media/src/video-extraction/get-frames-since-keyframe.ts
+++ b/packages/media/src/video-extraction/get-frames-since-keyframe.ts
@@ -11,6 +11,21 @@ import {
import {makeKeyframeBank} from './keyframe-bank';
import {rememberActualMatroskaTimestamps} from './remember-actual-matroska-timestamps';
+type VideoSinks = {
+ sampleSink: VideoSampleSink;
+ packetSink: EncodedPacketSink;
+};
+
+type AudioSinks = {
+ sampleSink: AudioSampleSink;
+};
+
+export type AudioSinkResult =
+ | AudioSinks
+ | 'no-audio-track'
+ | 'cannot-decode-audio';
+export type VideoSinkResult = VideoSinks | 'no-video-track' | 'cannot-decode';
+
export const getSinks = async (src: string) => {
const input = new Input({
formats: ALL_FORMATS,
@@ -18,24 +33,67 @@ export const getSinks = async (src: string) => {
});
const format = await input.getFormat();
+ const isMatroska = format === MATROSKA;
- const videoTrack = await input.getPrimaryVideoTrack();
+ const getVideoSinks = async (): Promise<VideoSinkResult> => {
+ const videoTrack = await input.getPrimaryVideoTrack();
+ if (!videoTrack) {
+ return 'no-video-track';
+ }
- const audioTrack = await input.getPrimaryAudioTrack();
- const isMatroska = format === MATROSKA;
+ const canDecode = await videoTrack.canDecode();
+
+ if (!canDecode) {
+ return 'cannot-decode';
+ }
+
+ return {
+ sampleSink: new VideoSampleSink(videoTrack),
+ packetSink: new EncodedPacketSink(videoTrack),
+ };
+ };
+
+ let videoSinksPromise: Promise<VideoSinkResult> | null = null;
+ const getVideoSinksPromise = () => {
+ if (videoSinksPromise) {
+ return videoSinksPromise;
+ }
+
+ videoSinksPromise = getVideoSinks();
+ return videoSinksPromise;
+ };
+
+ let audioSinksPromise: Promise<AudioSinkResult> | null = null;
+
+ const getAudioSinks = async (): Promise<AudioSinkResult> => {
+ const audioTrack = await input.getPrimaryAudioTrack();
+ if (!audioTrack) {
+ return 'no-audio-track';
+ }
+
+ const canDecode = await audioTrack.canDecode();
+
+ if (!canDecode) {
+ return 'cannot-decode-audio';
+ }
+
+ return {
+ sampleSink: new AudioSampleSink(audioTrack),
+ };
+ };
+
+ const getAudioSinksPromise = () => {
+ if (audioSinksPromise) {
+ return audioSinksPromise;
+ }
+
+ audioSinksPromise = getAudioSinks();
+ return audioSinksPromise;
+ };
return new WeakRef({
- video: videoTrack
- ? {
- sampleSink: new VideoSampleSink(videoTrack),
- packetSink: new EncodedPacketSink(videoTrack),
- }
- : null,
- audio: audioTrack
- ? {
- sampleSink: new AudioSampleSink(audioTrack),
- }
- : null,
+ getVideo: () => getVideoSinksPromise(),
+ getAudio: () => getAudioSinksPromise(),
actualMatroskaTimestamps: rememberActualMatroskaTimestamps(isMatroska),
isMatroska,
getDuration: () => input.computeDuration(),