forked from bilibili/WebAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecord-usermedia.ts
79 lines (70 loc) · 2.34 KB
/
record-usermedia.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Combinator } from '@webav/av-cliper'
import { AVRecorder } from '../src/av-recorder'
; (async () => {
if (!(await Combinator.isSupported())) {
alert('Your browser does not support WebCodecs')
}
})()
let recorder: AVRecorder | null = null
const startEl = document.querySelector('#startRecod') as HTMLButtonElement
startEl?.addEventListener('click', () => {
; (async () => {
const mediaStream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: true
})
const recodeMS = mediaStream.clone()
const vEl = document.querySelector('video') as HTMLVideoElement
vEl.muted = true
vEl.srcObject = mediaStream
vEl.play().catch(console.error)
recorder = new AVRecorder(recodeMS, {
width: 1280,
height: 720
})
await recorder.start()
const writer = await createFileWriter('mp4')
recorder.outputStream?.pipeTo(writer).catch(console.error)
startEl.style.visibility = 'hidden'
pauseEl.style.visibility = 'visible'
continueEl.style.visibility = 'hidden'
})().catch(console.error)
})
const stopEl = document.querySelector('#stopRecod') as HTMLButtonElement
stopEl?.addEventListener('click', () => {
; (async () => {
await recorder?.stop()
alert('save done')
startEl.style.visibility = 'visible'
pauseEl.style.visibility = 'hidden'
continueEl.style.visibility = 'hidden'
})().catch(console.error)
})
const pauseEl = document.querySelector('#pauseRecod') as HTMLButtonElement
pauseEl?.addEventListener('click', () => {
; (async () => {
if (recorder == null) return
recorder.pause()
startEl.style.visibility = 'hidden'
pauseEl.style.visibility = 'hidden'
continueEl.style.visibility = 'visible'
})().catch(console.error)
})
const continueEl = document.querySelector('#continueRecod') as HTMLButtonElement
continueEl?.addEventListener('click', () => {
; (async () => {
if (recorder == null) return
recorder.resume()
startEl.style.visibility = 'hidden'
pauseEl.style.visibility = 'visible'
continueEl.style.visibility = 'hidden'
})().catch(console.error)
})
async function createFileWriter(
extName: string
): Promise<FileSystemWritableFileStream> {
const fileHandle = await window.showSaveFilePicker({
suggestedName: `WebAV-export-${Date.now()}.${extName}`
})
return fileHandle.createWritable()
}