🎥 Make videos programmatically with React
smoother
Jonny Burger314233100249
diff --git a/packages/cli/src/compositions.ts b/packages/cli/src/compositions.tsindex 1a8240b6fa..46de3acf5d 100644--- a/packages/cli/src/compositions.ts+++ b/packages/cli/src/compositions.ts@@ -112,4 +112,6 @@ export const listCompositionsCommand = async (remotionRoot: string) => {}).join('\n'));++ await RenderInternals.cleanDownloadMap(downloadMap);};diff --git a/packages/cli/src/render.tsx b/packages/cli/src/render.tsxindex f3abf7633c..c16af47156 100644--- a/packages/cli/src/render.tsx+++ b/packages/cli/src/render.tsx@@ -51,6 +51,8 @@ export const render = async (remotionRoot: string) => {await initializeRenderCli(remotionRoot, 'sequence');+ Log.verbose('Asset dirs', downloadMap.assetDir);+const {codec,proResProfile,@@ -341,6 +343,7 @@ export const render = async (remotionRoot: string) => {try {await RenderInternals.deleteDirectory(urlOrBundle);+ await RenderInternals.cleanDownloadMap(downloadMap);} catch (err) {Log.warn('Could not clean up directory.');Log.warn(err);diff --git a/packages/cli/src/still.ts b/packages/cli/src/still.tsindex e040332539..b217185680 100644--- a/packages/cli/src/still.ts+++ b/packages/cli/src/still.ts@@ -251,4 +251,5 @@ export const still = async (remotionRoot: string) => {Log.info('-', 'Output can be found at:');Log.info(chalk.cyan(`▶️ ${absoluteOutputLocation}`));await closeBrowserPromise;+ await RenderInternals.cleanDownloadMap(downloadMap);};diff --git a/packages/example/freeze.mp4 b/packages/example/freeze.mp4new file mode 100644index 0000000000..62e819549dBinary files /dev/null and b/packages/example/freeze.mp4 differdiff --git a/packages/renderer/src/assets/download-and-map-assets-to-file.ts b/packages/renderer/src/assets/download-and-map-assets-to-file.tsindex 4b8d4cc922..1191bbe2af 100644--- a/packages/renderer/src/assets/download-and-map-assets-to-file.ts+++ b/packages/renderer/src/assets/download-and-map-assets-to-file.ts@@ -177,7 +177,6 @@ export const downloadAsset = async ({}if (downloadMap.isDownloadingMap[src]?.[downloadDir]) {- console.log('TRIGGER WAIT', {src, downloadDir});return waitForAssetToBeDownloaded({downloadMap, src, downloadDir});}diff --git a/packages/renderer/src/assets/download-map.ts b/packages/renderer/src/assets/download-map.tsindex 419582d3dd..40a4a4dae8 100644--- a/packages/renderer/src/assets/download-map.ts+++ b/packages/renderer/src/assets/download-map.ts@@ -1,4 +1,7 @@+import fs, {mkdirSync} from 'fs';+import path from 'path';import type {TAsset} from 'remotion';+import {deleteDirectory} from '../delete-directory';import {tmpDir} from '../tmp-dir';type EncodingStatus =@@ -51,12 +54,12 @@ export type DownloadMap = {videoDurationResultCache: Record<string, VideoDurationResult>;durationOfAssetCache: Record<string, AudioChannelsAndDurationResultCache>;downloadDir: string;- complexFilterScript: string;preEncode: string;audioMixing: string;complexFilter: string;audioPreprocessing: string;stitchFrames: string;+ assetDir: string;};export type RenderAssetInfo = {@@ -66,7 +69,25 @@ export type RenderAssetInfo = {downloadMap: DownloadMap;};+const makeAndReturn = (dir: string, name: string) => {+ const p = path.join(dir, name);+ mkdirSync(p);+ return p;+};++const packageJsonPath = path.join(__dirname, '..', '..', 'package.json');++const packageJson = fs.existsSync(packageJsonPath)+ ? JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))+ : null;+export const makeDownloadMap = (): DownloadMap => {+ const dir = tmpDir(+ packageJson+ ? 'remotion-assets'+ : 'remotion-assets-' + packageJson.version.replace(/\./g, '-')+ );+return {isDownloadingMap: {},hasBeenDownloadedMap: {},@@ -78,12 +99,16 @@ export const makeDownloadMap = (): DownloadMap => {videoDurationResultCache: {},durationOfAssetCache: {},id: String(Math.random()),- downloadDir: tmpDir('remotion-assets-dir'),- complexFilter: tmpDir('remotion-complex-filter'),- complexFilterScript: tmpDir('remotion-complex-filter-script'),- preEncode: tmpDir('pre-encode'),- audioMixing: tmpDir('remotion-audio-mixing'),- audioPreprocessing: tmpDir('remotion-audio-preprocessing'),- stitchFrames: tmpDir('remotion-stitch-temp-dir'),+ assetDir: dir,+ downloadDir: makeAndReturn(dir, 'remotion-assets-dir'),+ complexFilter: makeAndReturn(dir, 'remotion-complex-filter'),+ preEncode: makeAndReturn(dir, 'pre-encode'),+ audioMixing: makeAndReturn(dir, 'remotion-audio-mixing'),+ audioPreprocessing: makeAndReturn(dir, 'remotion-audio-preprocessing'),+ stitchFrames: makeAndReturn(dir, 'remotion-stitch-temp-dir'),};};++export const cleanDownloadMap = (downloadMap: DownloadMap) => {+ return deleteDirectory(downloadMap.downloadDir);+};diff --git a/packages/renderer/src/create-ffmpeg-complex-filter.ts b/packages/renderer/src/create-ffmpeg-complex-filter.tsindex 7e9a1ae3e7..656c7a441d 100644--- a/packages/renderer/src/create-ffmpeg-complex-filter.ts+++ b/packages/renderer/src/create-ffmpeg-complex-filter.ts@@ -1,5 +1,3 @@-import fs from 'fs';-import path from 'path';import type {DownloadMap} from './assets/download-map';import {createFfmpegMergeFilter} from './create-ffmpeg-merge-filter';import {makeFfmpegFilterFile} from './ffmpeg-filter-file';@@ -22,10 +20,6 @@ export const createFfmpegComplexFilter = async (downloadMap);- const tempPath = downloadMap.complexFilterScript;- const filterFile = path.join(tempPath, 'complex-filter.txt');- await fs.promises.writeFile(filterFile, complexFilter);-return {complexFilterFlag: ['-filter_complex_script', file],cleanup,diff --git a/packages/renderer/src/get-compositions.ts b/packages/renderer/src/get-compositions.tsindex 5ac44fc8a2..2507ffe80a 100644--- a/packages/renderer/src/get-compositions.ts+++ b/packages/renderer/src/get-compositions.ts@@ -1,6 +1,6 @@import type {TCompMetadata} from 'remotion';import type {DownloadMap} from './assets/download-map';-import {makeDownloadMap} from './assets/download-map';+import {cleanDownloadMap, makeDownloadMap} from './assets/download-map';import type {BrowserExecutable} from './browser-executable';import type {BrowserLog} from './browser-log';import type {Browser} from './browser/Browser';@@ -133,6 +133,10 @@ export const getCompositions = async (cleanup();close?.();cleanupPageError();+ // Clean download map if it was not passed in+ if (!config?.downloadMap) {+ cleanDownloadMap(downloadMap);+ }});});};diff --git a/packages/renderer/src/index.ts b/packages/renderer/src/index.tsindex 0f20323dcc..1edc393b0f 100644--- a/packages/renderer/src/index.ts+++ b/packages/renderer/src/index.ts@@ -1,6 +1,6 @@import execa from 'execa';import {downloadFile} from './assets/download-file';-import {makeDownloadMap} from './assets/download-map';+import {cleanDownloadMap, makeDownloadMap} from './assets/download-map';import {DEFAULT_BROWSER} from './browser';import {DEFAULT_TIMEOUT} from './browser/TimeoutSettings';import {canUseParallelEncoding} from './can-use-parallel-encoding';@@ -166,4 +166,5 @@ export const RenderInternals = {validateEveryNthFrame,perf,makeDownloadMap,+ cleanDownloadMap,};diff --git a/packages/renderer/src/render-frames.ts b/packages/renderer/src/render-frames.tsindex f70f0b03e9..e33dec9b58 100644--- a/packages/renderer/src/render-frames.ts+++ b/packages/renderer/src/render-frames.ts@@ -5,7 +5,7 @@ import {Internals} from 'remotion';import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';import {downloadAndMapAssetsToFileUrl} from './assets/download-and-map-assets-to-file';import type {DownloadMap} from './assets/download-map';-import {makeDownloadMap} from './assets/download-map';+import {cleanDownloadMap, makeDownloadMap} from './assets/download-map';import {DEFAULT_BROWSER} from './browser';import type {BrowserExecutable} from './browser-executable';import type {BrowserLog} from './browser-log';@@ -484,6 +484,10 @@ export const renderFrames = (cleanup.forEach((c) => {c();});+ // Clean download map if it was not passed in+ if (!options.downloadMap) {+ cleanDownloadMap(downloadMap);+ }});});};diff --git a/packages/renderer/src/render-media.ts b/packages/renderer/src/render-media.tsindex 795e5753b7..a3f3f827a4 100644--- a/packages/renderer/src/render-media.ts+++ b/packages/renderer/src/render-media.ts@@ -6,7 +6,7 @@ import type {SmallTCompMetadata} from 'remotion';import {Internals} from 'remotion';import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';import type {DownloadMap} from './assets/download-map';-import {makeDownloadMap} from './assets/download-map';+import {cleanDownloadMap, makeDownloadMap} from './assets/download-map';import type {BrowserExecutable} from './browser-executable';import type {BrowserLog} from './browser-log';import type {Browser as PuppeteerBrowser} from './browser/Browser';@@ -371,6 +371,11 @@ export const renderMedia = ({) {fs.unlinkSync(preEncodedFileLocation);}++ // Clean download map if it was not passed in+ if (!options?.downloadMap) {+ cleanDownloadMap(downloadMap);+ }});return Promise.race([diff --git a/packages/renderer/src/render-still.ts b/packages/renderer/src/render-still.tsindex 66c514e9df..9e179ac940 100644--- a/packages/renderer/src/render-still.ts+++ b/packages/renderer/src/render-still.ts@@ -4,7 +4,7 @@ import type {SmallTCompMetadata} from 'remotion';import {Internals} from 'remotion';import type {RenderMediaOnDownload} from './assets/download-and-map-assets-to-file';import type {DownloadMap} from './assets/download-map';-import {makeDownloadMap} from './assets/download-map';+import {cleanDownloadMap, makeDownloadMap} from './assets/download-map';import {DEFAULT_BROWSER} from './browser';import type {BrowserExecutable} from './browser-executable';import type {Browser as PuppeteerBrowser} from './browser/Browser';@@ -255,7 +255,14 @@ export const renderStill = (options: RenderStillOptions): Promise<void> => {.then((res) => resolve(res)).catch((err) => reject(err))- .finally(() => close?.());+ .finally(() => {+ // Clean download map if it was not passed in+ if (!options?.downloadMap) {+ cleanDownloadMap(downloadMap);+ }++ return close?.();+ });});return Promise.race([diff --git a/packages/renderer/src/stitch-frames-to-video.ts b/packages/renderer/src/stitch-frames-to-video.tsindex caf5356205..3e0e890c1c 100644--- a/packages/renderer/src/stitch-frames-to-video.ts+++ b/packages/renderer/src/stitch-frames-to-video.ts@@ -109,8 +109,6 @@ const getAssetsData = async ({console.log('asset positions', assetPositions);}- const tempPath = downloadMap.audioMixing;-const preprocessProgress = new Array(assetPositions.length).fill(0);const updateProgress = () => {@@ -122,7 +120,7 @@ const getAssetsData = async ({const preprocessed = (await Promise.all(assetPositions.map(async (asset, index) => {- const filterFile = path.join(tempPath, `${index}.wav`);+ const filterFile = path.join(downloadMap.audioMixing, `${index}.wav`);const result = await preprocessAudioTrack({ffmpegExecutable: ffmpegExecutable ?? null,ffprobeExecutable: ffprobeExecutable ?? null,@@ -149,7 +147,7 @@ const getAssetsData = async ({downloadMap,});- deleteDirectory(tempPath);+ deleteDirectory(downloadMap.audioMixing);onProgress(1);@@ -385,6 +383,8 @@ export const spawnFfmpeg = async (return {task: task.then(() => {+ deleteDirectory(options.assetsInfo.downloadMap.audioPreprocessing);+if (tempFile === null) {return null;}