-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmediarecorder.html
142 lines (124 loc) · 4.97 KB
/
mediarecorder.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
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="w3.css">
<head>
<meta name=viewport content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MediaStream Recoder demo (w/ MediaSource)</title>
</head>
<body>
<div class="w3-container w3-orange"> Record Real-Time video (no audio) and play it back or download as file.</div>
<div class="w3-row">
<div class="w3-teal w3-container w3-quarter">Video Encoder</div>
<div class="w3-container">
<select id="encoder" class="w3-select w3-threequarter w3-pale-green ">
<option value="video/webm;codecs=vp8">VP8</option>
<option value="video/webm;codecs=vp9">VP9</option>
<option value="video/webm;codecs=h264">H264</option>
<option selected value="video/webm">(none/free choice)</option>
</select>
</div>
</div>
</body>
<script type="text/javascript" src="dimsum.js"></script>
<script>
/* global Blob */
/* global MediaRecorder */
/* global MediaSource */
/* global URL */
var recorder;
var theStream;
//
// Idea from: http://codepen.io/anon/pen/gpmPzm?editors=101, stuff received
// data in |chunks|, then blobify it and plug the result in a <video>.
//
// The alternative is, e.g. [1, 2], using MediaSource where we basically pass
// recorded chunks one by one into a MediaSource associated to a <video>.
// MediaRecorder supposedly produces BlobEvents and Blobs are not friendly to
// MediaSource-SourceBuffer, so there's an Experimental CL sending stuff as
// an Uint8ArrayEvent.
//
// [1] http://html5-demos.appspot.com/static/media-source.html
// [2] https://github.com/html5rocks/www.html5rocks.com/blob/master/content/tutorials/streaming/multimedia/en/index.md
var recordedChunks = [];
var theMimeType;
var mediaSource = new MediaSource();
var sourceBuffer;
function mediaSourceOpened(e) {
console.log('MediaSource opened correctly');
var codec = document.getElementById("encoder");
sourceBuffer = mediaSource.addSourceBuffer("video/webm");
}
mediaSource.addEventListener('sourceopen', mediaSourceOpened, false);
createButton("btn", "Start playback", makeGetStreamX(640, 480, "btn", getUserMediaOkCallback));
document.body.appendChild(document.createElement("br"));
createVideoTag('localview', 320, 240, '');
createVideoTag('video', 320, 240, mediaSource);
document.body.appendChild(document.createElement("br"));
createButton("btn2", "Stop recording and play back", stopStreamsAndPlaybackData);
document.getElementById("btn2").disabled = true;
createButton("btn3", "Stop recording and download data", stopStreamsAndDownloadData);
document.getElementById("btn3").disabled = true;
////////////////////////////////////////////////////////////////////////////////
function getUserMediaOkCallback(stream) {
console.log("getUserMedia succeeded :)");
theStream = stream;
document.getElementById("localview").src = URL.createObjectURL(stream);
document.getElementById("btn").disabled = true;
document.getElementById("btn2").disabled = false;
document.getElementById("btn3").disabled = false;
const theMimeType = encoder.options[encoder.selectedIndex].value;
try {
recorder = new MediaRecorder(stream, {mimeType : theMimeType});
} catch (e) {
console.assert(false, 'Exception while creating MediaRecorder: ' + e);
return;
}
console.assert(recorder.state == "inactive");
recorder.ondataavailable = recorderOnDataAvailable;
recorder.onstop = recorderOnStop;
recorder.start();
console.log("Recorder is started, mimeType: " + theMimeType);
console.assert(recorder.state == "recording");
}
function recorderOnDataAvailable(event) {
// |event.data.size| can be printed out for debugging..
recordedChunks.push(event.data);
}
function saveByteArray(data, name) {
var blob = new Blob(data, {type: "video/webm"});
var url = URL.createObjectURL(blob);
var a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
a.href = url;
a.download = name;
a.click();
URL.revokeObjectURL(url);
}
function stopStreamsAndPlaybackData() {
document.getElementById("btn2").disabled = true;
document.getElementById("btn3").disabled = true;
console.log('Stopping record and starting playback');
recorder.stop();
theStream.getVideoTracks()[0].stop();
// sourceBuffer.appendBuffer(recordedChunks);
// Or...
var superBuffer = new Blob(recordedChunks);
document.getElementById("video").src = window.URL.createObjectURL(superBuffer);
}
function stopStreamsAndDownloadData() {
document.getElementById("btn2").disabled = true;
document.getElementById("btn3").disabled = true;
console.log('Stopping record and starting playback');
recorder.stop();
theStream.getVideoTracks()[0].stop();
if (encoder.options[encoder.selectedIndex].value == "video/x-matroska;codecs=h264" ||
encoder.options[encoder.selectedIndex].value == "video/x-matroska;codecs=jpeg") {
setTimeout(() => {saveByteArray(recordedChunks, 'test.mkv');}, 100);
} else {
setTimeout(() => {saveByteArray(recordedChunks, 'test.webm');}, 100);
}
}
</script>
</html>