remote-rdr / ffmpeg-fix.js
shiveshnavin's picture
Fixes
6091049
import fs from 'fs-extra'
import path from 'path'
import which from 'which'
import os from 'os'
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import ffbinaries from 'ffbinaries'
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename)
var dest = path.join(__dirname, '/ffmpeg');
async function createSymlink(targetPath, symlinkPath) {
try {
console.log('Adding Symlink', symlinkPath, '=>', targetPath)
if (!(await fs.pathExists(targetPath))) {
console.error('Target executable not found:', targetPath);
return;
}
if (await fs.exists(symlinkPath)) {
await fs.unlink(symlinkPath);
}
if (await fs.pathExists(symlinkPath)) {
await fs.remove(symlinkPath);
}
await fs.symlink(targetPath, symlinkPath);
console.log('Symlink created successfully:', symlinkPath);
} catch (error) {
console.error('An error occurred:', error.message);
}
}
async function findAndCreateSymlink(targetExecutable, ffmpegPath) {
try {
// Find the target executable in the system's PATH
var ffmpegPath = ffmpegPath || await which(targetExecutable);
let remotionFolder = path.join(__dirname, 'node_modules', '@remotion')
const files = fs.readdirSync(remotionFolder);
files.forEach((file) => {
const filePath = path.join(remotionFolder, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory() && file.startsWith('compositor-')) {
const symlinkPath = path.join(filePath, 'ffmpeg', 'remotion', 'bin', targetExecutable);
// await createSymlink(ffmpegPath, symlinkPath);
fs.copyFileSync(ffmpegPath, symlinkPath)
console.log('Copied to', symlinkPath)
}
});
} catch (error) {
console.error('An error occurred while creating symlink ffmpeg:', error);
}
}
const ffmpegtargetExecutable = os.platform() === 'win32' ? 'ffmpeg.exe' : 'ffmpeg';
const ffprobetargetExecutable = os.platform() === 'win32' ? 'ffprobe.exe' : 'ffprobe';
const ffmpegPath = path.join(dest, ffmpegtargetExecutable)
const ffprobePath = path.join(dest, ffprobetargetExecutable)
function classifyOS() {
const platform = os.platform();
const arch = os.arch();
if (platform === 'win32' && arch === 'x64') {
return 'windows-64';
} else if (platform === 'linux' && arch === 'ia32') {
return 'linux-32';
} else if (platform === 'linux' && arch === 'x64') {
return 'linux-64';
} else if (platform === 'linux' && arch === 'arm') {
const armVersion = os.endianness() === 'LE' ? 'el' : 'hf';
return `linux-arm${armVersion}`;
} else if (platform === 'linux' && arch === 'arm64') {
return 'linux-arm64';
} else if (platform === 'darwin' && arch === 'x64') {
return 'osx-64';
} else {
return 'unknown';
}
}
function process() {
findAndCreateSymlink(ffmpegtargetExecutable, ffmpegPath);
findAndCreateSymlink(ffprobetargetExecutable, ffprobePath);
}
if (!fs.existsSync(ffmpegPath) || !fs.existsSync(ffprobePath)) {
let platform = classifyOS()
console.log(`Downloading ffmpeg and ffprobe binaries for ${platform} to ` + dest);
ffbinaries.downloadBinaries(['ffmpeg', 'ffprobe'], { platform: platform, quiet: true, destination: dest }, function () {
console.log(`Downloaded ffplay and ffprobe binaries for ${platform} to ` + dest + '.');
process()
});
}
else {
process()
}