The reason is that my recent project needs to package FFMPEG into ELECTRON, but the scheme to introduce FFMPEG needs to be adjusted in order to support M1 chip

Trouble spots

  1. ffmpegThe introduced centralized solution does not support basedarmthemacLead to even installation shake trouble
  2. Introduction of the path call problem
  3. Optimize package size

Ffmpeg introduction scheme

Since ffmPEG-static is not intended to support M1, it is not possible to install ffmPEg-static directly or several libraries like it, but there are some benefits, if you install it to the local node_modules, If you package build packages for other platforms on one platform, it will result in missing package files, so there are several solutions

  1. Let users install their ownffmpeg– Not realistic, considering user level
  2. Provide your own binaries

What I did was punch binaries into electron, which won’t upgrade ffMPEG but will allow for a multi-platform build, so first of all, I had to find binaries that I could punch in

Files other than ARM-Mac can be downloaded directly from ffbinaries.com/downloads via Google. Arm-mac files can be compiled locally or downloaded from www.osxexperts.net/

So how does Electron get the correct FFMPEG binary?

In the project root directory, create a new FFMPEG folder. Then, according to platform-arch naming rules, create a folder for each system and store FFMPEG in it

import os from 'os'
import path from 'path'
import fs from 'fs'

const platofrm = os.platform()
const arch = os.arch()
const basePath = path.join(__static, '.. '.'ffmpeg')

const ffmpegPath = path.join(basePath, `${platofrm}-${arch}`.`ffmpeg${platofrm === 'win32' ? '.exe' : ' '}`)

export default ffmpegPath

Copy the code

In this way, to get the correct address, here since I used vue-cli-plugin-electron- Builder there is a __static available, other available app.getPath(‘exe’) instead

When packing, you also need to pack the binaries into it and add them in the electron builder

{
        "extraResources": ["./ffmpeg/${platform}-${arch}"]}Copy the code

Ok, so when you pack it, you type in the binary file

Remember the rules for creating folders? ${platform}-${arch} -${platform}-${arch

Platform and ARCH can be found at nodejs.cn/api/os.html…

conclusion

/ffmpeg/${platform}-${arch} -${platform}-${arch} -${arch

The problem with this approach is that the binaries have to be maintained manually and the repository size is tight