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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8802484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:59:30  来源:igfitidea点击:

How do I get a snapshot still of a video in nodejs?

node.jshtml5-videohtml5-canvas

提问by MYR

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 轻松地做到这一点,但我看不到在 nodejs 中做到这一点的方法。我知道我可能需要使用像 node-canvas 这样的画布插件来做快照。

Any ideas welcome.

欢迎任何想法。

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

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

myjavascript.js

我的javascript.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

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

回答by fent

node-fluent-ffmpeghas a nice takeScreenshotsfunction.

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

回答by Steven Oosterbeek

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, hereis tutorial on how to do so for Mac.

由于 '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);

});

回答by Steven Westmoreland

You can use the node-fluent-ffmpegmodule for this. You'll need to install ffmpeg; on MacOS, install Homebrewthen use the brew install ffmpegcommand.

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