2017-05-13 61 views
0

我使用下面的代碼從視頻中生成縮略圖。 它生成一個4幀的圖像。問題是它不會在幀之間尋找,所以4幀是相同的。我想我必須使用select而不是-ss,但我不知道如何。ffmpeg與JavaScript的縮略圖

// - inPath: path to video file 
// - outPath: path to thumbnail, no thumbnail will be generated when absent 
// - count: number of thumbs to generate 
self.exec = function (inPath, outPath, count, handler) { 
    var args = outPath ? [ 
     '-i', inPath , 
     '-f', 'image2', 
     '-vframes', count || 1, 
     '-aspect', '4:3', 
     '-filter:vf', 'scale=\'if(gt(a,4/3),128,-1)\':' + 
      '\'if(gt(a,4/3),-1,96)\',' + 
      'pad=w=128:h=97:x=(ow-iw)/2:y=(oh-ih)/2:color=black,tile=4x1', 
     '-y', 
     '-ss', '15', 
     outPath 
    ] : [ 
     '-i', inPath 
    ]; 
+1

無關的protip:'ffmpeg'中的參數順序很重要,請在'i'標誌之前執行'ss'標誌,因爲它的搜索速度要快得多。此外,我通常有一個bash腳本或這樣的縮略圖生成一個基本的文件結構(例如'/ media/

+1

請參閱https:/ /trac.ffmpeg.org/wiki/Create%20a%20thumbnail%20image%20every%20X%20seconds%20of%20the%20video –

回答

0

我已經有了這個工作。

mdata = ffprobe(function(metadata){ },inPath) 

     var parts = mdata['duration'].split(":"); 
     var hours = parts[0]; 
     var minutes = parts[1]; 
     var seconds = parts[2]; 
     var durationSec = 3600 * parseFloat(hours) + 60 * parseFloat(minutes) + parseFloat(seconds); 

     var distance = Math.round(durationSec/5);  

     var args = outPath ? [ 
      '-ss', distance , 
      '-i', inPath , 
      '-f', 'image2', 
      '-vframes', count || 1, 
      '-aspect', '4:3', 
      '-filter:vf', 'select=\'isnan(prev_selected_t)+gte(t-prev_selected_t\\,'+ distance +')\',' + 'scale=\'if(gt(a,4/3),206,-1)\':' + 
      '\'if(gt(a,4/3),-1,154)\',' + 
      'pad=w=206:h=155:x=(ow-iw)/2:y=(oh-ih)/2:color=black,tile=4x1', 
      '-y', 



      outPath 
     ] : [ 
      '-i', inPath 
     ];