This repository has been archived by the owner on Mar 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
demo-flv.html
140 lines (121 loc) · 4.11 KB
/
demo-flv.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
<html>
<head>
<meta charset="utf-8">
<script src="build/ts/rtmp.js"></script>
</head>
<body>
<label for="mediasource">MediaSource: </label><input type="checkbox" id="mediasource">
<br>
<label for="source">File: </label><select id="source">
<option>media/20051210-w50s_56K.flv</option> <!-- from http://www.mediacollege.com/adobe/flash/video/tutorial/example-flv.html -->
<option>media/barsandtone.flv</option>
<option>media/big_buck_bunny_720p_h264.flv</option> <!-- from http://707foley.co/gallery3/index.php/Video/big_buck_bunny_720p_h264 -->
</select>
<button onclick="play(document.getElementById('source').value)">Play</button>
<br>
Or <label for="file">Select local: </label><input type="file" id="file" onchange="playFile()">
<br>
<video id="video" autoplay controls></video>
<br>
<h1>Notes</h1>
<ul>
<li>Needs Firefox with FFMPEG enabled, see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1107264">Bug 1107264</a>.
(I had to compile ffmpeg at release/2.5 branch configured with
<code>./configure --enable-debug --enable-shared --disable-stripping --enable-nonfree --enable-libmp3lame --prefix=/Users/yury/Work/junk/ffmpeg-bin/</code>
and run Firefox with
<code>DYLD_LIBRARY_PATH=/Users/yury/Work/junk/ffmpeg-bin/lib/ ./mach run</code>)</li>
<li>VP6 is enabled at <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=1107339">Bug 1107264</a></li>
</ul>
<script>
function play(url) {
document.getElementById('video').removeAttribute('src');
isMediaSourceEnabled = document.getElementById('mediasource').checked;
initDecoder();
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
var data = new Uint8Array(xhr.response);
flvParser.push(data);
flvParser.close();
};
xhr.send();
}
function playFile() {
var file = document.getElementById('file').files[0];
play(URL.createObjectURL(file));
}
var mediaSource = null;
var mediaSourceReadyForUpdate = false;
var mediaBuffer = null;
var isMediaSourceEnabled = false;
function initMediaSource() {
mediaSource = new MediaSource();
mediaSource.addEventListener('sourceopen', function(e) {
mediaBuffer = mediaSource.addSourceBuffer('video/mp4');
mediaSourceReadyForUpdate = true;
updateMediaSource();
}.bind(this));
mediaSource.addEventListener('sourceend', function(e) {
mediaSource = null;
});
}
function updateMediaSource() {
if (!mediaSourceReadyForUpdate || mp4File.length === 0) {
return;
}
var bytes = mp4File.shift();
mediaBuffer.appendBuffer(bytes);
mediaSourceReadyForUpdate = false;
mediaBuffer.addEventListener('update', function updateHandler() {
mediaBuffer.removeEventListener('update', updateHandler);
mediaSourceReadyForUpdate = true;
updateMediaSource();
});
}
var flvParser = null;
var mp4Mux = null;
var mp4File = null;
var mimeType = null;
function initDecoder() {
mp4File = [];
if (isMediaSourceEnabled) {
initMediaSource();
document.getElementById('video').src = URL.createObjectURL(mediaSource);
}
flvParser = new RtmpJs.FLV.FLVParser();
flvParser.onHeader = function (header) {
console.log(header);
};
flvParser.onTag = function (data) {
if (data.type === 18) {
var ba = new Shumway.AVM2.AS.flash.utils.ByteArray();
ba.writeRawBytes(data.data);
ba.position = 0;
var name = Shumway.AVM2.AMF0.read(ba);
var value = Shumway.AVM2.AMF0.read(ba);
if (name == 'onMetaData') {
mp4Mux = new RtmpJs.MP4.MP4Mux(RtmpJs.MP4.parseFLVMetadata(value));
mp4Mux.ondata = function onMP4Data(data) {
mp4File.push(data);
if (isMediaSourceEnabled) {
updateMediaSource();
}
};
}
return;
}
mp4Mux.pushPacket(data.type, data.data, data.timestamp);
};
flvParser.onClose = function () {
mp4Mux.flush();
mp4Mux = null;
flvParser = null;
if (!isMediaSourceEnabled) {
document.getElementById('video').src = URL.createObjectURL(new Blob(mp4File, {type: mimeType || 'video/mp4'}));
mp4File = null;
}
};
}
</script>
</body></html>