-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
168 lines (151 loc) · 4.46 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
<!doctype html>
<html>
<head>
<style>
div {
box-sizing: border-box;
width: 1280px;
height: 60px;
font-size: 53.5px;
padding: 0 8px;
font-family: Arial;
font-weight: bold;
background: white;
}
</style>
</head>
<body style="background: red;">
<div>
<span id="frame">00000</span>
<span id="timecode">00:00:00.000</span>
<span id="curtime">00:00:00.000</span>
<span id="accuracy">00000</span>
</div>
<video src="time.mp4" width="1280" height="720" controls></video>
<script>
const d = document;
const b = d.body;
const w = window;
const v = d.querySelector("video");
const f = d.querySelector("#frame");
const c = d.querySelector("#timecode");
const t = d.querySelector("#curtime");
const a = d.querySelector("#accuracy");
const colors = [
"#FF0000",
"#00FF00",
"#0000FF",
"#FFFF00",
"#FF00FF",
"#00FFFF"
]
v.playbackRate = 0.5;
let drift = null;
let tu = 0;
const MINUTE = 60;
const HOUR = 60 * 60;
const FPS = 24000 / 1001;
const FRAME = 1 / FPS;
function zpad(str, num = 2, pad = "0") {
str = "" + str;
while (str.length < num) {
str = pad + str;
}
return str;
}
function tc(seconds) {
const ss = ~~seconds % MINUTE;
const ms = ~~((seconds - ~~seconds) * 1000);
const mm = ~~((seconds % HOUR) / MINUTE);
const hh = ~~(seconds / HOUR);
return `${zpad(hh)}:${zpad(mm)}:${zpad(ss)}:${zpad(ms, 3)}`;
}
v.addEventListener("canplay", e => {
if (drift == null) {
drift = v.currentTime;
console.log(`video time drift: ${drift}`);
}
});
d.addEventListener("keydown", e => {
// console.log(e.keyCode);
if (e.keyCode === 32) {
e.preventDefault();
if (v.paused) {
v.play();
} else {
v.pause();
}
} else if (e.keyCode === 49) {
v.playbackRate = 1.0;
} else if (e.keyCode === 50) {
v.playbackRate = 0.5;
} else if (e.keyCode === 51) {
v.playbackRate = 0.25;
}
});
let paintCount = "";
let droppedCount = "";
if (v.mozPaintedFrames !== undefined) {
paintCount = "mozPaintedFrames";
}
if (v.webkitDecodedFrameCount !== undefined) {
paintCount = "webkitDecodedFrameCount";
droppedCount = "webkitDroppedFrameCount";
}
let currentFrame = 0;
let nextFrame = 1;
let nextFrameTime = nextFrame * FRAME;
let lastPaintCount = 0;
const FRAME_WINDOW_LOWER = FRAME * 0.95;
const FRAME_WINDOW_UPPER = FRAME * 0.25;
function within(value, target, window) {
const half = window / 2;
return value >= target - half && value <= target + half;
}
v.addEventListener("seeked", e => {
const time = v.currentTime - drift;
const frame = Math.floor(time / FRAME);
currentFrame = frame;
nextFrame = frame + 1;
nextFrameTime = nextFrame * FRAME;
if (paintCount) {
lastPaintCount = v[paintCount];
}
console.log("seeked");
});
function loop() {
const time = v.currentTime - drift;
t.textContent = tc(time);
const frame = Math.round(time / FRAME);
if (paintCount) {
const currentPaintCount = v[paintCount];
a.textContent = zpad(currentPaintCount, 5);
const diffPaintCount = currentPaintCount - lastPaintCount;
const check = time >= nextFrameTime - FRAME_WINDOW_LOWER && time <= nextFrameTime + FRAME_WINDOW_UPPER;
if (check && diffPaintCount > 0) {
currentFrame = nextFrame++;
nextFrameTime = nextFrame * FRAME;
console.log(`should be accurate frame change`)
} else if (time >= nextFrameTime && currentFrame < nextFrame) {
currentFrame = frame;
nextFrame = currentFrame + 1;
nextFrameTime = nextFrame * FRAME;
console.log(`[${tc(time)}] possibly a dropped frame`);
if (droppedCount) {
console.log("dropped frames:", v[droppedCount]);
}
}
lastPaintCount = currentPaintCount;
} else {
currentFrame = frame;
}
const timecode = currentFrame * FRAME;
f.textContent = zpad(currentFrame, 5);
c.textContent = tc(timecode);
b.style.backgroundColor = colors[Math.floor(currentFrame / 48) % 6];
requestAnimationFrame(loop);
}
requestAnimationFrame(loop);
</script>
</body>
</html>