[英]Electron and NodeJS: Execute shell command asyncronously with live stream
Electron:實時獲取文件轉換百分比:
我想運行命令ffmpeg -i video.mp4
(example) 將視頻轉換為另一種格式。 但我想獲得在流程輸出中流式傳輸的轉換百分比,並將其獲取到我的 Electron 應用程序或 NodeJS 中。
我已經嘗試了所有方法: spawn
fork
exec
並且所有方法都返回進程輸出的最后一行。 我想要寫的每一行的實時流,以顯示百分比進度。
我試過了:
叉子
const {fork} = require('child_process')
const forked = fork('ffmpeg -i video.mp4');
forked.on('message', (msg) => {
console.log(msg);
})
執行方案 1
const execFile = require('child_process').execFile;
execFile('ffmpeg -i video.mp4', [], (e, stdout, stderr) => {
if (e instanceof Error){
console.error(e);
}
console.log('stdout ', stdout)
console.log('stderr ', stderr);
})
執行方案 2
const exec = require('child_process').exec;
exec('ffmpeg -i video.mp4', (error, stdout, stderr) => {
console.log(stdout);
});
/*EXEC Alternative 2*/
const exec = require('child_process').exec;
const proccessing = exec('ffmpeg -i video.mp4');
proccessing.stdout.on('data', function(data) {
console.log(data);
});
proccessing.stdout.pipe(process.stdout);
產卵
const spawn = require('child_process').spawn,
const processing = spawn('ffmpeg -i video.mp4');
processing .stdout.on('data', function (data) {
console.log('stdout: ' + data.toString());
});
processing .stderr.on('data', function (data) {
console.log('stderr: ' + data.toString());
});
processing .on('exit', function (code) {
console.log('code ' + code.toString());
});
概括:
🎯目標:在控制台中得到這個結果
10% converted
15% converted
20% converted
100% converted...
❌錯誤:我得到了什么:
100% converted
//Sometimes I get an empty string because it is the last line of the .exe script
在標記為重復之前,我確定在 STACKOVERFLOW 中沒有一個答案適合我
您需要將 ffmpeg 與ffmpeg-progress-wrapper 一起使用。 附加事件“進度”並獲取“進度”屬性。
process.on('progress', (progress) => console.log(JSON.stringify(progress.progress));
它從 0 到 1,因此您需要進行一些調整。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.