Skip to content

🎥 Make videos programmatically with React

simplify

Jonny Burgerb3d8060a4a55
diff --git a/packages/renderer/src/ffmpeg-flags.ts b/packages/renderer/src/ffmpeg-flags.ts
index 678c8c83de..f9d84f817c 100644
--- a/packages/renderer/src/ffmpeg-flags.ts
+++ b/packages/renderer/src/ffmpeg-flags.ts
@@ -4,10 +4,10 @@ import os from 'os';
import path from 'path';
import {_downloadFile} from './browser/BrowserFetcher';
import type {FfmpegExecutable} from './ffmpeg-executable';
-import {binaryExists, ffmpegInNodeModules} from './validate-ffmpeg';
+import {binaryExists} from './validate-ffmpeg';
let buildConfig: string | null = null;
-const listeners: Record<string, (() => void)[]> = {};
+const listeners: Record<string, ((path: string) => void)[]> = {};
const isDownloading: Record<string, boolean> = {};
export type FfmpegVersion = [number, number, number] | null;
@@ -40,7 +40,7 @@ const getFfmpegFolderName = (remotionRoot: string): string => {
const ffmpegBinaryPrefix = 'ffmpeg-';
const randomFfmpegRuntimeId = String(Math.random()).replace('0.', '');
-export const getFfmpegAbsolutePath = (remotionRoot: string): string => {
+export const ffmpegInNodeModules = (remotionRoot: string): string | null => {
const folderName = getFfmpegFolderName(remotionRoot);
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName);
@@ -71,6 +71,15 @@ export const getFfmpegAbsolutePath = (remotionRoot: string): string => {
return path.join(folderName, ffmpegInstalled);
}
+ return null;
+};
+
+const getFfmpegAbsolutePath = (remotionRoot: string): string => {
+ const folderName = getFfmpegFolderName(remotionRoot);
+ if (!fs.existsSync(folderName)) {
+ fs.mkdirSync(folderName);
+ }
+
if (os.platform() === 'win32') {
return path.resolve(
folderName,
@@ -128,40 +137,30 @@ export const getFfmpegVersion = async (
};
const waitForFfmpegToBeDownloaded = (url: string) => {
- if (!isDownloading[url]) {
- return Promise.resolve();
- }
-
- return new Promise<void>((resolve) => {
+ return new Promise<string>((resolve) => {
if (!listeners[url]) {
listeners[url] = [];
}
- listeners[url].push(resolve);
+ listeners[url].push((src) => resolve(src));
});
};
+const onProgress = (downloadedBytes: number, totalBytesToDownload: number) => {
+ console.log(
+ 'Downloading ffmpeg: ',
+ `${toMegabytes(downloadedBytes)}/${toMegabytes(totalBytesToDownload)}`
+ );
+};
+
export const downloadFfmpeg = async (
remotionRoot: string,
url: string
-): Promise<void> => {
+): Promise<string> => {
// implement callback instead
- const onProgress = (
- downloadedBytes: number,
- totalBytesToDownload: number
- ) => {
- console.log(
- 'Downloading ffmpeg: ',
- `${toMegabytes(downloadedBytes)}/${toMegabytes(totalBytesToDownload)}`
- );
- };
const destinationPath = getFfmpegAbsolutePath(remotionRoot);
- if (isDownloading[url]) {
- return waitForFfmpegToBeDownloaded(url);
- }
-
isDownloading[url] = true;
const totalBytes = await _downloadFile(url, destinationPath, onProgress);
onProgress(totalBytes, totalBytes);
@@ -174,8 +173,10 @@ export const downloadFfmpeg = async (
listeners[url] = [];
}
- listeners[url].forEach((listener) => listener());
+ listeners[url].forEach((listener) => listener(destinationPath));
listeners[url] = [];
+
+ return destinationPath;
};
export const getExecutableFfmpeg = async (
@@ -194,14 +195,17 @@ export const getExecutableFfmpeg = async (
const {url} = getFfmpegDownloadUrl();
- await waitForFfmpegToBeDownloaded(url);
+ if (isDownloading[url]) {
+ return waitForFfmpegToBeDownloaded(url);
+ }
+
+ const inNodeMod = ffmpegInNodeModules(remotionRoot);
- if (ffmpegInNodeModules(remotionRoot)) {
- return getFfmpegAbsolutePath(remotionRoot);
+ if (inNodeMod) {
+ return inNodeMod;
}
- await downloadFfmpeg(remotionRoot, url);
- return getFfmpegAbsolutePath(remotionRoot);
+ return downloadFfmpeg(remotionRoot, url);
};
function toMegabytes(bytes: number) {
diff --git a/packages/renderer/src/validate-ffmpeg.ts b/packages/renderer/src/validate-ffmpeg.ts
index 258d6476e3..57cecadcca 100644
--- a/packages/renderer/src/validate-ffmpeg.ts
+++ b/packages/renderer/src/validate-ffmpeg.ts
@@ -1,9 +1,9 @@
import execa from 'execa';
-import fs, {statSync} from 'fs';
+import {statSync} from 'fs';
import os from 'os';
import {
downloadFfmpeg,
- getFfmpegAbsolutePath,
+ ffmpegInNodeModules,
getFfmpegDownloadUrl,
} from './ffmpeg-flags';
@@ -41,11 +41,6 @@ export const binaryExists = async (
}
};
-export const ffmpegInNodeModules = (remotionRoot: string): boolean => {
- const expectedFfmpegPath = getFfmpegAbsolutePath(remotionRoot);
- return fs.existsSync(expectedFfmpegPath);
-};
-
const isHomebrewInstalled = (): Promise<boolean> => {
return binaryExists('brew', null);
};