如何用nodejs获取视频的快照?

时间:2021-07-22 06:53:38

I am trying to write a nodejs server that will take a time input (possibly part of url path) and then provide a still of the video frame at that time index as a jpeg picture.

我正在尝试编写一个nodejs服务器,该服务器将接受一个时间输入(可能是url路径的一部分),然后以jpeg图片的形式提供当时的视频帧的静止帧。

I can do this easily in plain Javascript, but I cant see a way to do this in nodejs. I know I will probably need to use a canvas plugin like node-canvas to do the snapshot.

我可以在普通的Javascript中轻松做到这一点,但我无法在node . js中找到这样做的方法。我知道我可能需要使用像node-canvas这样的canvas插件来做快照。

Any ideas welcome.

欢迎任何的想法。

The following is how I do it in Javascript at the moment:

以下是我目前在Javascript中的做法:

myjavascript.js

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

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>

3 个解决方案

#1


28  

node-fluent-ffmpeg has a nice takeScreenshots function.

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

#2


1  

As 'node-fluent-ffmpeg' wasn't not working for me for some reason, I figured this out myself - based on the code of video-thumb (which also wasn't working for me). You do need to install FFMPEG before you can use this code, here is tutorial on how to do so for Mac.

由于“node-fluent-ffmpeg”不适合我,我根据视频拇指代码(它也不适合我)自己发现了这一点。在使用此代码之前,您确实需要安装FFMPEG,这里有关于如何在Mac上安装FFMPEG的教程。

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

});

#3


0  

You can use the node-fluent-ffmpeg module for this. You'll need to install ffmpeg; on MacOS, install Homebrew then use the brew install ffmpeg command.

您可以使用node- fluve -ffmpeg模块进行此操作。您需要安装ffmpeg;在MacOS上,安装Homebrew然后使用brew安装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'
  });

#1


28  

node-fluent-ffmpeg has a nice takeScreenshots function.

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

#2


1  

As 'node-fluent-ffmpeg' wasn't not working for me for some reason, I figured this out myself - based on the code of video-thumb (which also wasn't working for me). You do need to install FFMPEG before you can use this code, here is tutorial on how to do so for Mac.

由于“node-fluent-ffmpeg”不适合我,我根据视频拇指代码(它也不适合我)自己发现了这一点。在使用此代码之前,您确实需要安装FFMPEG,这里有关于如何在Mac上安装FFMPEG的教程。

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

});

#3


0  

You can use the node-fluent-ffmpeg module for this. You'll need to install ffmpeg; on MacOS, install Homebrew then use the brew install ffmpeg command.

您可以使用node- fluve -ffmpeg模块进行此操作。您需要安装ffmpeg;在MacOS上,安装Homebrew然后使用brew安装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'
  });