将视频blob保存到文件系统电子/节点js [英] Save video blob to filesystem electron/node js

查看:360
本文介绍了将视频blob保存到文件系统电子/节点js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的电子应用程序中,用户可以使用MediaRecorder API从他们的网络摄像头录制视频。

In my electron application, users can record video from their webcam using the MediaRecorder API.

当用户点击停止记录按钮时,我会得到一个blob记录的视频。

When the user hit the "stop record" button, I get a blob of the recorded video.

我想要做的是将此blob转换为真正的webm视频并将其写入用户的文件系统,例如:

What i would like to do is to convert this blob to a real webm video and write it into the user's filesystem with for example :

fs.writeFile(localdir + '\\video.webm', videoBlob); // does not work

以下示例与我从网络摄像头获取的base64图像快照非常有效,但我不能使它与我得到的视频blob一起工作。

The example below works pretty well with base64 image snapshot that I get from the webcam, but i can't make it work with the video blob that I get.

感谢您的启发!

推荐答案

创建视频blob。 chunk 是来自 MediaRecorder 实例的 ondataavailable

Create a video blob. chunks is an array of event.data from a MediaRecorder instance's ondataavailable

var blob = new Blob(chunks, {
    type: 'video/webm'
})

将blob作为 ArrayBuffer 读取,并使用它来创建缓冲区。将缓冲区保存为文件。

Read the blob as an ArrayBuffer, and use that to create a Buffer. Save the buffer as a file.

var reader = new FileReader()
reader.onload = function(){
    var buffer = new Buffer(reader.result)
    fs.writeFile(path, buffer, {}, (err, res) => {
        if(err){
            console.error(err)
            return
        }
        console.log('video saved')
    })
}
reader.readAsArrayBuffer(blob)

这篇关于将视频blob保存到文件系统电子/节点js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆