-
Notifications
You must be signed in to change notification settings - Fork 3
/
timelapse.html
213 lines (178 loc) · 6.41 KB
/
timelapse.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
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="w3.css">
<link rel="stylesheet" href="range.css" media="screen" />
<link rel="manifest" href="manifest.json">
<head>
<meta name=viewport content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Time Lapse demo</title>
</head>
<body>
<div class="w3-container w3-orange">Time Lapse WebApp.</div>
<div class="w3-row">
<!-- onfocus trick to get onchange events also when nothing changes-->
<select id="videoSource" class="w3-select w3-half w3-green"
onfocus="this.selectedIndex = -1;">
<option value="" disabled selected hidden>Choose video source</option>
</select>
<!--
<input type="button" id="startStreaming" class="w3-btn-primary w3-rest w3-teal w3-round"
onclick="getUserMedia()" value="Start streaming">
-->
</div>
<div class="w3-row-padding w3-center">
<div class="w3-container w3-half">
<div class="w3-container w3-card-4">
<video id="videotag" class="w3-round w3-border" width="240" height="180" autoplay=true> </video>
<div>
<input type="button" id="startRecording" class="w3-btn-primary w3-teal w3-round"
onclick="startRecording()" value="Start Recording">
</div>
</div>
</div>
<div class="w3-container w3-half">
<div class="w3-container w3-card-4">
<canvas id="canvas" class="w3-round w3-border w3-card-4" width="240"></canvas>
<div>
<input type="button" id="stopRecording" class="w3-btn-primary w3-teal w3-round"
onclick="stopRecording()" value="Stop Recording">
</div>
</div>
</div>
</div>
<div class="w3-container w3-row">
<div class="w3-quarter">
<select id="interval" class="w3-select">
<option value="1000" disabled selected hidden>Capture interval (ms)</option>
<option value="1000">1s</option>
<option value="2000">2s</option>
<option value="5000">5s</option>
</select>
</div>
<div class="w3-quarter">
<select id="duration" class="w3-select" placeholder="Miguelao">
<option value="Infinity" disabled selected hidden>Capture duration (s)</option>
<option value="Infinity">Infinity</option>
<option value="100">100s</option>
</select>
</div>
</div>
<script type="text/javascript" src="dimsum.js"></script>
<script>
var theStream;
var theImageCapturer;
var theCanvasStream;
var theRecorder;
var exposureIntervalMs = 1000;
var exposureDurationS = Infinity;
var videoSelect = document.querySelector('select#videoSource');
videoSelect.onchange = getUserMedia;
var recordedChunks = [];
document.getElementById("stopRecording").disabled = true;
////////////////////////////////////////////////////////////////////////////////
// This Section is all about recording.
function startRecording() {
var canvas = document.getElementById("canvas");
theCanvasStream = canvas.captureStream();
try {
theRecorder = new MediaRecorder(theCanvasStream, {forcedVideoFrameRate : 30.0});
} catch (e) {
console.assert(false, 'Exception while creating MediaRecorder: ' + e);
return;
}
document.getElementById("startRecording").disabled = true;
document.getElementById("stopRecording").disabled = false;
theRecorder.ondataavailable = onDataAvailable;
theRecorder.onstop = stopRecording;
theRecorder.start();
console.log("Recorder started");
var interval = document.getElementById("interval");
exposureIntervalMs = interval.value;
console.log('Exposure interval: ' + exposureIntervalMs + 'ms');
var duration = document.getElementById("duration");
exposureDurationS = duration.value;
console.log('Exposure duration: ' + exposureDurationS + ' seconds');
setTimeout(() => { grabFrame(); }, exposureIntervalMs);
}
function onDataAvailable(event) {
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 stopRecording() {
document.getElementById("startRecording").disabled = false;
document.getElementById("stopRecording").disabled = true;
console.log('Stopping recording and downloading');
theRecorder.stop();
//theStream.getVideoTracks()[0].stop();
setTimeout(() => {saveByteArray(recordedChunks, 'test.webm');}, 100);
}
////////////////////////////////////////////////////////////////////////////////
function grabFrame() {
theImageCapturer.grabFrame()
.then(function(frame) {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.drawImage(frame, 0, 0, frame.width, frame.height,
0, 0, canvas.width, canvas.height);
if (theRecorder.state == 'recording')
setTimeout(() => { grabFrame(); }, exposureIntervalMs);
});
}
////////////////////////////////////////////////////////////////////////////////
// Down below it's all the stuff to do with getUserMedia().
function gotStream(stream) {
if (theStream)
theStream.getTracks().forEach((track) => { track.stop(); });
theStream = stream;
var videoTag = document.getElementById("videotag");
videoTag.src = URL.createObjectURL(theStream);
theImageCapturer = new ImageCapture(theStream.getVideoTracks()[0]);
}
function getUserMedia() {
var videoSource = videoSelect.value;
console.log("opening camera with id: " + videoSource);
var constraints = {
"audio": false,
"video": {
deviceId: videoSource ? {exact: videoSource} : undefined,
width: { exact: 640 },
height: { exact : 480 }
}
};
navigator.getUserMedia(constraints, gotStream, (e) => {
console.error('User media request denied with error code ' + e);
});
};
function refreshSources() {
navigator.mediaDevices.enumerateDevices()
.then((devices) => {
devices.forEach((device) => {
console.log( device.label + ' ' + device.kind);
if (device.kind == 'videoinput') {
var option = document.createElement('option');
option.value = device.deviceId;
option.text = device.label || 'Camera #' + (videoSelect.length + 1);
console.log("cam [" + option.text + "] with id: " + option.value);
videoSelect.appendChild(option);
}
});
})
.catch((err) => {
console.log(err.name + ": " + err.message);
});
}
refreshSources();
</script>
<div class="w3-container w3-sand">(Note: make sure to use https://)</div>
</html>