-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo-capture-example.html
225 lines (167 loc) · 5.8 KB
/
video-capture-example.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
<!DOCTYPE html>
<html>
<head>
<title>Video Capture Example</title>
<meta name='viewport' content='width=device-width, initial-scale=1.0' />
<!-- <script src='./resources/app.js'></script>
<link rel='stylesheet' type='text/css' href='./resources/style.css'> -->
<style type='text/css'>
</style>
</head>
<body>
<video id='video' width='1280' height='720' autoplay>
Your browser does not support the video tag.
</video>
<button id='snap'>Grab a Video</button>
<br>
<canvas id='canvas' width='320' height='240'></canvas>
<section>
<div style="float:left;">
<button id="camera-me">1. Turn on camera</button>
<h4><code>getUserMedia()</code> feed</h4>
<video autoplay></video>
</div>
<div id="video-preview">
<button id="record-me" disabled>2. Record<!--⚫--></button>
<button id="stop-me" disabled>◼</button>
<!--<button id="play-me" disabled>►</button>-->
<span id="elasped-time"></span>
<h4>.webm recording (no audio)</h4>
</div>
</section>
</body>
<script>
var context; // Lazy/dumb hoisting
//Standarized the getUserMedia function across all of the different browsers
navigator.getUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
/** check if url is https or localhost and alert the user that we will not be able to access the microphone */
var audioCaptureInitializationCheck = function () {
var url = window.location.href;
var secureUrlIdentifier = 'https';
var localhostIdentifier = 'localhost';
if (url.indexOf(secureUrlIdentifier) === -1) {
if (url.indexOf(localhostIdentifier) === -1){
alert('Either need https or localhost to capture from the camera and microphone')
}
else {
return true;
}
}
else {
return true;
}
}
// requestAnimationFrame = requestAnimationFrame ||
// webkitRequestAnimationFrame || mozRequestAnimationFrame ||
// msRequestAnimationFrame || oRequestAnimationFrame;
// cancelAnimationFrame = cancelAnimationFrame ||
// webkitCancelAnimationFrame || mozCancelAnimationFrame ||
// msCancelAnimationFrame || oCancelAnimationFrame;
var ORIGINAL_DOC_TITLE = document.title;
var video = document.getElementById('video');
var canvas = document.createElement('canvas'); // offscreen canvas.
var rafId = null;
var startTime = null;
var endTime = null;
var frames = [];
function $(selector) {
return document.querySelector(selector) || null;
}
function toggleActivateRecordButton() {
debugger
var b = document.getElementById('snap');
b.value = b.disabled ? 'Record' : 'Recording...';
b.classList.toggle('recording');
b.disabled = !b.disabled;
}
function turnOnCamera(e) {
debugger
e.target.disabled = true;
document.getElementById('snap').disabled = false;
video.controls = false;
var finishVideoSetup_ = function() {
// TODO: Verify that this bug still exists and create a code example that supports https otherwise
// Note: video.onloadedmetadata doesn't fire in Chrome when using getUserMedia so
// we have to use setTimeout. See crbug.com/110938.
setTimeout(function() {
video.width = 320;//video.clientWidth;
video.height = 240;// video.clientHeight;
canvas.width = video.width;
canvas.height = video.height;
}, 1000);
};
navigator.getUserMedia({video: true, audio: true}, function(stream) {
video.src = window.URL.createObjectURL(stream);
finishVideoSetup_();
}, function(e) {
alert('Something seriously went wrong');
});
};
//Everything above is working
function record() {
debugger
var elapsedTime = $('#elasped-time');
var ctx = canvas.getContext('2d');
var CANVAS_HEIGHT = canvas.height;
var CANVAS_WIDTH = canvas.width;
frames = []; // clear existing frames;
startTime = Date.now();
toggleActivateRecordButton();
$('#stop-me').disabled = false;
function drawVideoFrame_(time) {
rafId = requestAnimationFrame(drawVideoFrame_);
ctx.drawImage(video, 0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
document.title = 'Recording...' + Math.round((Date.now() - startTime) / 1000) + 's';
// Read back canvas as webp.
console.time('canvas.dataURL() took');
var url = canvas.toDataURL('image/webp', 1); // image/jpeg is way faster :(
frames.push(url);
};
rafId = requestAnimationFrame(drawVideoFrame_);
};
function stop() {
debugger
cancelAnimationFrame(rafId);
endTime = Date.now();
$('#stop-me').disabled = true;
document.title = ORIGINAL_DOC_TITLE;
toggleActivateRecordButton();
console.log('frames captured: ' + frames.length + ' => ' +
((endTime - startTime) / 1000) + 's video');
embedVideoPreview();
};
function embedVideoPreview(opt_url) {
debugger
var url = opt_url || null;
var video = $('#video-preview video') || null;
var downloadLink = $('#video-preview a[download]') || null;
if (!video) {
video = document.createElement('video');
video.autoplay = true;
video.controls = true;
video.loop = true;
video.style.width = canvas.width + 'px';
video.style.height = canvas.height + 'px';
$('#video-preview').appendChild(video);
downloadLink = document.createElement('a');
downloadLink.download = 'capture.webm';
downloadLink.textContent = '[ download video ]';
downloadLink.title = 'Download your .webm video';
var p = document.createElement('p');
p.appendChild(downloadLink);
$('#video-preview').appendChild(p);
} else {
window.URL.revokeObjectURL(video.src);
}
if (!url) {
var webmBlob = Whammy.fromImageArray(frames, 1000 / 60);
url = window.URL.createObjectURL(webmBlob);
}
video.src = url;
downloadLink.href = url;
}
document.getElementById('record-me').addEventListener('click', record);
document.getElementById('stop-me').addEventListener('click', stop);
document.getElementById('snap').addEventListener('click', turnOnCamera);
</script>
</html>