🎥 Make videos programmatically with React
feat(audio-mute): added mute support for video + audio
Louie Colgan78346f2d194e
diff --git a/packages/core/src/audio/AudioForRendering.tsx b/packages/core/src/audio/AudioForRendering.tsxindex 51e2e38202..29a3e71f68 100644--- a/packages/core/src/audio/AudioForRendering.tsx+++ b/packages/core/src/audio/AudioForRendering.tsx@@ -15,6 +15,10 @@ export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {throw new Error('No src passed');}+ if (props.muted) {+ return;+ }+registerAsset({type: 'audio',src: props.src,@@ -23,6 +27,7 @@ export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {});return () => unregisterAsset(id);}, [+ props.muted,props.src,registerAsset,absoluteFrame,diff --git a/packages/core/src/video/VideoForRendering.tsx b/packages/core/src/video/VideoForRendering.tsxindex 2411324a23..9b0058107b 100644--- a/packages/core/src/video/VideoForRendering.tsx+++ b/packages/core/src/video/VideoForRendering.tsx@@ -25,14 +25,19 @@ export const VideoForRendering: React.FC<RemotionVideoProps> = ({throw new Error('No src passed');}+ if (props.muted) {+ return;+ }+registerAsset({type: 'video',src: props.src,id,sequenceFrame: frame,});+return () => unregisterAsset(id);- }, [props.src, registerAsset, id, unregisterAsset, frame]);+ }, [props.muted, props.src, registerAsset, id, unregisterAsset, frame]);useEffect(() => {if (!videoRef.current) {diff --git a/packages/example/src/AudioTesting/index.tsx b/packages/example/src/AudioTesting/index.tsxindex 3676fbf08b..4932c4607b 100644--- a/packages/example/src/AudioTesting/index.tsx+++ b/packages/example/src/AudioTesting/index.tsx@@ -1,6 +1,6 @@import {Sequence, useVideoConfig, Video} from 'remotion';// FIXME: commit sample movie-import movie from './movie.webm';+import movie from '../resources/movie.webm';const AudioTesting: React.FC = () => {const {durationInFrames} = useVideoConfig();diff --git a/packages/example/src/AudioTestingMute/index.tsx b/packages/example/src/AudioTestingMute/index.tsxnew file mode 100644index 0000000000..ccfe72386c--- /dev/null+++ b/packages/example/src/AudioTestingMute/index.tsx@@ -0,0 +1,32 @@+import React from 'react';+import {Sequence, useCurrentFrame, useVideoConfig, Video, Audio} from 'remotion';+// FIXME: commit sample movie + music+import movie from '../resources/movie.webm';+import music from '../resources/music.mp3';++const AudioTestingMute: React.FC = () => {+ const sequenceFrame = useCurrentFrame();+ const {durationInFrames, fps} = useVideoConfig();++ /**+ * Interleave music with the movie by muting each other+ * at certain points.+ */+ const getMuteState = React.useCallback((type: "movie" | "music") => {+ const muteParts = [+ { start: 1 * fps, end: 2 * fps },+ { start: 4 * fps, end: 5 * fps },+ ];+ const toMute = muteParts.some((mp) => sequenceFrame >= mp.start && sequenceFrame <= mp.end)+ return type == "movie" ? toMute : !toMute;+ }, [sequenceFrame, durationInFrames]);++ return (+ <Sequence from={0} durationInFrames={durationInFrames}>+ <Video src={movie} muted={getMuteState("movie")} />+ <Audio src={music} muted={getMuteState("music")} />+ </Sequence>+ );+};++export default AudioTestingMute;diff --git a/packages/example/src/Video.tsx b/packages/example/src/Video.tsxindex 1a42d00223..377b184f82 100644--- a/packages/example/src/Video.tsx+++ b/packages/example/src/Video.tsx@@ -145,6 +145,14 @@ export const Index: React.FC = () => {fps={30}durationInFrames={300}/>+ <Composition+ id="audio-testing-mute"+ lazyComponent={() => import('./AudioTestingMute')}+ width={1080}+ height={1080}+ fps={30}+ durationInFrames={300}+ /></>);};diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.tsindex 7f3d117395..ee6a4c223d 100644--- a/packages/renderer/src/stitcher.ts+++ b/packages/renderer/src/stitcher.ts@@ -81,7 +81,7 @@ export const stitchFramesToVideo = async (options: {'-filter_complex',[...options.assets.map((asset, i) => {- const duration = (asset.duration / options.fps).toFixed(3); // in secounds with millisecounds level precision+ const duration = (asset.duration / options.fps).toFixed(3); // in seconds with milliseconds level precisionconst assetTrimLeft = (asset.sequenceFrame / options.fps).toFixed(3);@@ -93,11 +93,11 @@ export const stitchFramesToVideo = async (options: {(asset.startInVideo / options.fps) *1000).toFixed(); // in milliseconds-+return [`[${i + 1}:a]`,duration ? `atrim=${assetTrimLeft}:${assetTrimRight},` : '',- `adelay=${startInVideo}`,+ `adelay=delays=${startInVideo}:all=1`,`[a${i + 1}]`,].join('');}),