hydra-synth
Version:
base synth for hydra-editor
87 lines (75 loc) • 2.91 kB
JavaScript
class VideoRecorder {
constructor(stream) {
this.mediaSource = new MediaSource()
this.stream = stream
// testing using a recording as input
this.output = document.createElement('video')
this.output.autoplay = true
this.output.loop = true
let self = this
this.mediaSource.addEventListener('sourceopen', () => {
console.log('MediaSource opened');
self.sourceBuffer = self.mediaSource.addSourceBuffer('video/webm; codecs="vp8"');
console.log('Source buffer: ', sourceBuffer);
})
}
start() {
// let options = {mimeType: 'video/webm'};
// let options = {mimeType: 'video/webm;codecs=h264'};
let options = {mimeType: 'video/webm;codecs=vp9'};
this.recordedBlobs = []
try {
this.mediaRecorder = new MediaRecorder(this.stream, options)
} catch (e0) {
console.log('Unable to create MediaRecorder with options Object: ', e0)
try {
options = {mimeType: 'video/webm,codecs=vp9'}
this.mediaRecorder = new MediaRecorder(this.stream, options)
} catch (e1) {
console.log('Unable to create MediaRecorder with options Object: ', e1)
try {
options = 'video/vp8' // Chrome 47
this.mediaRecorder = new MediaRecorder(this.stream, options)
} catch (e2) {
alert('MediaRecorder is not supported by this browser.\n\n' +
'Try Firefox 29 or later, or Chrome 47 or later, ' +
'with Enable experimental Web Platform features enabled from chrome://flags.')
console.error('Exception while creating MediaRecorder:', e2)
return
}
}
}
console.log('Created MediaRecorder', this.mediaRecorder, 'with options', options);
this.mediaRecorder.onstop = this._handleStop.bind(this)
this.mediaRecorder.ondataavailable = this._handleDataAvailable.bind(this)
this.mediaRecorder.start(100) // collect 100ms of data
console.log('MediaRecorder started', this.mediaRecorder)
}
stop(){
this.mediaRecorder.stop()
}
_handleStop() {
//const superBuffer = new Blob(recordedBlobs, {type: 'video/webm'})
// const blob = new Blob(this.recordedBlobs, {type: 'video/webm;codecs=h264'})
const blob = new Blob(this.recordedBlobs, {type: this.mediaRecorder.mimeType})
const url = window.URL.createObjectURL(blob)
this.output.src = url
const a = document.createElement('a')
a.style.display = 'none'
a.href = url
let d = new Date()
a.download = `hydra-${d.getFullYear()}-${d.getMonth() + 1}-${d.getDate()}-${d.getHours()}.${d.getMinutes()}.${d.getSeconds()}.webm`
document.body.appendChild(a)
a.click()
setTimeout(() => {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 300);
}
_handleDataAvailable(event) {
if (event.data && event.data.size > 0) {
this.recordedBlobs.push(event.data);
}
}
}
export default VideoRecorder