-
Notifications
You must be signed in to change notification settings - Fork 3
/
video-grep.html
195 lines (178 loc) · 7.66 KB
/
video-grep.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
<html>
<head>
<style>
input {margin-left:20px;}
#uw-container {margin-bottom: 20px;}
#grep-container {
margin: 20 auto;
width:400px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
display: none
}
#spinner {
text-align: center;
width: 50%;
margin: 20 auto 20 auto;
display: none
}
#video-container {
position: relative;
width: 100%;
}
video {
margin: 20 auto 20 auto;
display: block;
}
input[type=text] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input#videoUrl {
display: none;
}
.title {
margin-bottom: 50px;
}
</style>
<script src="//widget.cloudinary.com/global/all.js" type="text/javascript"></script>
</head>
<body>
<div width="100%" height="100%" id="uw-container"></div>
<div width="100%" height="100%" id="spinner">
<div class="title">Please wait while we're analysing your video...</div>
<img src="http://demo-res.cloudinary.com/image/upload/cloud-spinner-blue.svg">
</div>
<div width="100%" height="100%" id="grep-container">
<label for="query">Search:</label><input name="query" id="query" type="text">
<input name="url" id="videoUrl" type="hidden">
<button value="" onclick="return doGrep();">Send Request</button>
</div>
<div width="100%" height="100%" id="video-container">
<video width="640" height="480" id="videoPlayer" controls />
</div>
<script type="text/javascript">
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split('&');
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split('=');
if (decodeURIComponent(pair[0]) == variable) {
return decodeURIComponent(pair[1]);
}
}
}
function doGrep(){
var query = document.getElementById("query").value
var url = document.getElementById("videoUrl").value
console.log(query,url)
var xhr = new XMLHttpRequest();
xhr.open("GET", `https://dev-miki.cloudinary.auth0-extend.com/video-grep/grep?url=${url}&query=${query}`, true);
xhr.send();
xhr.onload = function (e) {
console.log(e.currentTarget.response)
if (e.currentTarget.response === "") {
alert("Sorry, found no results");
return;
}
var grep = document.getElementById("grep-container")
var videoPlayer = document.getElementById("videoPlayer")
videoPlayer.setAttribute('src', e.currentTarget.response)
videoPlayer.setAttribute('autoplay', 'autoplay');
// grep.style.display = "none"
}
}
function videoTranscripted(result){
var spinner = document.getElementById("spinner")
var grep = document.getElementById("grep-container")
var videoUrl = document.getElementById("videoUrl")
spinner.style.display = "none"
grep.style.display = "block"
videoUrl.value = result.url
var videoPlayer = document.getElementById("videoPlayer")
videoPlayer.src = result.url;
}
function videoUploaded(result){
var uw = document.getElementById("uw-container")
var spinner = document.getElementById("spinner")
uw.style.display = "none"
spinner.style.display = "block"
}
function callback(e, data){
// The polling function
videoUploaded();
function poll(fn, timeout, interval) {
var endTime = Number(new Date()) + (timeout || 2000);
interval = interval || 100;
var checkCondition = function(resolve, reject) {
fn().then(function(){
// console.log("resolving")
resolve();
return;
}).catch(function(reason){
if (reason !== "error") {
if (Number(new Date()) < endTime) {
// console.log("polling")
setTimeout(checkCondition, interval, resolve, reject);
} else {
reject(new Error('Timed out for ' + fn + ': ' + arguments));
}
} else {
console.log("here")
reject("An error occurred");
}
})
};
return new Promise(checkCondition);
}
// Usage: ensure element is visible
poll(function() {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open("GET", `https://dev-miki.cloudinary.auth0-extend.com/video-grep/poll?public_id=${data[0].public_id}`, true);
// xhr.open("GET", `https://dev-miki.cloudinary.auth0-extend.com/video-grep/poll_stub?public_id=${data[0].public_id}`, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve("VTT ready");
} else {
// console.error(xhr.statusText);
reject("VTT not yet ready")
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
reject("An error occurred")
};
xhr.send(null);
})
}, 600000, 1000).then(function() {
console.log("XXXXXX");
videoTranscripted(data[0]);
}).catch(function() {
// Polling timed out, handle the error!
});
}
(function() {
var videoUrl = getQueryVariable('video_url');
console.log(videoUrl);
if (videoUrl) {
videoTranscripted({ url: videoUrl });
} else {
cloudinary.openUploadWidget({
cloud_name: 'demo',
upload_preset: 'videogrep',
inline_container: '#uw-container'
}, callback);
}
})()
</script>
</body>
</html>