🎥 Make videos programmatically with React
throw hard error if not seekable
Jonny Burger4207e50bb97e
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.tsindex 0041b3383b..ae050e4081 100644--- a/packages/core/src/use-media-playback.ts+++ b/packages/core/src/use-media-playback.ts@@ -1,5 +1,5 @@import type {RefObject} from 'react';-import { useContext, useEffect} from 'react';+import {useContext, useEffect} from 'react';import {useMediaStartsAt} from './audio/use-audio-frame';import {playAndHandleNotAllowedError} from './play-and-handle-not-allowed-error';import {TimelineContext, usePlayingState} from './timeline-position-state';@@ -68,7 +68,7 @@ export const useMediaPlayback = ({// If scrubbing around, adjust timing// or if time shift is bigger than 0.2secmediaRef.current.currentTime = shouldBeTime;- warnAboutNonSeekableMedia(mediaRef.current);+ warnAboutNonSeekableMedia(mediaRef.current, 'console');}if (!playing || absoluteFrame === 0) {diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsxindex abccc204fe..85f806f36b 100644--- a/packages/core/src/video/VideoForRendering.tsx+++ b/packages/core/src/video/VideoForRendering.tsx@@ -19,6 +19,7 @@ import {SequenceContext} from '../Sequence';import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-current-frame';import {useUnsafeVideoConfig} from '../use-unsafe-video-config';import {evaluateVolume} from '../volume-prop';+import {warnAboutNonSeekableMedia} from '../warn-about-non-seekable-media';import {getMediaTime} from './get-current-time';import type {RemotionVideoProps} from './props';@@ -140,6 +141,11 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<videoRef.current.addEventListener('seeked',() => {+ warnAboutNonSeekableMedia(+ videoRef.current as HTMLVideoElement,+ 'exception'+ );+if (window.navigator.platform.startsWith('Mac')) {// Improve me: This is ensures frame perfectness but slows down render.// Please see this issue for context: https://github.com/remotion-dev/remotion/issues/200@@ -170,7 +176,7 @@ const VideoForRenderingForwardFunction: React.ForwardRefRenderFunction<`The browser threw an error while playing the video: ${videoRef.current?.error?.message}`);} else {- throw new Error('The browser threw an errir');+ throw new Error('The browser threw an error');}},{once: true}diff --git a/packages/core/src/warn-about-non-seekable-media.ts b/packages/core/src/warn-about-non-seekable-media.tsindex 637429ca2d..45fac20581 100644--- a/packages/core/src/warn-about-non-seekable-media.ts+++ b/packages/core/src/warn-about-non-seekable-media.ts@@ -1,7 +1,11 @@const alreadyWarned: {[key: string]: boolean} = {};-export const warnAboutNonSeekableMedia = (ref: HTMLMediaElement) => {+export const warnAboutNonSeekableMedia = (+ ref: HTMLMediaElement,+ type: 'console' | 'exception'+) => {// Media is not loaded yet, but this does not yet mean something is wrong with the media+if (ref.seekable.length === 0) {return;}@@ -17,11 +21,14 @@ export const warnAboutNonSeekableMedia = (ref: HTMLMediaElement) => {const range = {start: ref.seekable.start(0), end: ref.seekable.end(0)};if (range.start === 0 && range.end === 0) {- console.error(- 'The media',- ref.src,- 'does not seem to support seeking. Remotion cannot properly handle it. Please see https://remotion.dev/docs/non-seekable-media for assistance.'- );+ const msg = `The media does not seem to support seeking. Remotion cannot properly handle it. Please see https://remotion.dev/docs/non-seekable-media for assistance. Source: ${ref.src}`;++ if (type === 'console') {+ console.error(msg);+ } else {+ throw new Error(msg);+ }+alreadyWarned[ref.src] = true;}};