【问题标题】:Display canvas as gif for video preview将画布显示为 gif 以进行视频预览
【发布时间】:2017-08-25 15:40:59
【问题描述】:

我正在开发一个网站,我正在处理视频。

我需要在另一个页面上显示 gif 和预览/预告片,重定向到视频。

What I found & added so far

HTML

<div id=thumbs></div>

CSS

#video {width:320px}

JS

var i =0;
var video = document.createElement("video");
var thumbs = document.getElementById("thumbs");

video.addEventListener('loadeddata', function() {
    thumbs.innerHTML = "";
    video.currentTime = i;
}, false);

video.addEventListener('seeked', function() {
    var j = video.duration;
    var u = j/4;
    // now video has seeked and current frames will show
    // at the time as we expect
    generateThumbnail(i);

    // when frame is captured, increase
    i+=u;

    // if we are not passed end, seek to next interval
    if (i <= video.duration) {
        // this will trigger another seeked event
        video.currentTime = i;
    }
}, false);

video.preload = "auto";
video.src = "https://www.html5rocks.com/en/tutorials/video/basics/devstories.webm";

function generateThumbnail() {
  var c = document.createElement("canvas");
  var ctx = c.getContext("2d");
  c.width = 160;
  c.height = 90;
  ctx.drawImage(video, 0, 0, 160, 90);
  thumbs.appendChild(c);
  thumbs.replaceChild(c, thumbs.childNodes[0]);
}

我所做的是从它的 Url 获取一个视频,然后在相同的时间获取 5 帧。它给了我画布,我想将它们显示为 gif 或一系列图像。

【问题讨论】:

  • 最简单的方法是使用你的画布和一个简单的动画循环来每隔一段时间绘制你的静止图像。
  • @Kaiido 不幸的是,如果您有时间制作演示/一些代码或给我一些提示,我并没有真正使用太多 JS(我是初学者)做这个 ?太棒了!

标签: javascript html video canvas html5-video


【解决方案1】:

由于您只想显示它,而不是尝试生成 gif,因此最简单的方法可能是自己制作动画。

您已经有了获取视频帧的代码,但您目前正在 DOM 中显示它,一旦显示就忘记了。

您可以这样做,将这些帧直接存储为画布,并在一个最终的、可见的画布上以定时循环的方式绘制所有这些画布。

var thumbsList = []; // we will save our frames as canvas in here
var delay = 500; // the speed of the animation (ms)

function generateThumbnail() {
  var c = document.createElement("canvas");
  var ctx = c.getContext("2d");
  c.width = 160;
  c.height = 90;
  ctx.drawImage(video, 0, 0, 160, 90);
  thumbsList.push(c); // store this frame in our list
  if (thumbsList.length === 1) {
    displayThumbs(); // start animating as soon as we got a frame
  }
}
// initialises the display canvas, and starts the animation loop
function displayThumbs() {
  var c = document.createElement("canvas");
  var ctx = c.getContext("2d");
  c.width = 160;
  c.height = 90;
  thumbs.appendChild(c);
  startAnim(ctx); // pass our visible canvas' context
}

function startAnim(ctx) {

  var currentFrame = 0;
  // here is the actual loop
  function anim() {
    ctx.drawImage(thumbsList[currentFrame], 0, 0); // draw the currentFrame
    // increase our counter, and set it to 0 if too large
    currentFrame = (currentFrame + 1) % thumbsList.length;
    setTimeout(anim, delay); // do it again in x ms
  }
  anim(); // let's go !
}
var i = 0;
var video = document.createElement("video");
var thumbs = document.getElementById("thumbs");

/* OP's code */
video.addEventListener('loadeddata', function() {
  thumbs.innerHTML = "";
  video.currentTime = i;
}, false);

video.addEventListener('seeked', function() {
  var j = video.duration;
  var u = j / 4;
  // now video has seeked and current frames will show
  // at the time as we expect
  generateThumbnail(i);
  // when frame is captured, increase
  i += u;

  // if we are not passed end, seek to next interval
  if (i <= video.duration) {
    // this will trigger another seeked event
    video.currentTime = i;
  } else {
    //      displayFrame(); // wait for all images to be parsed before animating
  }
}, false);

video.preload = "auto";
video.src = "https://www.html5rocks.com/en/tutorials/video/basics/devstories.webm";
&lt;div id=thumbs&gt;&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 2021-09-20
    • 2013-12-09
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2023-03-18
    相关资源
    最近更新 更多