如何在nodejs中获取视频的快照?

MYR*_*MYR 16 node.js html5-video html5-canvas

我正在尝试编写一个nodejs服务器,它将花费一些时间输入(可能是url路径的一部分),然后在那个时间索引处提供一个视频帧的静止图像作为jpeg图片.

我可以在普通的Javascript中轻松地做到这一点,但是我无法在nodejs中看到这样做的方法.我知道我可能需要使用像canvas-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);
}
Run Code Online (Sandbox Code Playgroud)

的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>
Run Code Online (Sandbox Code Playgroud)

fen*_*ent 35

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')
  });
Run Code Online (Sandbox Code Playgroud)

  • 最新版本的库中没有这样的API。回调方法不会被调用。 (2认同)

小智 5

您可以为此使用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'
  });
Run Code Online (Sandbox Code Playgroud)