[英]electron fluent-ffmpeg mergeToFile() command promise not triggering
我正在嘗試將 fluent-ffmpeg 與我的電子應用程序一起使用,以將多個音頻文件與視頻中的圖像連接在一起。 所以如果我有三個文件:
song1.mp3 1:00 song2.mp3 0:30 song3.mp3 2:00 front.jpg
我可以創建 3:30 秒長的output.mp4
,並按順序播放每個文件。 將 front.jpg 設置為背景圖片。
我想先為這個視頻創建連接的音頻文件,然后我可以用兩個輸入渲染一個視頻; 圖像和 3:30 秒長的串聯音頻文件。 但是我很難讓我的電子應用程序等待 ffmpeg 作業運行和完成。
我知道如何在命令行上完成所有這些 ffmpeg 作業,但我一直在遵循本指南,了解如何將 ffmpeg 打包成可以在 mac/win10/linux 環境中運行的電子應用程序。 我現在正在win10上開發它。 gur.com/LtykP.png
我有一個按鈕: <button onClick='fullAlbum("upload-${uploadNumber}")'>FULLALBUM</button>
當我單擊運行fullAlbum()
函數時,該函數調用combineMp3FilesOrig
來運行實際的 ffmpeg 作業:
async function fullAlbum(uploadName) {
//document.getElementById("buttonId").disabled = true;
//get table
var table = $(`#upload_${uploadNumber}_table`).DataTable()
//get all selected rows
var selectedRows = table.rows( '.selected' ).data()
//get outputFile location
var path = require('path');
var outputDir = path.dirname(selectedRows[0].audioFilepath)
//create outputfile
var timestamp = new Date().getUTCMilliseconds();
let outputFilepath = `${outputDir}/output-${timestamp}.mp3`
console.log('fullAlbum() button pressed: ', timestamp)
await combineMp3FilesOrig(selectedRows, outputFilepath, '320k', timestamp);
//document.getElementById("buttonId").disabled = false;
console.log(`fullAlbum() /output-${timestamp}.mp3 should be created now`)
}
function combineMp3FilesOrig(selectedRows, outputFilepath, bitrate, timestamp) {
console.log(`combineMp3FilesOrig(): ${outputFilepath}`)
//begin get ffmpeg info
const ffmpeg = require('fluent-ffmpeg');
//Get the paths to the packaged versions of the binaries we want to use
const ffmpegPath = require('ffmpeg-static').replace('app.asar','app.asar.unpacked');
const ffprobePath = require('ffprobe-static').path.replace('app.asar','app.asar.unpacked');
//tell the ffmpeg package where it can find the needed binaries.
ffmpeg.setFfmpegPath(ffmpegPath);
ffmpeg.setFfprobePath(ffprobePath);
//end set ffmpeg info
//create ffmpeg command
console.log(`combineMp3FilesOrig(): create command`)
const command = ffmpeg();
//set command inputs
command.input('C:\\Users\\marti\\Documents\\martinradio\\uploads\\CharlyBoyUTurn\\5. Akula (Club Mix).flac')
command.input('C:\\Users\\marti\\Documents\\martinradio\\uploads\\CharlyBoyUTurn\\4. Civilian Barracks.flac')
return new Promise((resolve, reject) => {
console.log(`combineMp3FilesOrig(): command status logging`)
command.on('progress', function(progress) {
console.info(`Processing : ${progress.percent} % done`);
})
.on('codecData', function(data) {
console.log('codecData=',data);
})
.on('end', function() {
console.log('file has been converted succesfully; resolve() promise');
resolve();
})
.on('error', function(err) {
console.log('an error happened: ' + err.message, ', reject()');
reject(err);
})
console.log(`combineMp3FilesOrig(): add audio bitrate to command`)
command.audioBitrate(bitrate)
console.log(`combineMp3FilesOrig(): tell command to merge inputs to single file`)
command.mergeToFile(outputFilepath);
console.log(`combineMp3FilesOrig(): end of promise`)
});
console.log(`combineMp3FilesOrig(): end of function`)
}
當我單擊我的按鈕一次時,我的 console.logs 顯示輸入了承諾,創建了命令,但該函數不等待 resolve() 就結束了; 等待幾分鍾不會改變任何事情。
如果我再次按下按鈕:
一個新的命令被創建,到達 Promise 的末尾,但這次實際開始,並觸發前一個命令開始。 然后運行這兩個作業,它們的文件以正確的長度 (12:08) 和正確的質量 (320k) 呈現
我的承諾有什么我需要修復的涉及電子應用程序中的異步功能和承諾嗎? 我嘗試編輯我的 ffmpeg 命令以包含
command.run()
在我保證它被觸發的承諾結束時; 但這導致控制台中的錯誤說Uncaught (in promise) Error: No output specified
因為顯然在 fluent-ffmpeg command.mergeToFile(outputFilepath);
不夠好,我還需要包含.output(outputFilepath)
。 如果我將command.run()
更改為command.output(outputFilepath).run()
,當我單擊按鈕時,ffmpeg 作業將被觸發並呈現得非常好。 除了文件總是 128kbps
所以我試圖弄清楚為什么我包含的代碼塊,我的 ffmpeg 命令在創建時沒有第一次運行。
我玩過這個,我發現你的原始代碼有同樣的問題,文件以 128k 比特率輸出。
這段代碼似乎有效,盡管我得到的最大比特率為 320k(我認為這是編解碼器的限制)。
再次測試后,我認為我的行為與您相同,因為該文件需要一些時間來生成。 如果我從 combineMp3FilesOrig 函數返回一個 Promise,則在單擊處理程序中等待。 我禁用按鈕直到呼叫完成,顯然你的按鈕 id 會有所不同。
function combineMp3FilesOrig(selectedRows, outputFile, bitrate) {
const command = ffmpeg();
var count = selectedRows.length;
for(var i = 0; i < count; i++){
command.input(selectedRows[i].audioFilepath)
}
return new Promise((resolve, reject) => {
command.on('progress', function(progress) {
console.info(`Processing : ${progress.percent} % done`);
})
.on('codecData', function(data) {
console.log('codecData=',data);
})
.on('end', function() {
console.log('file has been converted succesfully');
resolve();
})
.on('error', function(err) {
console.log('an error happened: ' + err.message);
reject(err);
}).audioBitrate(bitrate).mergeToFile(outputFile);
});
}
async function convertFiles() {
document.getElementById("buttonId").disabled = true;
await combineMp3FilesOrig(selectedRows, 'output.mp3', '320k');
document.getElementById("buttonId").disabled = false;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.