drawImage in canvas 从一个视频帧改变色调

drawImage in canvas from a video frame change the hue

我写了一个简单的脚本,从视频中提取一帧并将其绘制到 canvas。我的问题是视频和绘制图像之间的颜色在变化。

我把结果放在原件旁边以便于查看。原来的在左边。顺便说一句,它似乎在 chrome 浏览器上更加明显。我在 OSX.

上进行的所有测试

这里是一个片段,canvas 在左边,视频在右边:

 // Get our mask image
var canvas = document.querySelector(".canvas");
var video = document.querySelector(".video");

var ctx = canvas.getContext('2d');
function drawMaskedVideo() {
  ctx.drawImage(video, 0, 0, video.videoWidth/2, video.videoHeight, 0,0, video.videoWidth/2,  video.videoHeight);
}

requestAnimationFrame(function loop() {
  drawMaskedVideo();
  requestAnimationFrame(loop.bind(this));
});
html, body {
  margin: 0 auto;
}
.video, .canvas {
  width: 100%;
}

.canvas {
  position: absolute;
  top: 0;
  left: 0;
}
<video class="video" autoplay="autoplay" muted="muted" preload="auto" loop="loop">
    <source src="http://mazwai.com/system/posts/videos/000/000/214/original/finn-karstens_winter-wonderland-kiel.mp4" type="video/mp4">
</video>
<canvas class='canvas' width='1280' height='720'></canvas>

我想知道为什么会发生这种情况,是否可以通过跨浏览器的方式摆脱它?

这里是我写的简单脚本:

let video = document.querySelector('#my-video') // .mp4 file used
let w = video.videoWidth;
let h = video.videoHeight;
let canvas = document.createElement('canvas');
    canvas.width  = w;
    canvas.height = h;
let ctx = canvas.getContext('2d');

ctx.drawImage(video, 0, 0, w, h)

document.querySelector('.canvas-container').appendChild(canvas);

解决方案可能就像为视频元素设置 css filter 一样简单:

.video {
    -webkit-filter: contrast(100%);
}

我无法对此进行推理,因为它被发现是意外(玩你的演示并阅读相关答案),所以我把技术解释留给其他人,现在给你一些魔法。

Any sufficiently advanced technology is indistinguishable from magic.

— Arthur C. Clarke

 // Get our mask image
var canvas = document.querySelector(".canvas");
var video = document.querySelector(".video");

var ctx = canvas.getContext('2d');
function drawMaskedVideo() {
  ctx.drawImage(video, 0, 0, video.videoWidth/2, video.videoHeight, 0,0, video.videoWidth/2,  video.videoHeight);
}

requestAnimationFrame(function loop() {
  drawMaskedVideo();
  requestAnimationFrame(loop.bind(this));
});
html, body {
  margin: 0 auto;
}
.video, .canvas {
  width: 100%;
}
.video {
  -webkit-filter: contrast(100%);
}

.canvas {
  position: absolute;
  top: 0;
  left: 0;
}
<video class="video" autoplay="autoplay" muted="muted" preload="auto" loop="loop">
    <source src="http://mazwai.com/system/posts/videos/000/000/214/original/finn-karstens_winter-wonderland-kiel.mp4" type="video/mp4">
</video>
<canvas class='canvas' width='1280' height='720'></canvas>


注: 运行 这在 Macbook Pro(2.3 GHz Intel Core i5)上我看不出性能有什么不同。在视频播放期间跟踪 CPU,两个演示都闲置在 28% 左右。