-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
280 lines (243 loc) · 7.9 KB
/
main.js
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
"use strict";
const URL = window.URL || window.webkitURL;
// Global variables
let files = [];
let updateInfoIntervalId;
let checkVideoInRangeIntervalId;
let webcamLinked = true;
let videoSwapped = false;
// Constants
const LOAD_VIDEO_DELAY_MS = 300;
// DOM Elements
let dragHeader = document.getElementById("drag_header");
let playerDiv = document.getElementById("player_div");
let mainVideo = document.getElementById("main_video");
let webcamVideo = document.getElementById("webcam_video");
let namesParagraph = document.getElementById("names_paragraph");
let timeInfoDiv = document.getElementById("time_info_div");
let showInfoButton = document.getElementById("show_time_info_button");
let hideWebcamButton = document.getElementById("hide_webcam_button");
let hideMainButton = document.getElementById("hide_main_button");
let unlinkWebcamButton = document.getElementById("unlink_webcam_button");
let matchVideoEndButton = document.getElementById(
"set_offset_to_match_end_button"
);
let offsetInput = document.getElementById("offset_input");
let playbackSpeedInput = document.getElementById("playback_speed_input");
let swapButton = document.getElementById("swap_button");
// Event listeners
document.body.addEventListener("drop", dropHandler);
document.body.addEventListener("dragover", dragOverHandler);
mainVideo.addEventListener("pause", pauseVideo);
mainVideo.addEventListener("ended", pauseVideo);
mainVideo.addEventListener("play", playVideo);
mainVideo.addEventListener("seeked", syncVideos);
offsetInput.addEventListener("change", syncVideos);
playbackSpeedInput.addEventListener("change", changePlaybackSpeed);
swapButton.addEventListener("click", swapVideos);
showInfoButton.addEventListener("click", showInfo);
hideWebcamButton.addEventListener("click", () =>
hideVideo(hideWebcamButton, webcamVideo, "webcam")
);
hideMainButton.addEventListener("click", () =>
hideVideo(hideMainButton, mainVideo, "main")
);
unlinkWebcamButton.addEventListener("click", unlinkWebcam);
matchVideoEndButton.addEventListener("click", setOffsetToMatchVideoEnd);
// Functions
function round(value, precision) {
let multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}
function dragOverHandler(ev) {
ev.preventDefault();
}
function dropHandler(ev) {
ev.preventDefault();
if (window.location.href.indexOf("example.html") !== -1) {
alert(
"You cannot drop videos in the example page. You will be redirected to the main one, drop your videos there."
);
window.location.href = window.location.href.replace(
"example.html",
"index.html"
);
return;
}
console.log("File(s) dropped");
files = Array.from(ev.dataTransfer.files);
if (files.length !== 2) {
alert("You need to drop exactly two videos");
return;
}
files.sort(function (a, b) {
return a.name.localeCompare(b.name);
});
webcamVideo.src = URL.createObjectURL(files[0]);
mainVideo.src = URL.createObjectURL(files[1]);
dragHeader.style.display = "none";
document.body.style.textAlign = "left";
playerDiv.style.display = "unset";
namesParagraph.innerHTML = "Video names:";
for (let file of files) {
namesParagraph.innerHTML += "<br>" + file.name;
}
let storedItem = localStorage.getItem(files[0].name);
let playbackInfo;
if (storedItem) {
playbackInfo = JSON.parse(storedItem);
}
setTimeout(function () {
if (playbackInfo) {
offsetInput.value = playbackInfo.offset;
mainVideo.currentTime = playbackInfo.time;
}
mainVideo.play();
}, LOAD_VIDEO_DELAY_MS);
setInterval(updateStoredTime, 5000);
// Optional feature: set offset automatically if video duration differs
// let durationDiff = webcamVideo.duration - mainVideo.duration;
// offsetInput.value = durationDiff;
// console.log("Video duration diff: " + durationDiff);
// syncVideos();
}
function updateStoredTime() {
let info = {};
if (!videoSwapped) {
info.time = parseInt(mainVideo.currentTime);
info.offset = offsetInput.value;
} else {
info.time = parseInt(webcamVideo.currentTime);
info.offset = -offsetInput.value;
}
localStorage.setItem(files[0].name, JSON.stringify(info));
// console.log("Saved video current time in browser local storage");
}
function pauseVideo() {
if (webcamLinked) {
webcamVideo.pause();
// syncVideos();
}
}
function playVideo() {
if (webcamLinked) {
webcamVideo.play();
// syncVideos();
}
}
function syncVideos() {
if (webcamLinked) {
let playing = isVideoPlaying(mainVideo);
mainVideo.pause();
webcamVideo.pause();
let timeMainVideo = mainVideo.currentTime;
let timeWebcamVideo = timeMainVideo + parseInt(offsetInput.value);
if (timeWebcamVideo >= 0 && timeWebcamVideo <= webcamVideo.duration) {
webcamVideo.currentTime = timeWebcamVideo;
} else {
webcamLinked = false;
unlinkWebcamButton.disabled = true;
webcamVideo.currentTime = 0;
checkVideoInRangeIntervalId = setInterval(checkVideoInRange, 100);
}
if (playing) {
mainVideo.play();
webcamVideo.play();
}
console.groupCollapsed("Videos synced");
console.log("Main video time: " + timeMainVideo);
console.log("Computed webcam video time: " + timeMainVideo);
console.log("Offset: " + offsetInput.value);
console.log("Webcam video duration: " + webcamVideo.duration);
console.groupEnd();
}
}
function checkVideoInRange() {
let timeMainVideo = mainVideo.currentTime;
let timeWebcamVideo = timeMainVideo + parseInt(offsetInput.value);
if (timeWebcamVideo >= 0 && timeWebcamVideo <= webcamVideo.duration) {
webcamLinked = true;
unlinkWebcamButton.disabled = false;
webcamVideo.currentTime = timeWebcamVideo;
webcamVideo.play();
clearInterval(checkVideoInRangeIntervalId);
}
}
function changePlaybackSpeed() {
webcamVideo.playbackRate = playbackSpeedInput.value;
mainVideo.playbackRate = playbackSpeedInput.value;
}
function swapVideos() {
let playing = isVideoPlaying(mainVideo);
let timeWebcamVideo = webcamVideo.currentTime;
let temp = webcamVideo.src;
webcamVideo.src = mainVideo.src;
mainVideo.src = temp;
offsetInput.value = -offsetInput.value;
videoSwapped = !videoSwapped;
setTimeout(function () {
mainVideo.currentTime = timeWebcamVideo;
if (playing) mainVideo.play();
}, LOAD_VIDEO_DELAY_MS);
}
function showInfo() {
if (timeInfoDiv.style.display === "none") {
timeInfoDiv.style.display = "unset";
showInfoButton.innerHTML = "Hide time info";
updateInfo();
updateInfoIntervalId = setInterval(updateInfo, 1000);
} else {
showInfoButton.innerHTML = "Show time info";
clearInterval(updateInfoIntervalId);
timeInfoDiv.style.display = "none";
}
}
function updateInfo() {
timeInfoDiv.innerHTML =
"Webcam video: " +
parseInt(webcamVideo.currentTime) +
"/" +
parseInt(webcamVideo.duration) +
"<br>" +
"Main video : " +
parseInt(mainVideo.currentTime) +
"/" +
parseInt(mainVideo.duration);
}
function hideVideo(button, video, videoSource) {
if (video.style.display === "none") {
video.style.display = "unset";
button.innerHTML = "Hide " + videoSource + " video";
} else {
video.style.display = "none";
button.innerHTML = "Show " + videoSource + " video";
}
}
function unlinkWebcam() {
if (webcamLinked) {
webcamLinked = false;
webcamVideo.pause();
unlinkWebcamButton.innerHTML = "Link webcam video";
} else {
webcamLinked = true;
offsetInput.value = round(
webcamVideo.currentTime - mainVideo.currentTime,
1
);
syncVideos();
if (!mainVideo.paused) webcamVideo.play();
unlinkWebcamButton.innerHTML = "Freeze and unlink webcam video";
}
}
function setOffsetToMatchVideoEnd() {
offsetInput.value = round(webcamVideo.duration - mainVideo.duration, 1);
syncVideos();
}
function isVideoPlaying(video) {
return (
video.currentTime > 0 &&
!video.paused &&
!video.ended &&
video.readyState > 2
);
}