-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.html
281 lines (258 loc) · 8.68 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>直接播放flv</title>
<style>
* {
padding: 0px;
margin: 0px;
}
html,
body {
height: 100%;
width: 100%;
}
body #con {
width: 1000px;
height: 400px;
line-height: 400px;
text-align: center;
background-color: aquamarine;
/*border-radius: 10px;*/
position: relative;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 18px;
}
div {
height: 100%;
width: 100%;
}
#con #drop {
width: 50%;
float: left;
}
#con video {
width: 50%;
height: 100%;
float: left;
}
#input {
width: 100%;
height: 40px;
position: absolute;
bottom: -40px;
background-color: #fff;
border: 1px solid #6f6f6f;
line-height: 40px;
text-align: left;
box-sizing: border-box;
}
#input input {
position: relative;
left: 0px;
top: 0px;
height: 34px;
width: 80%;
outline: none;
border: none;
padding: 5px;
font-size: 18px;
box-sizing: border-box;
float: left;
}
#btn_load {
width: 20%;
float: left;
text-align: center;
height: 100%;
background-color: rgb(18, 120, 189);
color: white;
user-select: false;
cursor: default;
}
</style>
<script>
var vcode='avc1.640020';
var acode='mp4a.40.2'
var mimecode = 'video/mp4; codecs=';
var sb = null;
var mes = null;
</script>
</head>
<body>
<div id='con'>
<div id='drop'>将flv文件拖入此处</div>
<video autoplay="autoplay" controls="controls" style="background-color: #000000;"></video>
<div id='input'>
<input id='videosrc' type="text" placeholder="请输入flv地址">
<div id='btn_load'>加载</div>
</div>
</div>
<a href=""></a>
<!--<video autoplay="autoplay" width="500px" height="400px" controls="controls" style="background-color: #000000;"></video>-->
<script type="text/javascript" src="bound.js"></script>
<script>
// console.log(flvParse);
var video = document.querySelector('video');
var resultU8t=null;
var _this = this;
var req = null;
var dropbox = document.querySelector('body #con div');
var btn_load = document.querySelector("#btn_load");
var temp_u8a = new Uint8Array();
var headers = new window.Headers();
var _requestAbort = false;
var init=false;
var error=false;
btn_load.addEventListener("click", function() {
req = new Request(document.querySelector("#videosrc").value, {
headers: headers,
method: 'GET',
cache: 'default',
mode: "cors"
});
fetch(req).then(function(response) {
// typeof(response.body)==ReadableStream
var reader = response.body.getReader();
return _this.readr(reader);
})
})
var temp_arr = []
function readr(reader) {
return reader.read().then(function(result) {
var chunk = result.value; ///Uint8Array
if(error){
return reader.cancel();
}
if (result.done) {
console.log("this's over");
return reader.cancel();
} else if (_requestAbort) {
_requestAbort = false;
return reader.cancel();
}
var temp = new Uint8Array(temp_u8a.length + chunk.length);
temp.set(temp_u8a, 0);
temp.set(chunk, temp_u8a.length);
var offest = flvParse.setFlv(temp);
temp_u8a = temp.slice(offest);
console.log('offest', offest, '数据长度', temp.length)
return _this.readr(reader);
})
}
dropbox.addEventListener("dragover", function(e) {
e.stopPropagation();
e.preventDefault();
}, false);
dropbox.addEventListener("drop", function(e) {
e.stopPropagation();
e.preventDefault();
var reader = new FileReader();
reader.addEventListener("load", processflv, false);
reader.readAsArrayBuffer(e.dataTransfer.files[0]);
}, false);
function processflv(e) {
var buffer = e.target.result;
var uint8 = new Uint8Array(buffer);
//将拖拽入的flv视频转成了2进制数组
flvParse.setFlv(uint8); //仍入转换器
// var flv2mp4 = new flv2fmp4();
// flv2mp4.setflv(buffer);
// flv2mp4.onInitSegment = onInitSegment.bind(_this);
// flv2mp4.onMediaSegment = onMediaSegment.bind(_this);
}
function onMediaInfo(a,b){
if(mes==null){
mes= new MediaSource;
mes.addEventListener('sourceopen', sourceOpen);
mes.addEventListener('sourceended',function(e){
console.log('mes',e,mes)
})
if(b.hasVideo&&b.hasAudio){
mimecode+='"'+(a.videoCodec||vcode)+','+(a.audioCodec||acode)+'"'
}
if(b.hasVideo&&!b.hasAudio){
mimecode+='"'+a.videoCodec||vcode+'"'
}
if(!b.hasVideo&&b.hasAudio){
mimecode+='"'+a.audioCodec||acode+'"'
}
function sourceOpen() {
sb = mes.addSourceBuffer(mimecode);
sb.addEventListener('updateend', () => {
doappendbuff();
console.log('加载完成');
});
sb.addEventListener('error', (e) => {
console.log(e,'报错',JSON.stringify(e))
error=true;
});
if(temp_arr.length>0){
doappendbuff();
}
}
video.src = URL.createObjectURL(mes);
}
}
/**
* 多次操作只有delay秒后才会触发
*/
function debounce (fn, delay){
var timer = null;
return function(...args){
clearTimeout(timer);
timer = setTimeout(() => fn.apply(this, args), delay);
}
}
function mp4Init(a,b) {
resultU8t=new Uint8Array(a.length);
resultU8t.set(a,0);
console.log('init', a);
temp_arr.unshift(a.buffer);
// console.log("moov"+URL.createObjectURL(new Blob([a.buffer])));
// var hrefa=document.createElement('a');
// hrefa.href=URL.createObjectURL(new Blob([a.buffer]));
// hrefa.innerHTML='moov下载';
// hrefa.style.display='block';
// document.body.appendChild(hrefa);
doappendbuff();
if(!init)init=true;
}
function outMp4(){
var hrefa=document.createElement('a');
hrefa.style.display='block';
hrefa.href=URL.createObjectURL(new Blob([resultU8t.buffer]));
hrefa.innerHTML='mp4下载';
document.body.appendChild(hrefa);
}
function onMediaSegment(a) {
console.log('moof', a);
// var temp=new Uint8Array(resultU8t.length+a.length);
// temp.set(resultU8t,0);
// temp.set(a,resultU8t.length);
//
// resultU8t=new Uint8Array(temp.length);
// resultU8t.set(temp,0);
// end();
temp_arr.push(a.buffer);
if(!init)return;
doappendbuff();
}
function doappendbuff() {
if (sb&&!sb.updating && temp_arr.length > 0) {
console.log('开始添加');
sb.appendBuffer(temp_arr.shift());
} else {
console.log('sourcebuff还在忙碌');
}
if(!sb){
console.log('sb还没准备好');
}
}
var end=debounce(outMp4,2000);
</script>
</body>
</html>