将视频转换为mp3 node.js

时间:2017-02-18 04:19:01

标签: node.js ffmpeg

我正在尝试下面的代码。任何人都可以告诉我代码中的错误

     var ffmpeg = require('fluent-ffmpeg');

     var proc = new ffmpeg({ source: 'C:/Users/Public/Videos/Sample 

     Videos/Wildlife.wmv', nolog: true })

     proc.setFfmpegPath("C:\\nodejs\\ffmpeg")


     .toFormat('mp3')

     .on('end', function() {
     console.log('file has been converted successfully');
     })
     .on('error', function(err) {
     console.log('an error happened: ' + err.message);
     })
     // save to file <-- the new file I want -->
     .saveToFile('C:/Users/Public/Videos/Sample Videos/Wildlife.mp3');

C:\ nodejs&gt; node test.js 发生错误:生成C:\ nodejs \ ffmpeg ENOENT

1 个答案:

答案 0 :(得分:0)

您没有文件或目录错误。在Windows平台上它需要绝对路径

尝试以下代码

var os = require('os'); // add at top

var ffmpeg = require('fluent-ffmpeg');

     var proc = new ffmpeg({ source: 'C:/Users/Public/Videos/Sample 

     Videos/Wildlife.wmv', nolog: true })

     if(os.platform() === 'win32'){
         proc.setFfmpegPath("C:\\nodejs\\ffmpeg.exe")
     }else{
         proc.setFfmpegPath("C:\\nodejs\\ffmpeg") // change it for linux
     }


     .toFormat('mp3')

     .on('end', function() {
     console.log('file has been converted successfully');
     })
     .on('error', function(err) {
     console.log('an error happened: ' + err.message);
     })
     // save to file <-- the new file I want -->
     .saveToFile('C:/Users/Public/Videos/Sample Videos/Wildlife.mp3');