-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
198 lines (179 loc) · 7.81 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
<html>
<head>
<title>Video Frame Capture</title>
<script>
// Change this value from a "Stop button";
var stopped = false;
var started = false;
var imageFrames = []
function createVideo() {
return document.createElement('video')
}
function createDiv() {
return document.createElement('div')
}
function createCanvas() {
return document.createElement('canvas')
}
function stopVideoCapture() {
stopped = true
}
async function startVideoCapture() {
if(started){
return;
}
started=true
const start = Date.now()
const output = document.querySelector('#output')
output.innerHTML =''
const statusDiv = createDiv()
statusDiv.append('Processing')
output.append(statusDiv)
const video = createVideo()
video.src = 'mov_bbb.mp4' //pass this as input
let seekComplete;
video.onseeked = async (event) => {
if (seekComplete) seekComplete();
/**
* The seeked event is fired when a seek operation completed,
* the current playback position has changed,
* and the Boolean seeking attribute is changed to false.
*/
};
// workaround chromium metadata bug (https://stackoverflow.com/q/38062864/993683)
while (
(video.duration === Infinity || isNaN(video.duration)) &&
video.readyState < 2
) {
await new Promise((r) => setTimeout(r, 1000));
}
video.crossOrigin = "Anonymous";
video.height = 160; // this will be coming from configuration as an input
video.width = 320; // this will be coming from configuration as an input
const canvas = createCanvas();
canvas.width = video.width;
canvas.height = video.height;
var ctx = canvas.getContext('2d');
var track = video.captureStream().getVideoTracks()[0];
var processor = new MediaStreamTrackProcessor(track);
const frameReader = processor.readable.getReader();
stopped = false
const FPS = 25// this will be coming from configuration as an input
const totalExpectedFrames = Math.floor(FPS * video.duration) // this will be coming from configuration as an input
const intervalPerFrame = 1 / FPS; // 0.1 sec
let currentTime = intervalPerFrame;
let i = 0;
output.removeChild(statusDiv)
statusDiv.innerHTML = `Capturing ${i} of ${totalExpectedFrames} frames`
output.append(statusDiv)
video.currentTime = 0.01;
await new Promise(r => seekComplete = r);
frameReader.read().then(async function processFrame({ done, value }) {
if (done) {
console.log("Stream is done");
return;
}
if (stopped) { // brute force stop handler
frameReader.releaseLock();
processor.readable.cancel();
value.close();
console.log("Stopped");
video.remove();
console.log(imageFrames)
output.removeChild(statusDiv)
imageFrames.map((frame) => {
const img = new Image();
img.src = frame.url;
img.width = video.width;
img.height = video.height;
img.style.padding = "5px 5px 5px 5px"
img.style.objectFit = 'contain'
img.style.borderRadius = '10px'
output.append(img)
})
const end = new Date().getTime()
var timeDiff = end - start; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
statusDiv.innerHTML = `Completed ${i} of ${totalExpectedFrames} frames. Elapsed time ${seconds} secs`
output.append(statusDiv)
started=false
return;
}
var img = await createImageBitmap(value);
ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
i++
statusDiv.innerHTML = `Capturing ${i} of ${totalExpectedFrames} frames`
output.append(statusDiv)
canvas.toBlob(async (blob) => {
const url = URL.createObjectURL(blob)
imageFrames.push({
id: i,
img,
url,
t: currentTime
})
currentTime += intervalPerFrame;
console.log(`frame#${i}`, url)
img.close();
value.close();
if (i == totalExpectedFrames) {
// streaming of video completes
console.log("stream done");
stopped = true;
done = true;
frameReader.releaseLock();
processor.readable.cancel();
value.close();
console.log("Stopped");
video.remove();
console.log(imageFrames)
output.removeChild(statusDiv)
imageFrames.map((frame) => {
const img = new Image();
img.src = frame.url;
img.width = video.width;
img.height = video.height;
img.style.objectFit = 'contain'
img.style.borderRadius = '10px'
img.style.padding = "5px 5px 5px 5px"
output.append(img)
})
const end = new Date().getTime()
var timeDiff = end - start; //in ms
// strip the ms
timeDiff /= 1000;
// get seconds
var seconds = Math.round(timeDiff);
statusDiv.innerHTML = `Completed ${i} of ${totalExpectedFrames} frames. Elapsed time ${seconds} secs`
output.append(statusDiv)
started=false
return
}
video.currentTime = currentTime;
await new Promise(r => seekComplete = r);
frameReader.read().then(processFrame);
})
})
}
</script>
</head>
<body>
<h1>Capture video frames with web codecs</h1>
<video id="video" width="320" height="160" controls>
<source src="mov_bbb.mp4" type="video/mp4">
Your browser does not support HTML5 video.
</video>
<div>
<button onclick="startVideoCapture()" type="button" id="btn">Start capture frames</button>
<button onclick="stopVideoCapture()" type="button" id="btn-stop">Stop capture frames</button>
<p>Follow console logs for status, once capture is done, the frames will be visible below..</p>
<small>You don't have to necessarily play the video to start capturing 😉, just click on Start button to begin capturing the video frames 🚀</small>
</div>
<h3>Output</h3>
<div id="output">
</div>
</body>
</html>