File size: 3,390 Bytes
6091049
 
 
 
 
 
 
 
 
 
674bcb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f431cd4
674bcb3
 
 
 
 
 
 
2e90118
 
 
 
 
 
 
 
 
0f62cae
2e90118
 
674bcb3
f431cd4
674bcb3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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()
}