Skip to content

🎥 Make videos programmatically with React

Merge branch 'main' into ffmpeg-autoinstall

Patric Salvisberg7e4e096b9158
diff --cc packages/cli/src/index.tsx
index 3c0b8ecad8,b7f9c2c817..6d0cdd941a
--- a/packages/cli/src/index.tsx
+++ b/packages/cli/src/index.tsx
@@@ -44,9 -44,7 +43,7 @@@ export const cli = async () =>
process.exit(0);
}
- const remotionRoot = findRemotionRoot();
+ const remotionRoot = RenderInternals.findRemotionRoot();
- // To check node version and to warn if node version is <12.10.0
- checkNodeVersion();
if (command !== VERSIONS_COMMAND) {
await validateVersionsBeforeCommand(remotionRoot);
}
diff --cc packages/cli/src/lambda-command.ts
index 5c9b5db64c,d35696f730..1cd560507b
--- a/packages/cli/src/lambda-command.ts
+++ b/packages/cli/src/lambda-command.ts
@@@ -10,10 -10,10 +10,10 @@@ export const lambdaCommand = async (rem
});
const {LambdaInternals} = require(path);
- await LambdaInternals.executeCommand(parsedCli._.slice(1));
+ await LambdaInternals.executeCommand(parsedCli._.slice(1), remotionRoot);
process.exit(0);
} catch (err) {
- const manager = getPackageManager(remotionRoot);
+ const manager = getPackageManager(remotionRoot, undefined);
const installCommand =
manager === 'unknown' ? 'npm i' : manager.installCommand;
Log.error(err);
diff --cc packages/cli/src/render.tsx
index b38fa71a68,fc552a2c69..b62ccb5bf2
--- a/packages/cli/src/render.tsx
+++ b/packages/cli/src/render.tsx
@@@ -114,12 -112,14 +113,15 @@@ export const render = async (remotionRo
)
);
- Log.verbose('Browser executable: ', browserExecutable);
-
- await checkAndValidateFfmpegVersion({
+ const ffmpegVersion = await RenderInternals.getFfmpegVersion({
ffmpegExecutable,
+ remotionRoot,
});
+ Log.verbose(
+ 'FFMPEG Version:',
+ ffmpegVersion ? ffmpegVersion.join('.') : 'Built from source'
+ );
+ Log.verbose('Browser executable: ', browserExecutable);
const browserInstance = openBrowser(browser, {
browserExecutable,
diff --cc packages/renderer/src/ffmpeg-flags.ts
index a7c64da85e,d6f98e2fd9..9c48956eea
--- a/packages/renderer/src/ffmpeg-flags.ts
+++ b/packages/renderer/src/ffmpeg-flags.ts
@@@ -97,9 -29,8 +97,9 @@@ export const ffmpegHasFeature = async (
}: {
ffmpegExecutable: string | null;
feature: 'enable-gpl' | 'enable-libx265' | 'enable-libvpx';
+ remotionRoot: string;
}) => {
- if (!(await binaryExists('ffmpeg', ffmpegExecutable))) {
+ if (!binaryExists('ffmpeg', ffmpegExecutable)) {
return false;
}
@@@ -133,119 -58,52 +133,169 @@@ export const getFfmpegVersion = async
return parseFfmpegVersion(buildInfo);
};
+const waitForFfmpegToBeDownloaded = (url: string) => {
+ return new Promise<string>((resolve) => {
+ if (!listeners[url]) {
+ listeners[url] = [];
+ }
+
+ 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<string> => {
+ const destinationPath = getFfmpegAbsolutePath(remotionRoot);
+
+ console.log('DOWNLOAD', Math.random());
+ isDownloading[url] = true;
+ const totalBytes = await _downloadFile(url, destinationPath, onProgress);
+ onProgress(totalBytes, totalBytes);
+ if (os.platform() !== 'win32') {
+ fs.chmodSync(destinationPath, '777');
+ }
+
+ isDownloading[url] = false;
+ if (!listeners[url]) {
+ listeners[url] = [];
+ }
+
+ listeners[url].forEach((listener) => listener(destinationPath));
+ listeners[url] = [];
+
+ console.log({
+ destinationPath,
+ perm: fs.accessSync(destinationPath, fs.constants.X_OK),
+ stat: fs.statSync(destinationPath),
+ });
+
+ return destinationPath;
+};
+
+export const getExecutableFfmpeg = async (
+ ffmpegExecutable: FfmpegExecutable | null,
+ remotionRoot: string
+) => {
+ const exists = await binaryExists('ffmpeg', ffmpegExecutable);
+
+ if (exists) {
+ console.log('XXX get from exists');
+
+ if (ffmpegExecutable !== null) {
+ return ffmpegExecutable;
+ }
+
+ return 'ffmpeg';
+ }
+
+ const {url} = getFfmpegDownloadUrl();
+
+ if (isDownloading[url]) {
+ console.log('XXX get from waiting');
+ return waitForFfmpegToBeDownloaded(url);
+ }
+
+ const inNodeMod = ffmpegInNodeModules(remotionRoot);
+
+ if (inNodeMod) {
+ console.log('XXX get from node mode', inNodeMod);
+
+ return inNodeMod;
+ }
+
+ console.log('XXX get from DL');
+ return downloadFfmpeg(remotionRoot, url);
+};
+
+function toMegabytes(bytes: number) {
+ const mb = bytes / 1024 / 1024;
+ return `${Math.round(mb * 10) / 10} Mb`;
+}
+
+export const getFfmpegDownloadUrl = (): {
+ url: string;
+ contentLength: number;
+} => {
+ if (os.platform() === 'win32') {
+ return {
+ url: 'https://remotion-ffmpeg-binaries.s3.eu-central-1.amazonaws.com/ffmpeg-win-x86.exe',
+ contentLength: 127531008,
+ };
+ }
+
+ if (os.platform() === 'darwin') {
+ return process.arch === 'arm64'
+ ? {
+ url: 'https://remotion-ffmpeg-binaries.s3.eu-central-1.amazonaws.com/ffmpeg-macos-arm64',
+ contentLength: 42093320,
+ }
+ : {
+ url: 'https://remotion-ffmpeg-binaries.s3.eu-central-1.amazonaws.com/ffmpeg-macos-x86',
+ contentLength: 78380700,
+ };
+ }
+
+ return {
+ url: 'https://remotion-ffmpeg-binaries.s3.eu-central-1.amazonaws.com/ffmpeg-linux-amd64',
+ contentLength: 78502560,
+ };
+};
++
+ const printMessage = (ffmpegVersion: NonNullable<FfmpegVersion>) => {
+ console.warn('⚠️Old FFMPEG version detected: ' + ffmpegVersion.join('.'));
+ console.warn(' For audio support, you need at least version 4.1.0.');
+ console.warn(' Upgrade FFMPEG to get rid of this warning.');
+ };
+
+ const printBuildConfMessage = () => {
+ console.error('⚠️ Unsupported FFMPEG version detected.');
+ console.error(" Your version doesn't support the -buildconf flag");
+ console.error(
+ ' Audio will not be supported and you may experience other issues.'
+ );
+ console.error(
+ ' Upgrade FFMPEG to at least v4.1.0 to get rid of this warning.'
+ );
+ };
+
+ export const warnAboutFfmpegVersion = ({
+ ffmpegVersion,
+ buildConf,
+ }: {
+ ffmpegVersion: FfmpegVersion;
+ buildConf: string | null;
+ }) => {
+ if (buildConf === null) {
+ printBuildConfMessage();
+ return;
+ }
+
+ if (ffmpegVersion === null) {
+ return null;
+ }
+
+ const [major, minor] = ffmpegVersion;
+ // 3.x and below definitely is too old
+ if (major < 4) {
+ printMessage(ffmpegVersion);
+ return;
+ }
+
+ // 5.x will be all good
+ if (major > 4) {
+ return;
+ }
+
+ if (minor < 1) {
+ printMessage(ffmpegVersion);
+ }
+ };
diff --cc packages/renderer/src/index.ts
index c418027d7a,4b564d1c0c..b93784cbac
--- a/packages/renderer/src/index.ts
+++ b/packages/renderer/src/index.ts
@@@ -12,13 -12,7 +12,12 @@@ import {deleteDirectory} from './delete
import {ensureOutputDirectory} from './ensure-output-directory';
import {symbolicateError} from './error-handling/symbolicate-error';
import {SymbolicateableError} from './error-handling/symbolicateable-error';
-import {ffmpegHasFeature, getFfmpegVersion} from './ffmpeg-flags';
+import {
+ ffmpegHasFeature,
+ getExecutableFfmpeg,
- getFfmpegBuildInfo,
+ getFfmpegVersion,
+} from './ffmpeg-flags';
+import {findRemotionRoot} from './find-closest-package-json';
import {validateFrameRange} from './frame-range';
import {getActualConcurrency} from './get-concurrency';
import {getFramesToRender} from './get-duration-from-frame-range';
@@@ -169,9 -161,8 +166,10 @@@ export const RenderInternals =
makeDownloadMap,
cleanDownloadMap,
convertToPositiveFrameIndex,
+ findRemotionRoot,
+ getExecutableFfmpeg,
validateBitrate,
+ getFfmpegVersion,
};
// Warn of potential performance issues with Apple Silicon (M1 chip under Rosetta)
diff --cc packages/renderer/src/validate-ffmpeg.ts
index 36caed320f,e564d1896e..f89e0ab42c
--- a/packages/renderer/src/validate-ffmpeg.ts
+++ b/packages/renderer/src/validate-ffmpeg.ts
@@@ -1,11 -1,8 +1,14 @@@
import execa from 'execa';
import {statSync} from 'fs';
import os from 'os';
-import {getFfmpegBuildInfo, getFfmpegVersion} from './ffmpeg-flags';
+import {
+ downloadFfmpeg,
+ ffmpegInNodeModules,
++ getFfmpegBuildInfo,
+ getFfmpegDownloadUrl,
++ getFfmpegVersion,
+} from './ffmpeg-flags';
+ import {warnAboutFfmpegVersion} from './warn-about-ffmpeg-version';
const existsMap: {[key: string]: boolean} = {};
@@@ -44,59 -41,58 +47,74 @@@ const isHomebrewInstalled = (): boolea
return binaryExists('brew', null);
};
+ export const checkAndValidateFfmpegVersion = async (options: {
+ ffmpegExecutable: string | null;
++ remotionRoot: string;
+ }) => {
+ const ffmpegVersion = await getFfmpegVersion({
+ ffmpegExecutable: options.ffmpegExecutable,
++ remotionRoot: options.remotionRoot,
+ });
+ const buildConf = await getFfmpegBuildInfo({
+ ffmpegExecutable: options.ffmpegExecutable,
++ remotionRoot: options.remotionRoot,
+ });
+ warnAboutFfmpegVersion({ffmpegVersion, buildConf});
+ };
+
export const validateFfmpeg = async (
- customFfmpegBinary: string | null
+ customFfmpegBinary: string | null,
+ remotionRoot: string
): Promise<void> => {
- const ffmpegExists = binaryExists('ffmpeg', customFfmpegBinary);
- if (!ffmpegExists) {
- if (customFfmpegBinary) {
- console.error('FFmpeg executable not found:');
- console.error(customFfmpegBinary);
- throw new Error('FFmpeg not found');
- }
+ if (await binaryExists('ffmpeg', customFfmpegBinary)) {
+ return;
+ }
- console.error('It looks like FFMPEG is not installed');
- if (os.platform() === 'darwin' && isHomebrewInstalled()) {
- console.error('Run `brew install ffmpeg` to install ffmpeg');
- } else if (os.platform() === 'win32') {
- console.error('1. Install FFMPEG for Windows here:');
- console.error(
- 'https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg#installing-ffmpeg-in-windows'
- );
- console.error('2. Add FFMPEG to your PATH');
- console.error(' a. Go to the settings app.');
- console.error(' b. Click System.');
- console.error(' c. Click About.');
- console.error(' d. Click Advanced system settings.');
- console.error(' e. Click Environment variables.');
- console.error(
- ' f. Search for PATH environemnt variable, click edit and the folder where you installed FFMPEG.'
- );
- console.error(
- ' g. Important: Restart your terminal completely to apply the new PATH.'
- );
- console.error('3. Re-run this command.');
- } else {
- console.error(
- 'See https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg on how to install FFMPEG.'
- );
- }
+ if (ffmpegInNodeModules(remotionRoot)) {
+ return;
+ }
+ if (
+ os.platform() === 'darwin' ||
+ (os.platform() === 'win32' && process.arch === 'x64') ||
+ (os.platform() === 'linux' && process.arch === 'x64')
+ ) {
+ await downloadFfmpeg(remotionRoot, getFfmpegDownloadUrl().url);
+ return validateFfmpeg(customFfmpegBinary, remotionRoot);
+ }
+
+ if (customFfmpegBinary) {
+ console.error('FFmpeg executable not found:');
+ console.error(customFfmpegBinary);
- process.exit(1);
+ throw new Error('FFmpeg not found');
}
- await checkAndValidateFfmpegVersion({ffmpegExecutable: customFfmpegBinary});
+ console.error('It looks like FFMPEG is not installed');
+ if (os.platform() === 'darwin' && (await isHomebrewInstalled())) {
+ console.error('Run `brew install ffmpeg` to install ffmpeg');
+ } else if (os.platform() === 'win32') {
+ console.error('1. Install FFMPEG for Windows here:');
+ console.error(
+ 'https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg#installing-ffmpeg-in-windows'
+ );
+ console.error('2. Add FFMPEG to your PATH');
+ console.error(' a. Go to the settings app.');
+ console.error(' b. Click System.');
+ console.error(' c. Click About.');
+ console.error(' d. Click Advanced system settings.');
+ console.error(' e. Click Environment variables.');
+ console.error(
+ ' f. Search for PATH environemnt variable, click edit and the folder where you installed FFMPEG.'
+ );
+ console.error(
+ ' g. Important: Restart your terminal completely to apply the new PATH.'
+ );
+ console.error('3. Re-run this command.');
+ } else {
+ console.error(
+ 'See https://github.com/adaptlearning/adapt_authoring/wiki/Installing-FFmpeg on how to install FFMPEG.'
+ );
+ }
+
+ process.exit(1);
};