Angular14+Electron19中FFmpeg音频分析报错:输出格式null不可用
问题描述
升级至Angular 14 + Electron 19的项目中,原本正常运行的FFmpeg实现现在失效。调用getAudioVolumes方法时会报错:
An error occurred while analysing: Output format null is not available
归一化功能实现代码
组件方法
downloadYoutube(id: string){ const filename = id+'.mp4'; const filepath = Config.homePath? Config.homePath + '/' + filename : filename; const stream = ytdl(id).pipe(fs.createWriteStream(filepath)); stream.on('finish', (data) => { console.log(stream); this.mediaService.getAudioVolumes(stream).subscribe((d) => {console.log('done', d);}); }); }
服务类代码
import ffmpeg from 'fluent-ffmpeg-corrected'; import * as ffmpegBin from 'ffmpeg-static-electron'; import FS from 'fs'; import {Readable} from 'stream'; import {Config} from "../shared/config"; export interface IVolumes { meanVolume: string; maxVolume: string; } @Injectable() export class MediaService { ffmpegPath = ffmpegBin.path .replace('app.asar/bin', 'app.asar.unpacked/' + this.binPath) .replace('node_modules/electron/dist/Electron.app/Contents/Resources/electron.asar/renderer/bin', this.binPath) .replace('browser/javascript', 'mac/x64') // TODO: platform hardcoded! ; getAudioVolumes(stream: Readable | FS.WriteStream): Observable<IVolumes> { if (!Config.isElectron){ this.ffmpegPath= 'node_modules/ffmpeg-static-electron/bin/mac/x64/ffmpeg'; } ffmpeg.setFfmpegPath(this.ffmpegPath); return new Observable((observer: NextObserver<IVolumes>) => { const that = this; ffmpeg(stream) .withAudioFilter('volumedetect') .addOption('-f', 'null') .audioBitrate(128) .on('progress', function(progress) { console.log('Normalising Processing: ' + progress.percent + '% done'); }) .on('error', function(err) { console.log('An error occurred while analysing: ' + err.message); observer.error('DBs are not accessible'); }) .on('end', (stdout: any, stderr: string) => { const max = that.parseVolume(stderr, 'max_volume:'); const mean = that.parseVolume(stderr, 'mean_volume:'); console.log('volume analysis done, MeanDB is ', mean); observer.next({meanVolume: mean, maxVolume: max}); observer.complete(); }) .save('/dev/null'); }); }
复现步骤
- Fork对应仓库并切换至yt-dl分支
- 在根目录与app目录下分别执行
npm install - 修改以下node模块的package.json:
- fluent-ffmpeg-corrected 添加:
"browser": { "fs": false, "child_process": false } - isexe 添加:
"browser": { "fs": false}
- fluent-ffmpeg-corrected 添加:
- 执行
npm run electron:build构建应用 - 运行release目录下的应用,点击Download Kraftwerk按钮,即可触发上述错误
内容的提问来源于stack exchange,提问作者Arco Voltaico
相关产品推荐
相关产品推荐

