Fra*_*ssi 8 html javascript html5-canvas vue.js web-mediarecorder
我需要执行以下操作:
从 a 获取视频<video>并在 a 中播放<canvas>
将画布中的流记录为 Blob
就是这样。第一部分还可以。
对于第二部分,我成功录制了一个 Blob。问题是 Blob 是空的。
<video id="video" controls="true" src="http://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv"></video>
<canvas id="myCanvas" width="532" height="300"></canvas>
Run Code Online (Sandbox Code Playgroud)
// Init
console.log(MediaRecorder.isTypeSupported('video/webm')) // true
const canvas = document.querySelector("canvas")
const ctx = canvas.getContext("2d")
const video = document.querySelector("video")
// Start the video in the player
video.play()
// On play event - draw the video in the canvas
video.addEventListener('play', () => {
function step() {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
requestAnimationFrame(step)
}
requestAnimationFrame(step);
// Init stream and recorder
const stream = canvas.captureStream()
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm',
});
// Get the blob data when is available
let allChunks = [];
recorder.ondataavailable = function(e) {
console.log({e}) // img1
allChunks.push(e.data);
}
// Start to record
recorder.start()
// Stop the recorder after 5s and check the result
setTimeout(() => {
recorder.stop()
const fullBlob = new Blob(allChunks, { 'type' : 'video/webm' });
const downloadUrl = window.URL.createObjectURL(fullBlob)
console.log({fullBlob}) // img2
}, 5000);
})
Run Code Online (Sandbox Code Playgroud)
这是事件console.log的经过ondataavailable:
这是console.logBlob 的:
这是 JSFiddle。您可以在控制台查看结果:
https://jsfiddle.net/1b7v2pen/
此行为(Blob 数据大小:0)发生在 Chrome 和 Opera 上。
在 Firefox 上,它的行为略有不同。它记录一个非常小的视频 Blob(725 字节)。视频长度本来应该是5秒,但就是黑屏。
从画布记录流的正确方法是什么?
代码有问题吗?
为什么 Blob 出来时是空的?
MediaRecorder.stop()是一种异步方法。
在停止算法中,有一个对requestData的调用,它本身会将任务排队以触发事件dataavailable以及自上次此类事件以来的当前可用数据。
这意味着在您同步调用MediaRecorder#stop()之后,最后抓取的数据还不会成为您的allChunks数组的一部分。不久之后它们就会变得如此(通常在同一个事件循环中)。
因此,当您要保存从 MediaRecorder 制作的录音时,请务必始终从 MediaRecorder的onstop事件构建最终的 Blob,这将表明 MediaRecorder 实际上已结束,确实触发了其最后一个dataavailable事件,并且一切都很好。
我一开始错过的一件事是,您正在请求跨域视频。如果没有正确的跨源请求,这样做会使您的画布(和 MediaElement)受到污染,因此您的 MediaStream 将被静音。
由于您尝试请求的视频来自维基媒体,因此您可以简单地将其作为跨源资源进行请求,但对于其他资源,您必须确保服务器配置为允许这些请求。
const canvas = document.querySelector("canvas")
const ctx = canvas.getContext("2d")
const video = document.querySelector("video")
// Start the video in the player
video.play()
// On play event - draw the video in the canvas
video.addEventListener('play', () => {
function step() {
ctx.drawImage(video, 0, 0, canvas.width, canvas.height)
requestAnimationFrame(step)
}
requestAnimationFrame(step);
// Init stream and recorder
const stream = canvas.captureStream()
const recorder = new MediaRecorder(stream, {
mimeType: 'video/webm',
});
// Get the blob data when is available
let allChunks = [];
recorder.ondataavailable = function(e) {
allChunks.push(e.data);
}
recorder.onstop = (e) => {
const fullBlob = new Blob(allChunks, { 'type' : 'video/webm' });
const downloadUrl = window.URL.createObjectURL(fullBlob)
console.log({fullBlob})
console.log({downloadUrl})
}
// Start to record
recorder.start()
// Stop the recorder after 5s and check the result
setTimeout(() => {
recorder.stop()
}, 5000);
})Run Code Online (Sandbox Code Playgroud)
<!--add the 'crossorigin' attribute to your video -->
<video id="video" controls="true" src="https://upload.wikimedia.org/wikipedia/commons/7/79/Big_Buck_Bunny_small.ogv" crossorigin="anonymous"></video>
<canvas id="myCanvas" width="532" height="300"></canvas>Run Code Online (Sandbox Code Playgroud)
另外,我不得不指出,如果您不在画布上进行任何特殊绘图,您可能希望直接保存视频源,或者至少直接记录 <video> 的 captureStream MediaStream。