简体   繁体   English

使用 canvas.captureStream() 使用 alpha 通道捕获视频

[英]Capture video with alpha channel using canvas.captureStream()

I'm trying to capture the contents of a canvas element with alpha channel.我正在尝试使用 alpha 通道捕获画布元素的内容。 When I do it I get the RGB values correctly but the Alpha channel seems to get dropped when playing back the resulting video.当我这样做时,我正确地获得了 RGB 值,但是在播放结果视频时 Alpha 通道似乎被丢弃了。 Is there a way to achieve this?有没有办法实现这一目标?

I'm running the following code:我正在运行以下代码:

var stream = canvas.captureStream(60);
var recorder = new MediaRecorder(stream);

recorder.addEventListener('dataavailable', finishCapturing);
recorder.addEventListener('stop', function(e) {
    video.oncanplay = video.play;
    video.src = URL.createObjectURL(new Blob(blobs, {type:"video/webm; codecs=vp9"}));
});
startCapturing();
recorder.start();

Here's a plnkr demonstrating the issue: http://plnkr.co/edit/z3UL9ikmn6PtVoAHvY0B?p=preview这是一个演示该问题的 plnkr: http ://plnkr.co/edit/z3UL9ikmn6PtVoAHvY0B?p=preview

There is currently no options to enable (VP8/9 transparency channel) from the MediaRecorder API.目前没有选项可以从 MediaRecorder API 启用(VP8/9 透明通道)。
One could maybe open an issue on the W3C Mediacapture-Record git repo.人们可能会在 W3C Mediacapture-Record git repo 上打开一个问题。

For this, I can guess a few reasons :为此,我可以猜测几个原因:

  • From what I understand, webm alpha channel is grossly an hack from chrome, and is not really implemented in the codec itself, nor completely stabilized.据我所知,webm alpha 通道完全是 chrome 的一个黑客,并没有真正在编解码器本身中实现,也没有完全稳定。

  • MediaRecorder should be able to encode in many formats even if current implementations only support video webm/VP8 and webm/VP9 (chrome only).即使当前的实现仅支持视频 webm/VP8 和 webm/VP9(仅限 Chrome),MediaRecorder 也应该能够以多种格式进行编码。 So it would mean that they would have to somehow keep the alpha channel in the raw stream, only for this new canvas.captureStream method.所以这意味着他们必须以某种方式将 alpha 通道保留在原始流中,仅适用于这个新的canvas.captureStream方法。 Historically, MediaStream mainly came from getUserMedia interface, and there were no need way of getting transparency from there.历史上,MediaStream 主要来自 getUserMedia 接口,没有必要从那里获得透明度。
    [edit: Specs have changed since this answer was written, and MediaStreams should now keep the alpha channel, even if the consumer may not be able to use it, also Chrome now supports more video codecs.] [编辑:自从编写此答案以来, 规范已发生变化,MediaStreams 现在应该保留 alpha 通道,即使消费者可能无法使用它,Chrome 现在也支持更多视频编解码器。]

  • Chrome, which is the only one to support YUVA webm display in its stable channel (FF supports it in nightly 54), is still not able to include the duration inside their recorded files, let's them fix this before adding the hackish alpha_mode=true . Chrome 是唯一一个在其稳定频道中支持 YUVA webm 显示的(FF 在每晚 54 中支持它),仍然无法在其录制的文件中包含duration ,让他们在添加 hackish alpha_mode=true之前解决这个问题。

However, you can achieve it yourself kind of easily :但是,你可以自己那种轻松实现它:

If you really want a transparent webm file (only readable in chrome and FF nightly), you can use a second canvas to do the recording, set its background (using fillRect ) to a chroma that won't appear elsewhere in your drawings, draw the original one on it and record its stream.如果你真的想要一个透明的 webm 文件(只能在 chrome 和 FF nightly 中读取),你可以使用第二个画布来进行录制,将其背景(使用fillRect )设置为不会出现在绘图中其他地方的色度,绘制原始的一个并记录它的流。 Once recorded, use ffmpeg to reencode the recorded video, this time with the alpha channel :录制后,使用 ffmpeg 重新编码录制的视频,这次使用 alpha 通道:

// all #00FF00 pixels will become transparent.
ffmpeg -i in.webm -c:v libvpx -vf "chromakey=0x00ff00:0.1:0.1,format=yuva420p" -auto-alt-ref 0 out.webm

I personally needed the -auto-alt-ref 0 flag, not sure everyone needs it though我个人需要-auto-alt-ref 0标志,但不确定每个人都需要它

But because of this other chrome bug , you'll actually have to append this other canvas on screen too, and hide it with css ( opacity: 0; width:0px; height:0px; should do).但是由于这个其他 chrome 错误,您实际上也必须在屏幕上附加这个其他画布,并用 css 隐藏它( opacity: 0; width:0px; height:0px;应该这样做)。

TL;DR TL; 博士

This API's implementations are far from being stabilized, no one has made the request for such a feature yet, it may come in near future though, and can be done server-side for the time being.这个 API 的实现还远没有稳定下来,还没有人提出这样的功能的请求,不过它可能会在不久的将来出现,并且暂时可以在服务器端完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将drawImage与MediaRecorder一起使用时,为什么canvas.captureStream中的视频为空 - Why is video from canvas.captureStream empty when using drawImage with MediaRecorder <video>使用 MediaRecorder() 从<canvas>使用 canvas.captureStream() 在 firefox、chrome 上呈现不同 - <video> playback of recorded stream using MediaRecorder() from <canvas> using canvas.captureStream() renders differently at firefox, chromium Canvas.captureStream() 在 Angular 8 中不存在 - Canvas.captureStream() does not exist in Angular 8 如何检测何时不支持`canvas.captureStream()`? - How to detect when `canvas.captureStream()` is not supported? 将“navigator.mediaDevices.getUserMedia”替换为“canvas.captureStream” - Replace "navigator.mediaDevices.getUserMedia" with "canvas.captureStream" Firefox canvas.captureStream 捕获处于非活动状态的黑帧 - Firefox canvas.captureStream captures black frames as was inactive 使用captureStream和mediaRecorder进行画布录制 - Canvas recording using captureStream and mediaRecorder 使用画布捕获视频截图 - Capture video screenshot using canvas 没有Alpha通道的画布toDataURL - Canvas toDataURL without alpha channel 使用 HtmlMediaElement.captureStream() 从视频中获取音频 - Using HtmlMediaElement.captureStream() to get the audio from a video
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM