2017-02-22 67 views
0

我有一個簡單的nodejs腳本來下載YouTube視頻並轉換爲MP3。我在承諾模式方面存在一些問題,需要一些幫助。基本上,只有一個resolve();,但腳本永遠不會到達那個部分。如果我移動resolve();根據console.log('Finished downloading! start to convert to mp3');如何解決這個承諾轉換爲MP3完成(流利-ffmpeg)

它開始工作,所以問題是與某個回調函數相關的resolve();

全碼:https://github.com/kenpeter/test_youtube_dl

代碼片段:

// input 
var input = [ 
    "https://www.youtube.com/watch?v=YWZ7KtRSoAo", 
    "https://www.youtube.com/watch?v=0kdbu8RZNaY" 
]; 

// lib 
var youtubedl = require('youtube-dl'); 

// fs 
var fs = require('fs'); 

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

// Promise 
var Promise = require("bluebird"); 


// https://www.promisejs.org/ 
function youtubedl_get_info() { 
    return new Promise(function (resolve, reject){ 
    youtubedl.getInfo(input, function(err, info) { 
     if (err) { 
     reject(err); 
     } 
     else 
     { 
     //console.log(info); 
     resolve(info); 
     } 
    });  
    }); 
} 

youtubedl_get_info().then(function(infos) { 

    // info is array 
    // http://bluebirdjs.com/docs/api/promise.each.html 
    Promise.each(infos, function(info) { 

    // return new promise 
    return new Promise(function(resolve, reject) { 

     // output 
     var video_output = info._filename.replace(/[&\/\\#,+()$~%'":*?<>{}\ ]/g, "_"); 

     var audio_title = info.title; 
     audio_title = audio_title.replace(/[&\/\\#,+()$~%'":*?<>{}\ ]/g, "_"); 

     // dl zero 
     var downloaded = 0; 

     // fs file exist sync 
     if (fs.existsSync(video_output)) { 
     downloaded = fs.statSync(video_output).size; 
     } 


     var video = youtubedl(
     info.webpage_url, 
     ['--format=18'], 
     { start: downloaded, cwd: __dirname + "/video" } 
    ); 

     video.on('info', function(info) { 
     console.log('Download started'); 
     console.log('filename: ' + video_output); 

     var total = info.size + downloaded; 
     console.log('size: ' + total); 

     if (downloaded > 0) { 
      console.log('resuming from: ' + downloaded); 
      console.log('remaining bytes: ' + info.size); 
     } 
     }); 

     video.pipe(fs.createWriteStream("./video/" + video_output, { flags: 'a' })); 

     video.on('complete', function complete(info) { 
     'use strict'; 
     console.log('filename: ' + video_output + ' already downloaded.'); 
     }); 


     video.on('end', function() { 
     console.log('Finished downloading! start to convert to mp3'); 

     // It seems I cannot do more async here. 
     // https://codedump.io/share/KVSJfXwwlRSI/1/nodejs--how-to-pipe---youtube-to-mp4-to-mp3 
     var proc = new ffmpeg({source: "./video/" + video_output}); 

     proc.setFfmpegPath('/usr/bin/ffmpeg'); 
     proc.saveToFile("./audio/" + audio_title + ".mp3", function(stdout, stderr) { 
      console.log("----- done -----"); 

      // Why I never come here?????????????????????????????????? 
      resolve(); 
     }); 

     }); 


    }); 

    }); 

}); 
+0

任何控制檯錯誤?是'proc.setFfmpegPath( '的/ usr/bin中/的ffmpeg');'這行工作的罰款? – Sravan

回答

1

這是因爲.saveToFile不接受回調。 嘗試這樣運行它:

proc.setFfmpegPath('./ffmpeg'); 
 
\t \t \t \t \t proc.output("./audio/" + audio_title + ".mp3"); 
 
\t \t \t \t \t proc.on('error', function (err) { 
 
\t \t \t \t \t \t console.log(err); 
 
\t \t \t \t \t }); 
 
\t \t \t \t \t proc.on('end', function() { 
 
\t \t \t \t \t \t resolve(); 
 
\t \t \t \t \t }); 
 
\t \t \t \t \t proc.run();

這裏是the docs關於這一主題。

好聽的歌曲BTW :)