Skip to content

🎥 Make videos programmatically with React

allow to trim audio

Jonny Burger66da155d68c1
diff --git a/packages/core/src/CompositionManager.tsx b/packages/core/src/CompositionManager.tsx
index 33003d8e11..b1e1d682b5 100644
--- a/packages/core/src/CompositionManager.tsx
+++ b/packages/core/src/CompositionManager.tsx
@@ -27,6 +27,7 @@ export type TAsset = {
type: 'audio';
src: string;
id: string;
+ sequenceFrame: number;
};
export type CompositionManagerContext = {
diff --git a/packages/core/src/audio/AudioForRendering.tsx b/packages/core/src/audio/AudioForRendering.tsx
index 50256bb246..51e2e38202 100644
--- a/packages/core/src/audio/AudioForRendering.tsx
+++ b/packages/core/src/audio/AudioForRendering.tsx
@@ -1,10 +1,11 @@
import React, {useContext, useEffect, useState} from 'react';
import {CompositionManager} from '../CompositionManager';
-import {useAbsoluteCurrentFrame} from '../use-frame';
+import {useAbsoluteCurrentFrame, useCurrentFrame} from '../use-frame';
import {RemotionAudioProps} from './props';
export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {
const absoluteFrame = useAbsoluteCurrentFrame();
+ const sequenceFrame = useCurrentFrame();
const {registerAsset, unregisterAsset} = useContext(CompositionManager);
const [id] = useState(() => String(Math.random()));
@@ -18,9 +19,17 @@ export const AudioForRendering: React.FC<RemotionAudioProps> = (props) => {
type: 'audio',
src: props.src,
id,
+ sequenceFrame,
});
return () => unregisterAsset(id);
- }, [props.src, registerAsset, absoluteFrame, id, unregisterAsset]);
+ }, [
+ props.src,
+ registerAsset,
+ absoluteFrame,
+ id,
+ unregisterAsset,
+ sequenceFrame,
+ ]);
return null;
};
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index f9db536ea6..91b580b6fc 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -1,5 +1,6 @@
import {
CompositionManager,
+ TAsset,
TCompMetadata,
TComposition,
TSequence,
@@ -74,4 +75,5 @@ export type {
TCompMetadata,
TSequence,
WebpackOverrideFn,
+ TAsset,
};
diff --git a/packages/example/src/AudioTesting/index.tsx b/packages/example/src/AudioTesting/index.tsx
index c50601f7c0..c99ae8b015 100644
--- a/packages/example/src/AudioTesting/index.tsx
+++ b/packages/example/src/AudioTesting/index.tsx
@@ -10,7 +10,7 @@ const AudioTesting: React.FC = () => {
const {durationInFrames} = useVideoConfig();
return (
<div>
- <Sequence from={0} durationInFrames={durationInFrames / 3}>
+ <Sequence from={0 - 2000} durationInFrames={durationInFrames / 3 + 2000}>
<Audio src={music} />
</Sequence>
<Sequence
diff --git a/packages/renderer/src/assets.ts b/packages/renderer/src/assets.ts
index 0b013b37f0..b8b4a1a6df 100644
--- a/packages/renderer/src/assets.ts
+++ b/packages/renderer/src/assets.ts
@@ -1,21 +1,32 @@
-type AssetMetadata = {
- type: 'audio';
- src: string;
+import {TAsset} from 'remotion';
+
+type UnsafeAsset = TAsset & {
+ startInVideo: number;
+ duration: number | null;
};
-export type Asset = AssetMetadata & {
- from: number;
- duration?: number;
+type MediaAsset = Omit<UnsafeAsset, 'duration'> & {
+ duration: number;
};
-export type Assets = Asset[];
+export type Assets = MediaAsset[];
-const areEqual = (a: AssetMetadata, b: AssetMetadata) => {
+const areEqual = (a: TAsset, b: TAsset) => {
return a.type === b.type && a.src === b.src;
};
-export const calculateAssetsPosition = (frames: AssetMetadata[][]) => {
- const assets: Assets = [];
+const findFrom = (target: TAsset[], asset: TAsset) => {
+ const index = target.findIndex((a) => areEqual(a, asset));
+ if (index === -1) {
+ return false;
+ } else {
+ target.splice(index, 1);
+ return true;
+ }
+};
+
+export const calculateAssetsPosition = (frames: TAsset[][]): Assets => {
+ const assets: UnsafeAsset[] = [];
for (let frame = 0; frame < frames.length; frame++) {
const prev = (frames[frame - 1] ?? []).slice();
@@ -23,29 +34,28 @@ export const calculateAssetsPosition = (frames: AssetMetadata[][]) => {
const next = (frames[frame + 1] ?? []).slice();
for (const asset of current) {
- const findFrom = (target: AssetMetadata[]) => {
- const index = target.findIndex((a) => areEqual(a, asset));
- if (index === -1) {
- return false;
- } else {
- target.splice(index, 1);
- return true;
- }
- };
-
- if (!findFrom(prev)) {
- assets.push({...asset, from: frame});
+ if (!findFrom(prev, asset)) {
+ assets.push({
+ ...asset,
+ duration: null,
+ startInVideo: frame,
+ });
}
- if (!findFrom(next)) {
+ if (!findFrom(next, asset)) {
const found = assets.find(
- (a) => a.duration == null && areEqual(a, asset)
+ (a) => a.duration === null && areEqual(a, asset)
);
if (!found) throw new Error('something wrong');
- found.duration = frame - found.from - 1;
+ found.duration = frame - found.startInVideo - 1;
}
}
}
- return assets;
+ for (const asset of assets) {
+ if (asset.duration === null) {
+ throw new Error('duration is unexpectedly null');
+ }
+ }
+ return assets as MediaAsset[];
};
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index 01d4bdc689..4cd98c6a38 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -79,14 +79,21 @@ export const stitchFramesToVideo = async (options: {
'-filter_complex',
[
...options.assets.map((asset, i) => {
- const duration = asset.duration
- ? (asset.duration / options.fps).toFixed(3)
- : undefined; // in secounds with millisecounds level precision
- const from = ((asset.from / options.fps) * 1000).toFixed(); // in milliseconds
+ const duration = (asset.duration / options.fps).toFixed(3); // in secounds with millisecounds level precision
+ const assetTrimLeft = (asset.sequenceFrame / options.fps).toFixed(3);
+ const assetTrimRight = (
+ (asset.sequenceFrame + asset.duration) /
+ options.fps
+ ).toFixed(3);
+ const startInVideo = (
+ (asset.startInVideo / options.fps) *
+ 1000
+ ).toFixed(); // in milliseconds
+
return [
`[${i + 1}:a]`,
- duration ? `atrim=0:${duration},` : '',
- `adelay=delays=${from}:all=1`,
+ duration ? `atrim=${assetTrimLeft}:${assetTrimRight},` : '',
+ `adelay=${startInVideo}`,
`[a${i + 1}]`,
].join('');
}),