-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualPlayer.html
367 lines (326 loc) · 13.6 KB
/
dualPlayer.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!DOCTYPE html>
<html>
<head>
<title>Dual Player</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=5.0">
<style>
#container {
padding: 15px;
}
#containerP {
position: relative;
width: 1280px;
/* Adjust as needed */
height: 720px;
/* Adjust as needed */
overflow: hidden;
}
#leftPlayer,
#rightPlayer {
position: absolute;
width: 100%;
height: 100%;
}
#leftPlayer {
z-index: 2;
clip-path: inset(0 50% 0 0);
}
#rightPlayer {
z-index: 1;
}
#VideoSeekSlider {
position: absolute;
width: 90%;
bottom: 0;
left: 50%;
transform: translate(-50%, -50%);
/* Adjust as needed */
z-index: 3;
}
/* Style for the separator line */
#separator {
position: absolute;
height: 100%;
width: 2px;
display: block;
left: 50%;
/* Adjust as needed */
background-color: red;
z-index: 3;
}
/* Style for the video labels */
.videoName,
.currenttime {
position: absolute;
color: white;
background-color: black;
padding: 5px;
z-index: 3;
}
#leftVideoName {
top: 0;
left: 0;
}
#rightVideoName {
top: 0;
right: 0;
}
#leftCurrentTime {
bottom: 30px;
left: 0;
}
#rightCurrentTime {
bottom: 30px;
right: 0;
}
#difftime {
position: absolute;
bottom: 20px;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-exp.min.css">
<link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre-icons.min.css">
</head>
<body>
<div id="container">
<h3>Please use chrome</h3>
<div id="containerP">
<div id="player">
<video id="leftPlayer" src="shortvovf1.mp4" type="video/mp4" muted preload:auto autoplay></video>
<video id="rightPlayer" src="shortvovf2.mp4" type="video/mp4" muted preload:auto autoplay></video>
<!-- Separator line -->
<div id="separator"></div>
<!-- Video labels -->
<div id="leftVideoName" class="videoName"></div>
<div id="rightVideoName" class="videoName"></div>
<!-- Current an diff Time labels -->
<div id="timedisplay">
<div id="leftCurrentTime" class="currenttime"></div>
<div id="rightCurrentTime" class="currenttime"></div>
<div id="difftime" class="currenttime"></div>
</div>
</div>
<div>
<!-- Slider for seeking -->
<input type="range" min="0" max="100" value="0" name="VideoSeekSlider" id="VideoSeekSlider"
style="accent-color:#5755d9;">
</div>
</div>
<br>
<div id="buttons">
<button class="btn btn-primary btn-sm" onclick="playVideo()">Play</button>
<button class="btn btn-primary btn-sm" onclick="stopVideo()">Stop</button>
<button class="btn btn-primary btn-sm" onclick="pauseVideo()">Pause</button>
<button class="btn btn-primary btn-sm" onclick="rewind()"> <<-10s </button>
<button class="btn btn-primary btn-sm" onclick="fastForward()"> +10s >> </button>
<label class="form-switch form-inline">
<input type="checkbox" onclick="toggleTimeDisplay()" checked>
<i class="form-icon"></i> Time display
</label>
<button class="btn btn-primary btn-sm" onclick="videoSync()">Players sync</button>
<button class="btn btn-primary btn-sm" onclick="toggleFullScreen()">Fullscreen</button>
</div>
<!-- player size-->
<div class="form-group">
<label class="form-label form-inline">Player Size:</label>
<label for="size50" class="form-radio form-inline">
<input type="radio" id="size50" name="size" value="50">
<i class="form-icon"></i> 50%
</label>
<label for="size75" class="form-radio form-inline">
<input type="radio" id="size75" name="size" value="75" checked>
<i class="form-icon"></i> 75%
</label>
<label for="size95" class="form-radio form-inline">
<input type="radio" id="size95" name="size" value="95">
<i class="form-icon"></i> 95%
</label>
<!-- Slider for players size -->
<input type="range" min="25" max="100" value="50" id="size" style="accent-color:#5755d9">
</div>
<!-- File input for each video -->
<input class="form-input" type="file" id="leftVideoFile" title="leftVideo" style="width:50%">
<input class="form-input" type="file" id="rightVideoFile" title="rightVideo" style="width:50%">
<!-- Text inputs for each video URL -->
<br>
<button class="btn btn-primary " onclick="loadVideos()">Load Videos</button>
<!-- Text input for each video URL -->
<br><br>
<input class="form-input" type="text" id="leftVideoUrl" style="width:50%" placeholder="URL for left video ">
<input class="form-input" type="text" id="rightVideoUrl" style="width:50%" placeholder="URL for right video ">
</div>
<script>
let container = document.getElementById("container");
let dualPlayerContainer = document.getElementById("player");
let separator = document.getElementById("separator");
let leftVideo = document.getElementById("leftPlayer");
let rightVideo = document.getElementById("rightPlayer");
let VideoPlayerSizeSlider = document.getElementById("size");
let VideoSeekSlider = document.getElementById("VideoSeekSlider");
let timeDisplay = document.getElementById("timedisplay");
// player video
function playVideo() {
leftVideo.play();
rightVideo.play();
}
function stopVideo() {
leftVideo.currentTime = 0;
leftVideo.pause();
rightVideo.currentTime = 0;
rightVideo.pause();
}
function pauseVideo() {
leftVideo.pause();
rightVideo.pause();
}
function fastForward() {
leftVideo.currentTime += 10;
rightVideo.currentTime += 10;
}
function rewind() {
leftVideo.currentTime -= 10;
rightVideo.currentTime -= 10;
}
//video sync
function videoSync() {
if (leftVideo.currentTime !== rightVideo.currentTime) {
console.log("before sync: " + leftVideo.currentTime + "--" + rightVideo.currentTime);
rightVideo.currentTime = leftVideo.currentTime;
timeupdate();
console.log("after: " + leftVideo.currentTime + "--" + rightVideo.currentTime);
}
}
// display current time of each video and diff between both
// seek slider position update
function timeupdate() {
let curLeftTime = leftVideo.currentTime;
let curRightTime = rightVideo.currentTime;
let totalDuration = leftVideo.duration;
let timePosition = (curLeftTime / totalDuration) * 100;
let diff = curRightTime - curLeftTime;
diff = diff.toFixed(3);
//
leftCurrentTime.textContent = convertHHMMSS(curLeftTime);
rightCurrentTime.textContent = convertHHMMSS(curRightTime);
difftime.textContent = "diff: " + diff;
// seek slider position update
VideoSeekSlider.value = timePosition;
}
leftVideo.addEventListener("timeupdate", timeupdate);
// time display on/off
function toggleTimeDisplay() {
if (getComputedStyle(timeDisplay).display != "none") {
timeDisplay.style.display = "none";
} else {
timeDisplay.style.display = "block";
}
}
// players full screen
function toggleFullScreen() {
if (!document.fullscreenElement) {
document.getElementById("containerP").requestFullscreen();
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
}
}
}
//load videos
function loadVideos() {
let leftVideoFile = document.getElementById("leftVideoFile");
let rightVideoFile = document.getElementById("rightVideoFile");
let leftVideoUrl = document.getElementById("leftVideoUrl");
let rightVideoUrl = document.getElementById("rightVideoUrl");
if (leftVideoFile.files[0]) {
leftVideo.src = URL.createObjectURL(leftVideoFile.files[0]);
leftVideoName.textContent = leftVideoFile.files[0].name;
} else if (leftVideoUrl.value) {
leftVideo.src = leftVideoUrl.value;
leftVideoName.textContent = leftVideoUrl.value;
}
if (rightVideoFile.files[0]) {
rightVideo.src = URL.createObjectURL(rightVideoFile.files[0]);
rightVideoName.textContent = rightVideoFile.files[0].name;
} else if (rightVideoUrl.value) {
rightVideo.src = rightVideoUrl.value;
rightVideoName.textContent = rightVideoUrl.value;
}
}
// managing the separation zone with the mouse
dualPlayerContainer.addEventListener('pointerdown', function (event) {
if (event.button === 0) { // mouse left button
container.onmousemove = function (event) {
let rect = container.getBoundingClientRect();
let x = event.clientX - rect.left; // mouse position on X axis
let width = rect.right - rect.left; // div width
let percentage = (x / width) * 100; //
//
leftVideo.style.clipPath = "inset(0 " + (100 - percentage) + "% 0 0)";
//
separator.style.left = percentage + "%";
}
}
});
container.addEventListener('pointerup', function () {
container.onmousemove = null; // Stops tracking the mouse when the button is released
});
// space bar management
window.onkeydown = function (e) {
if (e.keyCode == 32) { // 32 is the keyCode for the spacebar
if (leftVideo.paused) {
playVideo();
} else {
pauseVideo();
}
e.preventDefault(); // prevent the default action (scroll / move caret)
} else if (e.keyCode == 37) { // 37 is the keyCode for the left arrow
leftVideo.currentTime = leftVideo.currentTime-0.040;
videoSync();
e.preventDefault(); // prevent the default action (scroll / move caret)
} else if (e.keyCode == 39) { // 39 is the keyCode for the right arrow
leftVideo.currentTime = leftVideo.currentTime+0.040;
videoSync();
e.preventDefault(); // prevent the default action (scroll / move caret)
}
}
// Event listener for container size radio buttons
document.querySelectorAll('input[name="size"]').forEach((elem) => {
elem.addEventListener("change", function (event) {
containerP.style.width = event.target.value + "%";
// keep container 16/9 aspect ratio
containerP.style.height = (event.target.value * 9 / 16) + "vw";
});
});
// VideoPlayerSizeSlider management
VideoPlayerSizeSlider.oninput = function () {
containerP.style.width = this.value + "%";
containerP.style.height = (this.value * 9 / 16) + "vw";
}
// seek slider
VideoSeekSlider.oninput = function () {
let seekTo = leftVideo.duration * (VideoSeekSlider.value / 100);
leftVideo.currentTime = seekTo;
rightVideo.currentTime = seekTo;
}
//
function convertHHMMSS(currentTime) {
//
let hours = Math.floor(currentTime / 3600);
let minutes = Math.floor((currentTime - (hours * 3600)) / 60);
let seconds = Math.floor(currentTime - (hours * 3600) - (minutes * 60));
let images = Math.floor((currentTime % 1) / 0.04);
//
if (hours < 10) { hours = "0" + hours; }
if (minutes < 10) { minutes = "0" + minutes; }
if (seconds < 10) { seconds = "0" + seconds; }
if (images < 10) { images = "0" + images; }
let texteFormat = hours + 'h:' + minutes + 'm:' + seconds + 's:' + images + ' images ';
return texteFormat;
}
// Call when the page loads
</script>
</body>
</html>