2017-10-10 122 views
6

我使用帶有node.js的fluent-ffmpeg庫將原本以Flash電影格式的視頻轉碼爲具有多種分辨率,1080p的mp3格式。一旦轉碼完成後,我想將轉碼後的視頻轉移到s3存儲區。將FFmpeg輸出直接上傳到亞馬遜S3

我從源s3存儲桶中取出原始.flv文件,並將該流傳遞給ffmpeg構造函數。問題是轉碼完成後,我如何才能獲得mp4數據流發送到s3。

這裏是我到目前爲止的代碼:

 var params = { 
      Bucket: process.env.SOURCE_BUCKET, 
      Key: fileName 
     }; 
     s3.getObject(params, function(err, data) { 
      if (err) console.log(err, err.stack); // an error occurred 

      var format = ffmpeg(data) 
      .size('854x480') 
      .videoCodec('libx264') 
      .format('flv') 
      .toFormat('mp4'); 
      .on('end', function() { 
       //Ideally, I would like to do the uploading here 

       var params = { 
        Body: //{This is my confusion, how do I get the stream to add here?}, 
        Bucket: process.env.TRANSCODED_BUCKET, 
        Key: fileName 
       }; 
       s3.putObject(params, function (err, data) { 

       }); 
      }) 
      .on('error', function (err) { 
       console.log('an error happened: ' + err.message); 
      }); 

     }); 

對於上面的代碼,我在哪裏可以得到的編碼數據流添加到params對象的「身體」屬性?

更新:

這裏是什麼,我試圖做一個修訂:

var outputStream: MemoryStream = new MemoryStream(); 

     var proc = ffmpeg(currentStream) 
      .size('1920x1080') 
      .videoCodec('libx264') 
      .format('avi') 
      .toFormat('mp4') 
      .output(outputStream) 
      // setup event handlers 
      .on('end', function() { 
       uploadFile(outputStream, "").then(function(){ 
        resolve(); 
       }) 
      }) 
      .on('error', function (err) { 
       console.log('an error happened: ' + err.message); 
      }); 

我想避免複製文件從S3本地文件系統,而我更願意處理該文件在內存中,並在完成時上傳回s3。 fluent-ffmpeg會允許這種情況嗎?

回答

2

您似乎並未在任何地方保存轉碼輸出。

  1. 保存使用output到您的本地文件系統的轉碼(新.flv文件)的輸出。
  2. 將您的新文件的內容提供給putObject。根據the putObject documentation,所述Body參數接受:

    Body - (BufferTyped ArrayBlobStringReadableStream)對象數據。

下面是一些修改後的示例代碼:

// Generate a filename for the `.flv` version 
var flvFileName = fileName.substring(0, fileName.length - path.extname(fileName).length) + '.flv'; 

// Perform transcoding, save new video to new file name 
var format = ffmpeg(data) 
    .size('854x480') 
    .videoCodec('libx264') 
    .format('flv') 
    .toFormat('mp4'); 
    .output(flvFileName) 
    .on('end', function() { 
     // Provide `ReadableStream` of new video as `Body` for `pubObject` 
     var params = { 
      Body: fs.createReadStream(flvFileName) 
      Bucket: process.env.TRANSCODED_BUCKET, 
      Key: flvFileName 
     }; 

     s3.putObject(params, function (err, data) { 

     }); 
    }) 

注意:您可以從fluent-ffmpeg創建一個輸出流,並上載流AWS S3,如果你喜歡,但是這將邏輯和錯誤處理複雜化。

+0

有沒有辦法暫時寫入內存緩衝區,然後在正文參數中發送緩衝區? – user1790300

+0

有沒有辦法避免必須臨時將文件寫入文件系統? – user1790300