2012-01-10 27 views
12

我想寫一個nodejs服務器,將需要一個時間輸入(可能是url路徑的一部分),然後提供一個當時的視頻幀索引作爲JPEG圖片。如何在nodejs中獲取視頻的快照?

我可以在簡單的Javascript中輕鬆完成此操作,但是我無法在nodejs中看到這種方法。我知道我可能需要使用像node-canvas這樣的canvas插件來完成快照。

任何想法歡迎。

以下是我如何做到這一點的Javascript的時刻:

myjavascript.js

function capture(video, scaleFactor) { 
    if(scaleFactor == null){ 
     scaleFactor = 1; 
    } 
    var w = video.videoWidth * scaleFactor; 
    var h = video.videoHeight * scaleFactor; 
    stdout("<br/> w: "+ w+ "<br/> h: "+ h); 
    var canvas = document.createElement('canvas'); 
     canvas.width = w; 
     canvas.height = h; 
    var ctx = canvas.getContext('2d'); 
     ctx.drawImage(video, 0, 0, w, h); 
    return canvas; 
} 

function shoot(){ 
var video = document.getElementById("videoTag"); 
var output = document.getElementById("output"); 
var canvas = capture(video, 1); 
output.innerHTML = ''; 
output.appendChild(canvas); 
} 

的index.html

<html> 
<head> 
    <title>video snap</title> 
    <script type="text/javascript" src="myjavascript.js"></script> 
</head> 
<body> 

<div id="video_container" > 
     <video id="videoTag" width="640" height="360" autobuffer="" controls="true"> 
      <source src="frozenplanet.mp4" type="video/mp4"> 
      <source src="frozenplanet.ogv" type="video/ogg"> 
     </video> 
</div> 

<div id="output"></div> 

</body> 
</html> 
+2

Node.js沒有視頻處理工具,也不推薦使用網絡應用來處理視頻。如果你想在服務器端處理視頻,你需要一個適當的工具來實現,並且需要一些隊列實現(也許在節點上)。 – 2012-01-10 12:17:55

回答

26

node-fluent-ffmpeg有一個很好的takeScreenshots功能。

var proc = new ffmpeg('/path/to/your_movie.avi') 
    .takeScreenshots({ 
     count: 1, 
     timemarks: [ '600' ] // number of seconds 
    }, '/path/to/thumbnail/folder', function(err) { 
    console.log('screenshots were saved') 
    }); 
+0

我很想嘗試一下,看起來比exec ffmpeg更優雅,但是現在我不能得到npm來安裝它或者其他任何東西:S – MYR 2012-01-13 16:37:50

+0

npm install fluent -ffmpeg npm http GET https://registry.npmjs.org/fluent-ffmpeg npm ERR!錯誤:無法從註冊表中獲取:流利-ffmpeg ... 這是我得到的錯誤,檢查代理設置等,都好! :( – MYR 2012-01-13 16:43:19

+0

那麼那麼試着用npm來解決這個問題,因爲它肯定更好地讓用戶空間中的所有模塊都可用,而不必重新發明輪子。 – fent 2012-01-13 16:43:20

1

爲「節點流暢-ffmpeg的」不工作不適合我出於某種原因,我想通了這一點我自己 - 基於對video-thumb代碼(也沒有爲我工作)。您需要先安裝FFMPEG,然後才能使用此代碼,here是關於如何爲Mac執行操作的教程。

var path = require('path'), // Default node module 
    pathToFile = path.join(__dirname, 'folder', 'file.mov'), 
    pathToSnapshot = path.join(__dirname, 'folder', 'file-snapshot.jpg'); 

// Also a default node module 
require('child_process').exec(('ffmpeg -ss 00:00:25 -i ' + pathToFile + ' -vframes 1 -q:v 2 ' + pathToSnapshot), function() { 

    console.log('Saved the thumb to:', pathToSnapshot); 

}); 
0

您可以使用node-fluent-ffmpeg模塊。你需要安裝ffmpeg;在MacOS上,安裝Homebrew,然後使用brew install ffmpeg命令。

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

ffmpeg('/path/to/video.mp4') 
    .on('end', function() { 
    console.log('Screenshots taken'); 
    }) 
    .on('error', function(err) { 
    console.error(err); 
    }) 
    .screenshots({ 
    // Will take screenshots at 20%, 40%, 60% and 80% of the video 
    count: 4, 
    folder: '/path/to/output' 
    });