将预编译的二进制文件捆绑到电子应用程序中 [英] bundling precompiled binary into electron app

查看:140
本文介绍了将预编译的二进制文件捆绑到电子应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于如何将第三方预编译的二进制文件(如imagemagick)包含到电子应用程序中,是否有一个很好的解决方案?有node.js模块,但它们都是包装器或本机绑定到系统范围内安装的库。我想知道是否可以在分发中捆绑预编译的二进制文件。

Is there a good solution on how to include third party pre compiled binaries like imagemagick into an electron app? there are node.js modules but they are all wrappers or native binding to the system wide installed libraries. I wonder if it's possible to bundle precompiled binaries within the distribution.

推荐答案

我确实找到了解决方案,但我没有如果这被认为是最佳实践的想法。我找不到包含第三方预编译二进制文件的任何好文档,所以我只是摆弄它直到最终使用我的ffmpeg二进制文件。这就是我所做的(从电子快速启动开始,node.js v6):

I did find a solution to this, but I have no idea if this is considered best practice. I couldn't find any good documentation for including 3rd party precompiled binaries, so I just fiddled with it until it finally worked with my ffmpeg binary. Here's what I did (starting with the electron quick start, node.js v6):

Mac OS X方法

在app目录中,我在Terminal中运行以下命令,将ffmpeg二进制文件包含为模块:

From the app directory I ran the following commands in Terminal to include the ffmpeg binary as a module:

mkdir node_modules/ffmpeg
cp /usr/local/bin/ffmpeg node_modules/ffmpeg/
cd node_modules/.bin
ln -s ../ffmpeg/ffmpeg ffmpeg

(替换 / usr / local / bin / ffmpeg with您当前的二进制路径,从此处下载)放置链接允许electron-packager包含我保存到 node_modules / ffmpeg / 的二进制文件。

(replace /usr/local/bin/ffmpeg with your current binary path, download it from here) Placing the link allowed electron-packager to include the binary I saved to node_modules/ffmpeg/.

然后获取捆绑的应用程序路径(这样我可以使用我的二进制文件的绝对路径......无论我做什么,相对路径似乎都不起作用)我安装了npm包app- root-dir运行以下命令:

Then to get the bundled app path (so that I could use an absolute path for my binary... relative paths didn't seem to work no matter what I did) I installed the npm package app-root-dir by running the following command:

npm i -S app-root-dir

现在我有了r oot app目录,我只是为我的二进制文件附加子文件夹并从那里生成。这是我放在renderer.js中的代码:。

Now that I had the root app directory, I just append the subfolder for my binary and spawned from there. This is the code that I placed in renderer.js:.

var appRootDir = require('app-root-dir').get();
var ffmpegpath=appRootDir+'/node_modules/ffmpeg/ffmpeg';
console.log(ffmpegpath);

const
    spawn = require( 'child_process' ).spawn,
    ffmpeg = spawn( ffmpegpath, ['-i',clips_input[0]]);  //add whatever switches you need here

ffmpeg.stdout.on( 'data', data => {
     console.log( `stdout: ${data}` );
    });
   ffmpeg.stderr.on( 'data', data => {
console.log( `stderr: ${data}` );
    });

Windows方法


  1. 打开电子基础文件夹(electron-quick-start是默认名称),然后进入node_modules文件夹。创建一个名为ffmpeg的文件夹,并将静态二进制文件复制到此目录中。注意:它必须是二进制文件的静态版本,对于ffmpeg,我抓住了最新的Windows版本此处

  2. 要获得捆绑的应用程序路径(这样我可以使用绝对路径为我的二进制...相对路径似乎无论我做什么都不起作用)我通过从我的app目录中的命令提示符运行以下命令来安装npm包app-root-dir:

  1. Open your electron base folder (electron-quick-start is the default name), then go into the node_modules folder. Create a folder there called ffmpeg, and copy your static binary into this directory. Note: it must be the static version of your binary, for ffmpeg I grabbed the latest Windows build here.
  2. To get the bundled app path (so that I could use an absolute path for my binary... relative paths didn't seem to work no matter what I did) I installed the npm package app-root-dir by running the following command from a command prompt in my app directory:

 npm i -S app-root-dir


  • 在node_modules文件夹中,导航到.bin子文件夹。您需要在此处创建几个文本文件,以告知节点包含刚刚复制的二进制exe文件。使用您喜欢的文本编辑器创建两个文件,一个名为 ffmpeg ,其中包含以下内容:

  • Within your node_modules folder, navigate to the .bin subfolder. You need to create a couple of text files here to tell node to include the binary exe file you just copied. Use your favorite text editor and create two files, one named ffmpeg with the following contents:

    #!/bin/sh
    basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
    
    case `uname` in
        *CYGWIN*) basedir=`cygpath -w "$basedir"`;;
    esac
    
    if [ -x "$basedir/node" ]; then
      "$basedir/node"  "$basedir/../ffmpeg/ffmpeg" "$@"
      ret=$?
    else
      node  "$basedir/../ffmpeg/ffmpeg" "$@"
      ret=$?
    fi
    exit $ret
    

    第二个文本文件名为 ffmpeg.cmd

    @IF EXIST "%~dp0\node.exe" (
     "%~dp0\node.exe"  "%~dp0\..\ffmpeg\ffmpeg" %*
    ) ELSE (
       @SETLOCAL
     @SET PATHEXT=%PATHEXT:;.JS;=;%
     node  "%~dp0\..\ffmpeg\ffmpeg" %*
    )
    


  • 接下来,您可以在Windows电子发行版中运行ffmpeg(在renderer.js中)如下(我也使用app-root-dir节点模块)。注意添加到二进制路径的引号,如果您的应用程序安装到带空格的目录(例如 C:\Program Files \YourApp ),它将无法工作这些。

    Next you can run ffmpeg in your Windows electron distribution (in renderer.js) as follows (I'm using the app-root-dir node module as well). Note the quotes added to the binary path, if your app is installed to a directory with spaces (eg C:\Program Files\YourApp) it won't work without these.

    var appRootDir = require('app-root-dir').get();
    var ffmpegpath = appRootDir + '\\node_modules\\ffmpeg\\ffmpeg';
    
    const
        spawn = require( 'child_process' ).spawn;
        var ffmpeg = spawn( 'cmd.exe', ['/c',  '"'+ffmpegpath+ '"', '-i', clips_input[0]]);  //add whatever switches you need here, test on command line first
    ffmpeg.stdout.on( 'data', data => {
         console.log( `stdout: ${data}` );
     });
    ffmpeg.stderr.on( 'data', data => {
         console.log( `stderr: ${data}` );
     });
    

    这篇关于将预编译的二进制文件捆绑到电子应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆