forked from mikeys/video-grep
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
220 lines (177 loc) · 7.78 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<html>
<head>
<style>
input {margin-left:20px;}
#grep-container {
margin: 20 auto;
width:400px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
#video-container {
position: relative;
width: 100%;
}
video {
margin: 20 auto 20 auto;
display: block;
}
#tagCloud {
margin: 10 auto 0 auto;
height: 200px;
width: 100%;
}
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;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<link rel="stylesheet" href="https://mistic100.github.io/jQCloud/dist/jqcloud2/dist/jqcloud.min.css">
<script src="https://mistic100.github.io/jQCloud/dist/jqcloud2/dist/jqcloud.js"></script>
</head>
<body>
<div width="100%" height="100%" id="grep-container">
<label for="videoUrl">Cloudinary Url:</label>
<input name="url" id="videoUrl" type="text" onchange="checkStatus();">
<div id="grep">
<label for="query">Search:</label><input name="query" id="query" type="text">
<button value="" onclick="return grep();">Grep</button>
</div>
<div id="tagCloud">
</div>
</div>
<div width="100%" height="100%" id="video-container">
<video width="640" height="480" id="videoPlayer" controls />
</div>
<script type="text/javascript">
var transcript = null;
function checkStatus() {
let videoUrl = document.getElementById("videoUrl").value;
disableGrep();
if (!videoUrl) return;
updateHash(videoUrl);
updateVideoPlayer(videoUrl);
return loadTranscript(videoUrl)
.then(t => disableGrep(!t))
.then(() => {
buildTagCloud();
grep();
})
}
function disableGrep(disable = true) {
document.getElementById("grep").style.display = disable ? "none" : "block"
}
function loadTranscript(videoUrl) {
transcript = null;
let transcriptUrl = videoUrl.replace(/\.[^.]+$/, ".transcript").replace('/video/', '/raw/');
return fetch(transcriptUrl)
.then(response => response.json())
.catch(e => {
// TODO: show spinner
return null;
})
.then(t => transcript = t)
.then(() => transcript)
}
function updateVideoPlayer(videoUrl) {
let videoPlayer = document.getElementById("videoPlayer");
videoPlayer.style.display = "none";
if (!!videoUrl) {
videoPlayer.style.display = "block";
videoPlayer.setAttribute('src', videoUrl);
videoPlayer.setAttribute('autoplay', 'autoplay');
}
return videoUrl;
}
function buildTagCloud() {
if (!transcript) return;
tagWords = {};
transcript.forEach((item, _) => {
item.words.forEach((wordSegment, _) => {
let word = wordSegment.word
let refWord = word.toLowerCase()
if (word.length < 5) { return; } // not interesting
if (tagWords[refWord]) {
tagWords[refWord].weight += 1;
} else {
link = location.href.split('!').slice(0, 1).concat(word).join('!')
tagWords[refWord] = {text: word, weight: 1, link: link, handlers: { click: () => {
document.getElementById("query").value = word;
grep();
}}};
}
})
});
$('#tagCloud').jQCloud(Object.values(tagWords), { autoResize: true, shape: 'rectangular', removeOverflowing: false, fontSize: {from: 0.075, to: 0.025} });
}
function grep() {
if (!transcript) return;
let videoUrl = document.getElementById("videoUrl").value;
let query = document.getElementById("query").value.toLowerCase();
if (query == '') {
updateVideoPlayer(videoUrl);
return;
}
let { cloudName, publicId } = parseUrl(videoUrl);
updateHash(videoUrl, query);
let cmd = '';
let items = transcript.filter((item) => item.transcript.toLowerCase().includes(query)) || [];
items.forEach((item, index) => {
let startTime = item.words[0].start_time;
let endTime = item.words[item.words.length - 1].end_time;
if (endTime - startTime > 5) {
let wordIndex = item.words.findIndex((item) => item.word.toLowerCase().includes(query));
if (wordIndex !== -1) {
let startIndex = Math.max(0, wordIndex - 3);
let wordsLen = item.words.length;
let endIndex = Math.min(wordIndex + 3, wordsLen - 1);
startTime = item.words[startIndex].start_time;
endTime = item.words[endIndex].end_time;
}
}
startTime = Math.max(startTime - 0.3, 0).toFixed(2);
endTime = (endTime + 0.5).toFixed(2);
if (index === 0) {
cmd += `so_${startTime},eo_${endTime}/`;
} else {
cmd += `l_video:${publicId},so_${startTime},eo_${endTime},fl_splice/fl_layer_apply/`
}
});
if (!!cmd) {
videoUrl = videoUrl.replace(/((?<=upload)\/+)/, `/${cmd}/`);
}
updateVideoPlayer(videoUrl);
}
function updateHash(url, query) {
location.hash = [url, query].filter(v => !!v).join('!');
}
function parseHash() {
let [url, query] = location.hash.replace(/^#/, '').split('!');
if (/^http|res.cloudinary.com/.test(query))
[query, url] = [url, query];
return {url, query};
}
function onLoad() {
let {url, query} = parseHash();
if (!!query) document.getElementById("query").value = query;
if (!!url) document.getElementById("videoUrl").value = url;
checkStatus();
}
function parseUrl(url) {
let re = /cloudinary[.]com[/]([^/]+)[/].*[/]([^/]+)[.][^.]+$/;
let parsed = url.match(re);
let cloudName = parsed[1];
let publicId = parsed[2];
return { cloudName, publicId };
}
window.addEventListener("load", onLoad);
</script>
</body>
</html>