-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMediaSource.html
executable file
·73 lines (64 loc) · 1.81 KB
/
MediaSource.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<style>
video{
width: 300px;
height: 400px;
}
</style>
<video controls autoplay ></video>
<script>
const video = document.querySelector('video')
var mediaSource = new MediaSource();
video.src = URL.createObjectURL(mediaSource)
// 转换mp4为fmp4
// ffmpeg -i video.mp4 -movflags empty_moov+default_base_moof+frag_keyframe video-fragmented.mp4
mediaSource.onsourceopen = function(e) {
console.log(e)
var sourceBuffer = mediaSource.addSourceBuffer('video/mp4; codecs="avc1.64001e, mp4a.40.2"')
video.onloadedmetadata = () => {
video.play()
}
fetch('./f1.mp4').then(res => {
return res.body
// return res.arrayBuffer()
})
// .then(res => {
// console.log(res)
// sourceBuffer.appendBuffer(res)
// })
.then(async body => {
r = body.getReader()
console.log(r)
let b = 100000
while(true) {
const {done, value} = await r.read()
console.log('val', value, done)
if(value) {
let a = value
while(a.byteLength) {
sourceBuffer.appendBuffer(a.subarray(0,b))
await new Promise(r => {
setTimeout(r, 100)
})
console.log('fasdfafs')
console.log(a.byteLength)
a = a.subarray(b)
}
} else {
mediaSource.endOfStream()
return
}
}
})
}
</script>
</body>
</html>