将多个视频Blob转换为一个并保存

时间:2018-10-28 09:31:48

标签: javascript electron blob mp4 webm

我记录屏幕并在可用时将blob推入数组。最后,我想将其保存到webm或mp4文件中。我尝试将其转换为新的Blob,然后将其保存为arraybuffer,但这没有解决。

function recordScreen() {
        blobs = [];
        recorder = new MediaRecorder(localstream);
        recorder.ondataavailable = function(event) {
            blobs.push(event.data);
        }
        setTimeout(() => {
            stopRecording();
        }, 7000);
}

    function stopRecording() {
        recorder.stop();
        console.log(blobs);
        //But how to save it to a mp4 or a webm?
    }

//更新 好的,现在它已写入文件,但无法播放。

function recordScreen() {
    recorder = new MediaRecorder(localstream);
    recorder.ondataavailable = function(event) {
        chunks.push(event.data);
    }
    recorder.start();
    setTimeout(() => {
        stopRecording();
    }, 7000);
}

function stopRecording() {
    recorder.stop();
    setTimeout(() => {
        toArrayBuffer(new Blob(chunks, {type: 'video/webm'}), function(ab) {
            var buffer = toBuffer(ab);
            var file = `example.webm`;
            fs.writeFile(file, buffer, function(err) {
                if (err) {
                    console.error('Failed to save video ' + err);
                } else {
                    console.log('Saved video: ' + file);
                }
            });
        });
    }, 1000);
}

function toArrayBuffer(blob, cb) {
    let fileReader = new FileReader();
    fileReader.onload = function() {
        let arrayBuffer = this.result;
        cb(arrayBuffer);
    };
    fileReader.readAsArrayBuffer(blob);
}

function toBuffer(ab) {
    return Buffer.from(ab);
}

0 个答案:

没有答案