如何将预编译的二进制文件与电子捆绑在一起 [英] How can I bundle a precompiled binary with electron

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

问题描述

我正试图用电子应用程序包括一个预编译的二进制文件。我开始用电子快速启动应用程序,并修改了我的 renderer.js 文件,以包含当文件放在正文时触发的代码:

  spawn = require('child_process').spawn,
ffmpeg = spawn('node_modules / .bin / ffmpeg',['-i' ,夹子[0], ' - 一个', ' - 问:v', '1', ' - 了vcodec', 'libx264', ' - Y', ' - pix_fmt', 'YUV420P', ' - VF', 'setsar = 1,scale = trunc(iw / 2)* 2:trunc(ih / 2)* 2,crop = in_w:in_h-50:0:50','/tmp/out21321.mp4']);

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

我已将我的预编译ffmpeg二进制文件放在 node_modules / .bin / code>。一切在开发板中都很出色,但是当我使用电子包装器设置应用程序时,它会在触发时向控制台发出一个产生错误ENOENT 。我找到一个非常相似的问题但是,这个问题似乎并没有得到明确的回答。 电子包装商的npm页面确实表明可以捆绑,但我不能找到有关如何执行的任何文档。

解决方案

如果有人碰巧需要回答这个问题:我有一个解决方案对此,但我不知道这是否被认为是最佳做法。我找不到包含第三方预编译二进制文件的任何好的文档,所以我只是用它,直到它终于工作。这是我做的(从电子快速启动,node.js v6开始):



从应用程序目录中,我运行以下命令将ffmpeg二进制文件作为模块:

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

(replace / usr / local / bin / ffmpeg与你当前的二进制路径,从这里下载)放置链接允许电子包装器包含我保存的二进制文件到node_modules / ffmpeg /.



然后要获得捆绑的应用程序路径,我通过运行以下命令安装了npm包app-root-dir:

  npm i -S app-root-dir 

由于我可以得到应用程序路径,我只是附加了子文件夹为我的二进制并从那里产生。这是我放在renderer.js中的代码:。

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

const
spawn = require('child_process').spawn,
ffmpeg = spawn(ffmpegpath,['-i',clips_input [0]]); //添加你需要的任何开关

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


I am trying to include a precompiled binary with an electron app. I began with electron quick start app and modified my renderer.js file to include this code that is triggered when a file is dropped on the body:

spawn = require( 'child_process' ).spawn,
        ffmpeg = spawn( 'node_modules/.bin/ffmpeg', ['-i',clips[0],'-an','-q:v','1','-vcodec','libx264','-y','-pix_fmt','yuv420p','-vf','setsar=1,scale=trunc(iw/2)*2:trunc(ih/2)*2,crop=in_w:in_h-50:0:50', '/tmp/out21321.mp4']);

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

I have placed my precompiled ffmpeg binary in node_modules/.bin/. Everything works great in the dev panel, but when I use electron-packager to set up the app, it throws a spawn error ENOENT to the console when triggered. I did find a very similar question on SO, but the question doesn't seem to be definitively answered. The npm page on electron-packager does show that they can be bundled, but I cannot find any documentation on how to do so.

解决方案

If anyone happens to need an answer to this question: I do have 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. Here's what I did (starting with the electron quick start, node.js v6):

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

mkdir node_modules/ffmpeg
cp /usr/local/bin/ffmpeg node_modules/ffmpeg/
ln -s ../ffmpeg/ffmpeg node_modules/.bin/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/.

Then to get the bundled app path I installed the npm package app-root-dir by running the following command:

npm i -S app-root-dir

Since I could then get the app path, I just appended 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}` );
    });

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

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