Skip to content

🎥 Make videos programmatically with React

consolidation

Jonny Burger5f90e5694a2f
diff --git a/packages/cli/src/editor/helpers/get-timeline-sequence-layout.ts b/packages/cli/src/editor/helpers/get-timeline-sequence-layout.ts
index 05e9707121..0a05fd7ff2 100644
--- a/packages/cli/src/editor/helpers/get-timeline-sequence-layout.ts
+++ b/packages/cli/src/editor/helpers/get-timeline-sequence-layout.ts
@@ -1,4 +1,4 @@
-import {Internals, TComposition} from 'remotion';
+import {TComposition} from 'remotion';
import {TIMELINE_PADDING} from './timeline-layout';
export const SEQUENCE_BORDER_WIDTH = 1;
@@ -19,9 +19,7 @@ export const getTimelineSequenceLayout = ({
const maxMediaSequenceDuration = maxMediaDuration - startFrom;
const spatialDuration = Math.min(
maxMediaSequenceDuration,
- Internals.FEATURE_FLAG_V2_BREAKING_CHANGES
- ? durationInFrames - 1
- : durationInFrames
+ durationInFrames - 1
);
const lastFrame = (video.durationInFrames ?? 1) - 1;
diff --git a/packages/core/src/audio/AudioForDevelopment.tsx b/packages/core/src/audio/AudioForDevelopment.tsx
index c25664457e..a96112b376 100644
--- a/packages/core/src/audio/AudioForDevelopment.tsx
+++ b/packages/core/src/audio/AudioForDevelopment.tsx
@@ -1,12 +1,13 @@
import React, {useContext, useEffect, useMemo, useRef, useState} from 'react';
import {CompositionManager} from '../CompositionManager';
import {getAssetFileName} from '../get-asset-file-name';
-import {isApproximatelyTheSame} from '../is-approximately-the-same';
import {useNonce} from '../nonce';
import {SequenceContext} from '../sequencing';
-import {useMediaTagVolume} from '../sync-actual-volume';
import {TimelineContext, usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
+import {useMediaPlayback} from '../use-media-playback';
+import {useMediaTagVolume} from '../use-media-tag-volume';
+import {useSyncVolumeWithMediaTag} from '../use-sync-volume-with-media-tag';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
import {evaluateVolume} from '../volume-prop';
import {RemotionAudioProps} from './props';
@@ -38,18 +39,12 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
const actualVolume = useMediaTagVolume(audioRef);
- useEffect(() => {
- const userPreferredVolume = evaluateVolume({
- frame: audioFrame,
- volume,
- });
- if (
- !isApproximatelyTheSame(userPreferredVolume, actualVolume) &&
- audioRef.current
- ) {
- audioRef.current.volume = userPreferredVolume;
- }
- }, [actualVolume, audioFrame, frame, props.volume, volume]);
+ useSyncVolumeWithMediaTag({
+ audioFrame,
+ actualVolume,
+ volume,
+ mediaRef: audioRef,
+ });
useEffect(() => {
if (playing) {
@@ -126,36 +121,14 @@ export const AudioForDevelopment: React.FC<RemotionAudioProps> = (props) => {
nonce,
]);
- useEffect(() => {
- if (!audioRef.current) {
- throw new Error('No audio ref found');
- }
- if (!videoConfig) {
- throw new Error(
- 'No video config found. <Audio> must be placed inside a composition.'
- );
- }
- const shouldBeTime = frame / videoConfig.fps;
-
- const isTime = audioRef.current.currentTime;
- const timeShift = Math.abs(shouldBeTime - isTime);
- if (timeShift > 0.2 && !audioRef.current.ended) {
- console.log('Time has shifted by', timeShift, 'sec. Fixing...');
- // If scrubbing around, adjust timing
- // or if time shift is bigger than 0.2sec
- audioRef.current.currentTime = shouldBeTime;
- }
-
- if (!playing || absoluteFrame === 0) {
- // If scrubbing around, adjust timing
- // or if time shift is bigger than 0.2sec
- audioRef.current.currentTime = shouldBeTime;
- }
- if (audioRef.current.paused && !audioRef.current.ended && playing) {
- // Play audio
- audioRef.current.play();
- }
- }, [absoluteFrame, frame, playing, videoConfig]);
+ useMediaPlayback({
+ mediaRef: audioRef,
+ absoluteFrame,
+ frame,
+ playing,
+ videoConfig,
+ src: nativeProps.src,
+ });
return <audio ref={audioRef} {...nativeProps} />;
};
diff --git a/packages/core/src/feature-flags.ts b/packages/core/src/feature-flags.ts
index 3c5c78555b..6e351cd754 100644
--- a/packages/core/src/feature-flags.ts
+++ b/packages/core/src/feature-flags.ts
@@ -2,9 +2,3 @@
// does not yet work and downloading has a bug.
// Disabling this for now!
export const FEATURE_FLAG_FIREFOX_SUPPORT = false;
-
-// Disable this to enter V1 mode and disable breaking changes
-// Needed if making future 1.x releases
-// See here https://github.com/JonnyBurger/remotion/issues/185 what
-// the breaking changes are
-export const FEATURE_FLAG_V2_BREAKING_CHANGES = true;
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 213bb7ae7d..788bfd535e 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -44,10 +44,7 @@ import {
DEFAULT_WEBPACK_CACHE_ENABLED,
getWebpackCaching,
} from './config/webpack-caching';
-import {
- FEATURE_FLAG_FIREFOX_SUPPORT,
- FEATURE_FLAG_V2_BREAKING_CHANGES,
-} from './feature-flags';
+import {FEATURE_FLAG_FIREFOX_SUPPORT} from './feature-flags';
import {isAudioCodec} from './is-audio-codec';
import * as perf from './perf';
import {getCompositionName, getIsEvaluation, getRoot} from './register-root';
@@ -85,7 +82,6 @@ export const Internals = {
DEFAULT_PIXEL_FORMAT,
FEATURE_FLAG_FIREFOX_SUPPORT,
DEFAULT_WEBPACK_CACHE_ENABLED,
- FEATURE_FLAG_V2_BREAKING_CHANGES,
getBrowser,
DEFAULT_BROWSER,
getDefaultCrfForCodec,
diff --git a/packages/core/src/sequencing/index.tsx b/packages/core/src/sequencing/index.tsx
index ceadb68d58..bb0f0fd8b6 100644
--- a/packages/core/src/sequencing/index.tsx
+++ b/packages/core/src/sequencing/index.tsx
@@ -6,7 +6,6 @@ import React, {
useState,
} from 'react';
import {CompositionManager} from '../CompositionManager';
-import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';
import {getTimelineClipName} from '../get-timeline-clip-name';
import {useNonce} from '../nonce';
import {TimelineContext} from '../timeline-position-state';
@@ -139,10 +138,7 @@ export const Sequence: React.FC<{
]);
const endThreshold = (() => {
- if (FEATURE_FLAG_V2_BREAKING_CHANGES) {
- return actualFrom + durationInFrames - 1;
- }
- return actualFrom + durationInFrames;
+ return actualFrom + durationInFrames - 1;
})();
const content =
diff --git a/packages/core/src/use-media-playback.ts b/packages/core/src/use-media-playback.ts
new file mode 100644
index 0000000000..5f69535056
--- /dev/null
+++ b/packages/core/src/use-media-playback.ts
@@ -0,0 +1,56 @@
+import {RefObject} from 'react';
+import {VideoConfig} from './video-config';
+import {getCurrentTime} from './video/get-current-time';
+
+export const useMediaPlayback = ({
+ mediaRef,
+ src,
+ videoConfig,
+ frame,
+ playing,
+ absoluteFrame,
+ mediaType,
+}: {
+ mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>;
+ src: string | undefined;
+ videoConfig: VideoConfig | null;
+ frame: number;
+ playing: boolean;
+ absoluteFrame: number;
+ mediaType: 'audio' | 'video';
+}) => {
+ const tagName = mediaType === 'audio' ? '<Audio>' : '<Video>';
+ if (!mediaRef.current) {
+ throw new Error(`No ${mediaType} ref found`);
+ }
+ if (!videoConfig) {
+ throw new Error(
+ `No video config found. ${tagName} must be placed inside a composition.`
+ );
+ }
+ if (!src) {
+ throw new Error(`No 'src' attribute was passed to the ${tagName} element.`);
+ }
+ const shouldBeTime = getCurrentTime({
+ fps: videoConfig.fps,
+ frame,
+ src,
+ });
+
+ const isTime = mediaRef.current.currentTime;
+ const timeShift = Math.abs(shouldBeTime - isTime);
+ if (timeShift > 0.2 && !mediaRef.current.ended) {
+ console.log('Time has shifted by', timeShift, 'sec. Fixing...');
+ // If scrubbing around, adjust timing
+ // or if time shift is bigger than 0.2sec
+ mediaRef.current.currentTime = shouldBeTime;
+ }
+
+ if (!playing || absoluteFrame === 0) {
+ mediaRef.current.currentTime = shouldBeTime;
+ }
+ if (mediaRef.current.paused && !mediaRef.current.ended && playing) {
+ mediaRef.current.currentTime = shouldBeTime;
+ mediaRef.current.play();
+ }
+};
diff --git a/packages/core/src/use-sync-volume-with-media-tag.ts b/packages/core/src/use-sync-volume-with-media-tag.ts
new file mode 100644
index 0000000000..f04391d885
--- /dev/null
+++ b/packages/core/src/use-sync-volume-with-media-tag.ts
@@ -0,0 +1,28 @@
+import {RefObject, useEffect} from 'react';
+import {isApproximatelyTheSame} from './is-approximately-the-same';
+import {evaluateVolume, VolumeProp} from './volume-prop';
+
+export const useSyncVolumeWithMediaTag = ({
+ audioFrame,
+ actualVolume,
+ volume,
+ mediaRef,
+}: {
+ audioFrame: number;
+ actualVolume: number;
+ volume?: VolumeProp;
+ mediaRef: RefObject<HTMLVideoElement | HTMLAudioElement>;
+}) => {
+ useEffect(() => {
+ const userPreferredVolume = evaluateVolume({
+ frame: audioFrame,
+ volume,
+ });
+ if (
+ !isApproximatelyTheSame(userPreferredVolume, actualVolume) &&
+ mediaRef.current
+ ) {
+ mediaRef.current.volume = userPreferredVolume;
+ }
+ }, [actualVolume, audioFrame, mediaRef, volume]);
+};
diff --git a/packages/core/src/video/VideoForDevelopment.tsx b/packages/core/src/video/VideoForDevelopment.tsx
index adb5e73100..4434cff244 100644
--- a/packages/core/src/video/VideoForDevelopment.tsx
+++ b/packages/core/src/video/VideoForDevelopment.tsx
@@ -1,12 +1,10 @@
import React, {useEffect, useRef} from 'react';
-import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';
-import {isApproximatelyTheSame} from '../is-approximately-the-same';
-import {useMediaTagVolume} from '../sync-actual-volume';
import {usePlayingState} from '../timeline-position-state';
import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
+import {useMediaPlayback} from '../use-media-playback';
+import {useMediaTagVolume} from '../use-media-tag-volume';
+import {useSyncVolumeWithMediaTag} from '../use-sync-volume-with-media-tag';
import {useUnsafeVideoConfig} from '../use-unsafe-video-config';
-import {evaluateVolume} from '../volume-prop';
-import {getCurrentTime} from './get-current-time';
import {RemotionVideoProps} from './props';
export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
@@ -30,49 +28,22 @@ export const VideoForDevelopment: React.FC<RemotionVideoProps> = (props) => {
const actualVolume = useMediaTagVolume(videoRef);
- useEffect(() => {
- const userPreferredVolume = evaluateVolume({
- frame,
- volume,
- });
- if (
- !isApproximatelyTheSame(userPreferredVolume, actualVolume) &&
- videoRef.current
- ) {
- videoRef.current.volume = userPreferredVolume;
- }
- }, [actualVolume, frame, props.volume, volume]);
-
- useEffect(() => {
- if (!videoRef.current) {
- throw new Error('No video ref found');
- }
-
- if (!videoConfig) {
- throw new Error(
- 'No video config found. <Video> must be placed inside a composition.'
- );
- }
- const currentTime = (() => {
- if (FEATURE_FLAG_V2_BREAKING_CHANGES) {
- return getCurrentTime({
- fps: videoConfig.fps,
- frame,
- src: props.src as string,
- });
- }
- return frame / (1000 / videoConfig.fps);
- })();
-
- if (!playing || absoluteFrame === 0) {
- videoRef.current.currentTime = currentTime;
- }
-
- if (videoRef.current.paused && !videoRef.current.ended && playing) {
- videoRef.current.currentTime = currentTime;
- videoRef.current.play();
- }
- }, [frame, playing, videoConfig, absoluteFrame, props.src]);
+ useSyncVolumeWithMediaTag({
+ // TODO Switch to audioFrame
+ audioFrame: frame,
+ actualVolume,
+ volume,
+ mediaRef: videoRef,
+ });
+
+ useMediaPlayback({
+ mediaRef: videoRef,
+ absoluteFrame,
+ frame,
+ playing,
+ src: nativeProps.src,
+ videoConfig,
+ });
return <video ref={videoRef} {...nativeProps} />;
};
diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsx
index 72b6630017..7f6b500f11 100644
--- a/packages/core/src/video/VideoForRendering.tsx
+++ b/packages/core/src/video/VideoForRendering.tsx
@@ -1,7 +1,6 @@
import React, {useContext, useEffect, useMemo, useRef} from 'react';
import {useAudioFrame} from '../audio/use-audio-frame';
import {CompositionManager} from '../CompositionManager';
-import {FEATURE_FLAG_V2_BREAKING_CHANGES} from '../feature-flags';
import {isApproximatelyTheSame} from '../is-approximately-the-same';
import {isRemoteAsset} from '../is-remote-asset';
import {random} from '../random';
@@ -90,14 +89,11 @@ export const VideoForRendering: React.FC<RemotionVideoProps> = ({
}
const currentTime = (() => {
- if (FEATURE_FLAG_V2_BREAKING_CHANGES) {
- return getCurrentTime({
- fps: videoConfig.fps,
- frame: audioFrame,
- src: props.src as string,
- });
- }
- return audioFrame / (1000 / videoConfig.fps);
+ return getCurrentTime({
+ fps: videoConfig.fps,
+ frame: audioFrame,
+ src: props.src as string,
+ });
})();
const handle = delayRender();
if (process.env.NODE_ENV === 'test') {
diff --git a/packages/core/src/video/get-current-time.ts b/packages/core/src/video/get-current-time.ts
index 0bff2eb4bb..e945788c81 100644
--- a/packages/core/src/video/get-current-time.ts
+++ b/packages/core/src/video/get-current-time.ts
@@ -8,12 +8,17 @@ export const getCurrentTime = ({
src: string;
}) => {
if (src.endsWith('mp4')) {
- // In Chrome, if 30fps, the first frame is still displayed at 0.033333
+ // In Chrome, for MP4s, if 30fps, the first frame is still displayed at 0.033333
// even though after that it increases by 0.033333333 each.
// So frame = 0 in Remotion is like frame = 1 for the browser
return (frame + 1) / fps;
}
- const msPerFrame = 1000 / fps;
- const msShift = msPerFrame / 2;
- return (frame * msPerFrame + msShift) / 1000;
+ if (src.endsWith('webm')) {
+ // For WebM videos, we need to add a little bit of shift to get the right frame.
+ const msPerFrame = 1000 / fps;
+ const msShift = msPerFrame / 2;
+ return (frame * msPerFrame + msShift) / 1000;
+ }
+ // For audio, we don't do any shift correction
+ return frame / fps;
};