Last active
August 12, 2021 02:07
-
-
Save gee1k/2faec4a4fb66022f6f205df444cee3ee to your computer and use it in GitHub Desktop.
纯JS录制Video标签播放的视频
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Decoder, tools, Reader } from './EBML.js' | |
// EBML: https://www.webrtc-experiment.com/EBML.js | |
/** | |
* @param {Blob} file - File or Blob object. | |
* @param {function} callback - Callback function. | |
* @example | |
* getSeekableBlob(blob or file, callback); | |
* @see {@link https://github.com/muaz-khan/RecordRTC|RecordRTC Source Code} | |
*/ | |
export function getSeekableBlob(inputBlob, callback) { | |
const reader = new Reader() | |
const decoder = new Decoder() | |
const tool = tools | |
const fileReader = new FileReader() | |
fileReader.onload = function () { | |
const ebmlElms = decoder.decode(this.result) | |
ebmlElms.forEach(function (element) { | |
reader.read(element) | |
}) | |
reader.stop() | |
const refinedMetadataBuf = tool.makeMetadataSeekable(reader.metadatas, reader.duration, reader.cues) | |
const body = this.result.slice(reader.metadataSize) | |
const newBlob = new Blob([refinedMetadataBuf, body], { | |
type: inputBlob.type | |
}) | |
callback(newBlob) | |
} | |
fileReader.readAsArrayBuffer(inputBlob) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const videoEl = document.getElementById('video') | |
const stream = videoEl.captureStream() | |
const videoRecorder = new MediaRecorder(stream, {mimeType:'video/webm;codecs=vp8,opus'}) | |
let chunks = [] | |
videoRecorder.ondataavailable = function (e) { | |
chunks.push(e.data) | |
} | |
videoRecorder.onstop = function (e) { | |
var blob = new Blob(chunks, { 'type' : videoRecorder.mimeType }) | |
let a = document.createElement('a') | |
a.href = URL.createObjectURL(blob) | |
a.download = `test.mp4` | |
a.click() | |
chunks = [] | |
} | |
// 开始录制 | |
videoRecorder.start() | |
// 暂停录制 | |
videoRecorder.pause() | |
// 继续录制 | |
videoRecorder.resume() | |
// 停止录制 | |
videoRecorder.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment