Skip to content

🎥 Make videos programmatically with React

split up filter logic

Jonny Burger3275ee48e250
diff --git a/packages/renderer/src/calculate-ffmpeg-filters.ts b/packages/renderer/src/calculate-ffmpeg-filters.ts
new file mode 100644
index 0000000000..d6b9412219
--- /dev/null
+++ b/packages/renderer/src/calculate-ffmpeg-filters.ts
@@ -0,0 +1,43 @@
+import {Internals} from 'remotion';
+import {AssetAudioDetails, Assets} from './assets/types';
+import {resolveAssetSrc} from './resolve-asset-src';
+
+export const calculateFfmpegFilters = ({
+ assetPositions,
+ assetAudioDetails,
+ fps,
+}: {
+ assetPositions: Assets;
+ assetAudioDetails: Map<string, AssetAudioDetails>;
+ fps: number;
+}) => {
+ return assetPositions
+ .map((asset, i) => {
+ const duration = (asset.duration / fps).toFixed(3); // in seconds with milliseconds level precision
+ const assetTrimLeft = (asset.trimLeft / fps).toFixed(3);
+ const assetTrimRight = ((asset.trimLeft + asset.duration) / fps).toFixed(
+ 3
+ );
+ const startInVideo = ((asset.startInVideo / 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);
+};
diff --git a/packages/renderer/src/resolve-asset-src.ts b/packages/renderer/src/resolve-asset-src.ts
new file mode 100644
index 0000000000..3c7961a529
--- /dev/null
+++ b/packages/renderer/src/resolve-asset-src.ts
@@ -0,0 +1,12 @@
+import url from 'url';
+
+export const resolveAssetSrc = (src: string) => {
+ if (!src.startsWith('file:')) {
+ return src;
+ }
+ const {protocol} = new URL(src);
+
+ if (protocol === 'file:') return url.fileURLToPath(src);
+
+ throw new TypeError(`Unexpected src ${src}`);
+};
diff --git a/packages/renderer/src/stitcher.ts b/packages/renderer/src/stitcher.ts
index 3e98066073..bd2201f51b 100644
--- a/packages/renderer/src/stitcher.ts
+++ b/packages/renderer/src/stitcher.ts
@@ -1,13 +1,13 @@
import execa from 'execa';
import fs from 'fs';
import {Codec, Internals, PixelFormat, RenderAssetInfo} from 'remotion';
-import url from 'url';
import {calculateAssetPositions} from './assets/calculate-asset-positions';
import {convertAssetsToFileUrls} from './assets/convert-assets-to-file-urls';
import {getAssetAudioDetails} from './assets/get-asset-audio-details';
-import {AssetAudioDetails} from './assets/types';
+import {calculateFfmpegFilters} from './calculate-ffmpeg-filters';
import {DEFAULT_IMAGE_FORMAT, ImageFormat} from './image-format';
import {parseFfmpegProgress} from './parse-ffmpeg-progress';
+import {resolveAssetSrc} from './resolve-asset-src';
import {validateFfmpeg} from './validate-ffmpeg';
const getCodecName = (codec: Codec): string => {
@@ -77,39 +77,11 @@ export const stitchFramesToVideo = async (options: {
parallelism: options.parallelism,
});
- const filters = assetPositions
- .map((asset, i) => {
- const duration = (asset.duration / options.fps).toFixed(3); // in seconds with milliseconds level precision
- const assetTrimLeft = (asset.trimLeft / options.fps).toFixed(3);
- const assetTrimRight = (
- (asset.trimLeft + 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 filters = calculateFfmpegFilters({
+ assetAudioDetails,
+ assetPositions,
+ fps: options.fps,
+ });
const ffmpegArgs = [
['-r', String(options.fps)],
@@ -160,14 +132,3 @@ export const stitchFramesToVideo = async (options: {
});
await task;
};
-
-const resolveAssetSrc = (src: string) => {
- if (!src.startsWith('file:')) {
- return src;
- }
- const {protocol} = new URL(src);
-
- if (protocol === 'file:') return url.fileURLToPath(src);
-
- throw new TypeError(`Unexpected src ${src}`);
-};