-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
101 lines (66 loc) · 2.12 KB
/
script.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
var video = document.getElementById("rick")
var startTime = 1570815649091
video.addEventListener('loadedmetadata', function() {
video.play()
jumpToLocation()
video.addEventListener('play', function() {
console.log("Playing.")
window.setTimeout(sync, 2000)
})
})
function jumpToLocation() {
var difference = Date.now() - startTime
var currentTime = (difference / 1000) % video.duration
video.currentTime = currentTime
}
function restorePlaybackRate() {
console.log("Restoring normal speed and waiting 10 seconds.")
video.playbackRate = 1.0
window.setTimeout(sync, 10000)
}
function sync() {
console.log("Syncing.")
var difference = Date.now() - startTime
var currentTime = (difference / 1000) % video.duration
console.log("Video should be " + currentTime)
console.log("Video is " + video.currentTime)
var offset = currentTime - video.currentTime
console.log("Offset is " + offset)
if (offset > 1) {
console.log("Offset too big. Jumping.")
jumpToLocation()
window.setTimeout(sync, 2000)
}
else if (offset > 0.1) {
console.log("Speeding up a lot.")
video.playbackRate = 1.1
var delay = Math.round(offset * 10 * 1000)
console.log("Waiting " + (delay / 1000))
window.setTimeout(restorePlaybackRate, delay)
}
else if (offset < -0.1) {
console.log("Slowing down a lot.")
video.playbackRate = 0.9
var delay = Math.round(offset * -10 * 1000)
console.log("Waiting " + (delay / 1000))
window.setTimeout(restorePlaybackRate, delay)
}
else if (offset > 0.01) {
console.log("Speeding up a little bit.")
video.playbackRate = 1.01
var delay = Math.round(offset * 100 * 1000)
console.log("Waiting " + (delay / 1000))
window.setTimeout(restorePlaybackRate, delay)
}
else if (offset < -0.01) {
console.log("Slowing down a little bit.")
video.playbackRate = 0.99
var delay = Math.round(offset * -100 * 1000)
console.log("Waiting " + (delay / 1000))
window.setTimeout(restorePlaybackRate, delay)
}
else {
console.log("Timing good. Waiting 10 seconds.")
window.setTimeout(sync, 10000)
}
}