Skip to content

🎥 Make videos programmatically with React

`@remotion/media`: Fix `<Video>` not appearing in Studio

Jonny Burgera00b723864c8
diff --git a/packages/bugs/api/[v].ts b/packages/bugs/api/[v].ts
index eb0606b9ed..061f8638ba 100644
--- a/packages/bugs/api/[v].ts
+++ b/packages/bugs/api/[v].ts
@@ -7,6 +7,12 @@ type Bug = {
};
export const bugs: Bug[] = [
+ {
+ title: 'Severe issues with video. Upgrade to 4.0.402.',
+ description: 'Videos might not appear.',
+ link: 'https://remotion.dev/changelog',
+ versions: ['4.0.401'],
+ },
{
title: 'Broken Lambda',
description:
diff --git a/packages/bundler/src/index-html.ts b/packages/bundler/src/index-html.ts
index 5b639ee5fc..697850f6ca 100644
--- a/packages/bundler/src/index-html.ts
+++ b/packages/bundler/src/index-html.ts
@@ -77,6 +77,8 @@ export const indexHtml = ({
}
<script>window.remotion_projectName = ${JSON.stringify(projectName)};</script>
<script>window.remotion_publicPath = ${JSON.stringify(publicPath)};</script>
+ <script>window.remotion_audioEnabled = true;</script>
+ <script>window.remotion_videoEnabled = true;</script>
<script>window.remotion_renderDefaults = ${JSON.stringify(
renderDefaults,
)};</script>
diff --git a/packages/media/src/debug-overlay/preview-overlay.ts b/packages/media/src/debug-overlay/preview-overlay.ts
index 2560ef852a..7d9fc74542 100644
--- a/packages/media/src/debug-overlay/preview-overlay.ts
+++ b/packages/media/src/debug-overlay/preview-overlay.ts
@@ -11,8 +11,8 @@ export const drawPreviewOverlay = ({
videoIteratorManager,
}: {
context: OffscreenCanvasRenderingContext2D | CanvasRenderingContext2D;
- audioTime: number;
- audioContextState: AudioContextState;
+ audioTime: number | null;
+ audioContextState: AudioContextState | null;
audioSyncAnchor: number;
playing: boolean;
audioIteratorManager: AudioIteratorManager | null;
@@ -25,8 +25,10 @@ export const drawPreviewOverlay = ({
`Audio iterators created: ${audioIteratorManager?.getAudioIteratorsCreated()}`,
`Frames rendered: ${videoIteratorManager?.getFramesRendered()}`,
`Audio context state: ${audioContextState}`,
- `Audio time: ${(audioTime - audioSyncAnchor).toFixed(3)}s`,
- ];
+ audioTime
+ ? `Audio time: ${(audioTime - audioSyncAnchor).toFixed(3)}s`
+ : null,
+ ].filter(Boolean) as string[];
if (audioIteratorManager) {
const queuedPeriod = audioIteratorManager
@@ -36,7 +38,7 @@ export const drawPreviewOverlay = ({
const numberOfChunksAfterResuming = audioIteratorManager
?.getAudioBufferIterator()
?.getNumberOfChunksAfterResuming();
- if (queuedPeriod) {
+ if (queuedPeriod && audioTime) {
lines.push(
`Audio queued until: ${(queuedPeriod.until - (audioTime - audioSyncAnchor)).toFixed(3)}s`,
);
diff --git a/packages/media/src/media-player.ts b/packages/media/src/media-player.ts
index d2bbf61462..21538a97d7 100644
--- a/packages/media/src/media-player.ts
+++ b/packages/media/src/media-player.ts
@@ -35,7 +35,7 @@ export class MediaPlayer {
private globalPlaybackRate: number;
private audioStreamIndex: number;
- private sharedAudioContext: AudioContext;
+ private sharedAudioContext: AudioContext | null;
audioIteratorManager: AudioIteratorManager | null = null;
videoIteratorManager: VideoIteratorManager | null = null;
@@ -90,7 +90,7 @@ export class MediaPlayer {
canvas: HTMLCanvasElement | OffscreenCanvas | null;
src: string;
logLevel: LogLevel;
- sharedAudioContext: AudioContext;
+ sharedAudioContext: AudioContext | null;
loop: boolean;
trimBefore: number | undefined;
trimAfter: number | undefined;
@@ -270,7 +270,7 @@ export class MediaPlayer {
this.playbackRate * this.globalPlaybackRate,
);
- if (audioTrack) {
+ if (audioTrack && this.sharedAudioContext) {
this.audioIteratorManager = audioIteratorManager({
audioTrack,
delayPlaybackHandleIfNotPremounting:
@@ -372,7 +372,7 @@ export class MediaPlayer {
return;
}
- const currentPlaybackTime = this.getPlaybackTime();
+ const currentPlaybackTime = this.getAudioPlaybackTime();
if (currentPlaybackTime === newTime) {
return;
}
@@ -424,7 +424,10 @@ export class MediaPlayer {
});
}
- if (this.sharedAudioContext.state === 'suspended') {
+ if (
+ this.sharedAudioContext &&
+ this.sharedAudioContext.state === 'suspended'
+ ) {
await this.sharedAudioContext.resume();
}
@@ -517,13 +520,17 @@ export class MediaPlayer {
this.debugOverlay = debugOverlay;
}
- private updateAfterPlaybackRateChange(): void {
+ private updateAudioTimeAfterPlaybackRateChange(): void {
if (!this.audioIteratorManager) {
return;
}
+ if (!this.sharedAudioContext) {
+ return;
+ }
+
this.setPlaybackTime(
- this.getPlaybackTime(),
+ this.getAudioPlaybackTime(),
this.playbackRate * this.globalPlaybackRate,
);
@@ -543,12 +550,12 @@ export class MediaPlayer {
public setPlaybackRate(rate: number): void {
this.playbackRate = rate;
- this.updateAfterPlaybackRateChange();
+ this.updateAudioTimeAfterPlaybackRateChange();
}
public setGlobalPlaybackRate(rate: number): void {
this.globalPlaybackRate = rate;
- this.updateAfterPlaybackRateChange();
+ this.updateAudioTimeAfterPlaybackRateChange();
}
public setFps(fps: number): void {
@@ -590,11 +597,15 @@ export class MediaPlayer {
node: AudioBufferSourceNode,
mediaTimestamp: number,
) => {
- const currentTime = this.getPlaybackTime();
+ const currentTime = this.getAudioPlaybackTime();
const delayWithoutPlaybackRate = mediaTimestamp - currentTime;
const delay =
delayWithoutPlaybackRate / (this.playbackRate * this.globalPlaybackRate);
+ if (!this.sharedAudioContext) {
+ throw new Error('Shared audio context not found');
+ }
+
if (delay >= 0) {
node.start(this.sharedAudioContext.currentTime + delay);
} else {
@@ -602,7 +613,11 @@ export class MediaPlayer {
}
};
- private getPlaybackTime(): number {
+ private getAudioPlaybackTime(): number {
+ if (!this.sharedAudioContext) {
+ throw new Error('Shared audio context not found');
+ }
+
return calculatePlaybackTime({
audioSyncAnchor: this.audioSyncAnchor,
currentTime: this.sharedAudioContext.currentTime,
@@ -611,6 +626,10 @@ export class MediaPlayer {
}
private setPlaybackTime(time: number, playbackRate: number): void {
+ if (!this.sharedAudioContext) {
+ return;
+ }
+
this.audioSyncAnchor =
this.sharedAudioContext.currentTime - time / playbackRate;
}
@@ -626,8 +645,8 @@ export class MediaPlayer {
if (this.context && this.canvas) {
drawPreviewOverlay({
context: this.context,
- audioTime: this.sharedAudioContext.currentTime,
- audioContextState: this.sharedAudioContext.state,
+ audioTime: this.sharedAudioContext?.currentTime ?? null,
+ audioContextState: this.sharedAudioContext?.state ?? null,
audioSyncAnchor: this.audioSyncAnchor,
audioIteratorManager: this.audioIteratorManager,
playing: this.playing,
diff --git a/packages/media/src/video/video-for-preview.tsx b/packages/media/src/video/video-for-preview.tsx
index e6de1349ff..08d13776d0 100644
--- a/packages/media/src/video/video-for-preview.tsx
+++ b/packages/media/src/video/video-for-preview.tsx
@@ -174,7 +174,6 @@ const VideoForPreviewAssertedShowing: React.FC<VideoForPreviewProps> = ({
useEffect(() => {
if (!sharedAudioContext) return;
- if (!sharedAudioContext.audioContext) return;
try {
const player = new MediaPlayer({