如何在nodejs中获取video的快照?

我正在尝试编写一个nodejs服务器,它需要一个时间input(可能是urlpath的一部分),然后在这个时间索引提供一个静止的video帧作为JPEG图片。

我可以用简单的Javascript轻松地做到这一点,但我不能在nodejs中做到这一点。 我知道我可能需要使用像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> 

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') }); 

由于“node-fluent-ffmpeg”由于某种原因并不适合我,所以我根据video-thumb的代码(这也不适合我)为自己弄明白了。 你需要先安装FFMPEG,然后才能使用这个代码, 这里是关于如何为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); }); 

您可以使用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' });