Skip to content

🎥 Make videos programmatically with React

can handle muted videos

Jonny Burger86bc7b936be6
diff --git a/packages/core/src/internals.ts b/packages/core/src/internals.ts
index 243629deeb..59f98610ca 100644
--- a/packages/core/src/internals.ts
+++ b/packages/core/src/internals.ts
@@ -51,6 +51,7 @@ import * as perf from './perf';
import {getCompositionName, getIsEvaluation, getRoot} from './register-root';
import {RemotionRoot} from './RemotionRoot';
import * as Timeline from './timeline-position-state';
+import {truthy} from './truthy';
import {useUnsafeVideoConfig} from './use-unsafe-video-config';
import {useVideo} from './use-video';
@@ -92,6 +93,7 @@ export const Internals = {
validateSelectedPixelFormatAndCodecCombination,
validateFrameRange,
getWebpackCaching,
+ truthy,
};
export type {
diff --git a/packages/core/src/truthy.ts b/packages/core/src/truthy.ts
new file mode 100644
index 0000000000..e186dc6277
--- /dev/null
+++ b/packages/core/src/truthy.ts
@@ -0,0 +1,5 @@
+type Truthy<T> = T extends false | '' | 0 | null | undefined ? never : T; // from lodash
+
+export function truthy<T>(value: T): value is Truthy<T> {
+ return Boolean(value);
+}
diff --git a/packages/renderer/src/assets/assets.ts b/packages/renderer/src/assets/assets.ts
index 9d539b6a41..8e3ddf756a 100644
--- a/packages/renderer/src/assets/assets.ts
+++ b/packages/renderer/src/assets/assets.ts
@@ -12,7 +12,7 @@ type MediaAsset = Omit<UnsafeAsset, 'duration'> & {
duration: number;
};
-type AssetAudioDetails = {
+export type AssetAudioDetails = {
channels: number;
};
@@ -78,7 +78,13 @@ export async function getAudioChannels(path: string, cwd: string) {
.filter(Boolean) as string[];
const task = await execa('ffprobe', args, {cwd});
- return parseInt(task.stdout.replace('channels=', ''), 10);
+ const parsed = parseInt(task.stdout.replace('channels=', ''), 10);
+ // TODO: How to determine the amount of channels for video?
+ // Is it even necessary?
+ if (isNaN(parsed)) {
+ return 0;
+ }
+ return parsed;
}
export async function getAssetAudioDetails(options: {
diff --git a/packages/renderer/src/assets/map-localhost-to-file.ts b/packages/renderer/src/assets/map-localhost-to-file.ts
index 1cbbe38839..9aabc79363 100644
--- a/packages/renderer/src/assets/map-localhost-to-file.ts
+++ b/packages/renderer/src/assets/map-localhost-to-file.ts
@@ -12,6 +12,6 @@ export const mapLocalhostAssetToFile = ({
const newSrc = path.join(webpackBundle, pathname);
return {
...localhostAsset,
- src: 'file://' + newSrc,
+ src: newSrc,
};
};
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index 4282c49354..cca0c41ab5 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -2,7 +2,7 @@ import execa from 'execa';
import fs from 'fs';
import {Codec, Internals, PixelFormat} from 'remotion';
import url from 'url';
-import {Assets, getAssetAudioDetails} from './assets/assets';
+import {AssetAudioDetails, Assets, getAssetAudioDetails} from './assets/assets';
import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';
import {parseFfmpegProgress} from './parse-ffmpeg-progress';
import {validateFfmpeg} from './validate-ffmpeg';
@@ -69,6 +69,40 @@ export const stitchFramesToVideo = async (options: {
parallelism: options.parallelism,
});
+ const filters = options.assets
+ .map((asset, i) => {
+ const duration = (asset.duration / options.fps).toFixed(3); // in seconds with milliseconds 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
+ const audioDetails = assetAudioDetails.get(
+ resolveAssetSrc(asset.src)
+ ) as AssetAudioDetails;
+
+ if (audioDetails.channels === 0) {
+ return null;
+ }
+ const streamIndex = i + 1;
+ return {
+ filter: [
+ `[${streamIndex}:a]`,
+ duration ? `atrim=${assetTrimLeft}:${assetTrimRight},` : '',
+ `adelay=${new Array(audioDetails.channels)
+ .fill(startInVideo)
+ .join('|')}`,
+ `[a${streamIndex}]`,
+ ].join(''),
+ streamIndex,
+ };
+ })
+ .filter(Internals.truthy);
+
const ffmpegArgs = [
['-r', String(options.fps)],
['-f', 'image2'],
@@ -90,36 +124,13 @@ export const stitchFramesToVideo = async (options: {
: [
'-filter_complex',
[
- ...options.assets.map((asset, i) => {
- const duration = (asset.duration / options.fps).toFixed(3); // in seconds with milliseconds 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
- const audioDetails = assetAudioDetails.get(
- resolveAssetSrc(asset.src)
- );
-
- return [
- `[${i + 1}:a]`,
- duration ? `atrim=${assetTrimLeft}:${assetTrimRight},` : '',
- `adelay=${new Array(audioDetails!.channels)
- .fill(startInVideo)
- .join('|')}`,
- `[a${i + 1}]`,
- ].join('');
- }),
- `${options.assets.map((asset, i) => `[a${i + 1}]`).join('')}amix=${
- options.assets.length
- },dynaudnorm`,
- ].join(';'),
+ ...filters.map((f) => f.filter),
+ `${filters
+ .map((asset) => `[a${asset.streamIndex}]`)
+ .join('')}amix=${filters.length},dynaudnorm`,
+ ]
+ .filter(Boolean)
+ .join(';'),
],
'-shortest',
['-map', '0:v'],
@@ -143,9 +154,12 @@ export const stitchFramesToVideo = async (options: {
};
const resolveAssetSrc = (src: string) => {
+ if (!src.startsWith('file:')) {
+ return src;
+ }
const {protocol} = new URL(src);
if (protocol === 'file:') return url.fileURLToPath(src);
- return src;
+ throw new TypeError(`Unexpected src ${src}`);
};
diff --git a/packages/renderer/src/test/map-localhost-to-file.test.ts b/packages/renderer/src/test/map-localhost-to-file.test.ts
index f3ba1a7766..f8e97b74a4 100644
--- a/packages/renderer/src/test/map-localhost-to-file.test.ts
+++ b/packages/renderer/src/test/map-localhost-to-file.test.ts
@@ -15,7 +15,7 @@ test('Should correctly map a localhost URL to an asset', () => {
).toEqual({
type: 'video',
src:
- 'file:///var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/5f25ba62771d1f8195f858ec5ff8e8d6.mp4',
+ '/var/folders/hl/p8pg9kw15dbg3l7dbpn0scc80000gn/T/react-motion-graphicstoSTC7/5f25ba62771d1f8195f858ec5ff8e8d6.mp4',
id: '0.8331499681195862',
sequenceFrame: 2045,
});