-
Notifications
You must be signed in to change notification settings - Fork 1
/
yt-recut.html
331 lines (297 loc) · 7.83 KB
/
yt-recut.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
---
layout: default
---
<style>
body {
margin-top: 24px;
}
#video-url-form {
margin-bottom: 24px;
display: flex;
}
#video-url-form input {
flex: 1;
margin-right: 8px;
}
iframe {
margin-bottom: 24px;
}
.skip-input {
border: none;
background: none;
border-bottom: dotted 3px #4b4ba9;
width: calc(5 * 1rem);
font-weight: bold;
font-family: monospace;
}
#add-skip-button {
margin-top: 1rem;
}
.inline-skip {
background: none;
border: none;
color: #4b4ba9;
cursor: pointer;
font-weight: bold;
}
#title-container {
display: flex;
justify-content: space-between;
}
#reset-button {
margin-top: 24px;
height: 40px;
color: red;
border: 2px solid red;
padding: 8px;
background: none;
font-weight: bold;
cursor: pointer;
&:hover {
background: red;
color: white;
}
}
</style>
<div id="title-container">
<h1>YouTube Recut</h1>
<button
id="reset-button"
onclick="
URLManager.updateURL({ v: null, skips: null });
location.reload();
"
>
Reset
</button>
</div>
<p>Add a YouTube video and set timestamp ranges that you want to skip :)</p>
<form id="video-url-form">
<input
type="text"
id="video-url"
placeholder="https://www.youtube.com/watch?v=some-id"
required="true"
/>
<button>Set Video</button>
</form>
<div id="player"></div>
<form id="skipForm" onsubmit="return false;">
<div>
<label for="skipFrom">Skip from</label>
<input
type="text"
class="skip-input"
id="skipFrom"
placeholder="(hh:mm:ss)"
required
/>
<button id="skip-from-current" onclick="SkipManager.setFromCurrent()">
Current timestamp
</button>
</div>
<div>
<label for="skipTo">Skip to</label>
<input
type="text"
class="skip-input"
id="skipTo"
placeholder="(hh:mm:ss)"
required
/>
<button id="skip-to-current" onclick="SkipManager.setToCurrent()">
Current timestamp
</button>
</div>
<button onclick="SkipManager.addSkip()" id="add-skip-button">Add Skip</button>
</form>
<h2>Skips</h2>
<ul id="skipList"></ul>
<script>
// Utility functions
const TimeUtil = {
secondsToTimestamp(seconds) {
const date = new Date(null);
date.setSeconds(seconds);
return date.toISOString().substr(11, 8);
},
timestampToSeconds(timestamp) {
const parts = timestamp.split(":");
if (parts.length === 3) {
const [hours, minutes, seconds] = parts.map(Number);
return hours * 3600 + minutes * 60 + seconds;
}
if (parts.length === 2) {
const [minutes, seconds] = parts.map(Number);
return minutes * 60 + seconds;
}
if (parts.length === 1) {
return Number(parts[0]);
}
return 0;
},
};
// URL management
const URLManager = {
params: new URLSearchParams(window.location.search),
getParam(key) {
return this.params.get(key);
},
updateURL(params) {
const newURL = new URL(window.location);
Object.entries(params).forEach(([key, value]) => {
if (value) {
newURL.searchParams.set(key, value);
} else {
newURL.searchParams.delete(key);
}
});
window.history.pushState({}, "", newURL);
},
};
// Video management
const VideoManager = {
player: null,
videoId: null,
init() {
this.videoId = URLManager.getParam("v");
const videoUrlForm = document.getElementById("video-url-form");
videoUrlForm.style.display = this.videoId ? "none" : "flex";
videoUrlForm.addEventListener("submit", this.setVideo.bind(this));
if (this.videoId) {
this.loadPlayer();
}
},
loadPlayer() {
const tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
const firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
},
setVideo(event) {
event.preventDefault();
const input = document.getElementById("video-url");
const url = new URL(input.value);
this.videoId = url.searchParams.get("v");
URLManager.updateURL({ v: this.videoId });
document.getElementById("video-url-form").style.display = "none";
this.loadPlayer();
},
onPlayerReady() {
SkipManager.init();
this.startTimeCheck();
},
startTimeCheck() {
setInterval(() => {
if (
this.player &&
this.player.getPlayerState() === YT.PlayerState.PLAYING
) {
const currentTime = this.player.getCurrentTime();
const skipTo = SkipManager.getSkipDestination(currentTime);
if (skipTo) {
this.player.seekTo(skipTo, true);
}
}
}, 500);
},
};
// Skip management
const SkipManager = {
skips: [],
init() {
const skipsParam = URLManager.getParam("skips");
if (skipsParam) {
this.skips = skipsParam.split(",").map((skip) => {
const [from, to] = skip.split("-").map(Number);
return { from, to };
});
}
this.renderSkipList();
},
addSkip() {
const fromInput = document.getElementById("skipFrom");
const toInput = document.getElementById("skipTo");
const from = TimeUtil.timestampToSeconds(fromInput.value);
const to = TimeUtil.timestampToSeconds(toInput.value);
if (!this.validateSkip(from, to)) return;
this.skips.push({ from, to });
this.updateSkips();
this.clearInputs(fromInput, toInput);
},
validateSkip(from, to) {
if (from >= to) {
alert('Skip "to" time must be greater than "from" time');
return false;
}
if (this.skips.some((skip) => from < skip.to && to > skip.from)) {
alert("New skip overlaps with existing skip");
return false;
}
return true;
},
removeSkip(index) {
this.skips.splice(index, 1);
this.updateSkips();
},
setFromCurrent() {
document.getElementById("skipFrom").value = TimeUtil.secondsToTimestamp(
Math.round(VideoManager.player.getCurrentTime())
);
},
setToCurrent() {
document.getElementById("skipTo").value = TimeUtil.secondsToTimestamp(
Math.round(VideoManager.player.getCurrentTime())
);
},
getSkipDestination(currentTime) {
return this.skips.find(
(skip) => currentTime >= skip.from && currentTime < skip.to
)?.to;
},
updateSkips() {
const skipsParam = this.skips
.map((skip) => `${skip.from}-${skip.to}`)
.join(",");
URLManager.updateURL({ skips: skipsParam });
this.renderSkipList();
},
clearInputs(...inputs) {
inputs.forEach((input) => (input.value = ""));
},
renderSkipList() {
const list = document.getElementById("skipList");
list.innerHTML = this.skips
.map(
(skip, index) => `
<li>
<button class="inline-skip" onclick="VideoManager.player.seekTo(${
skip.from
}, true)">
${TimeUtil.secondsToTimestamp(skip.from)}
</button> to
<button class="inline-skip" onclick="VideoManager.player.seekTo(${
skip.to
}, true)">
${TimeUtil.secondsToTimestamp(skip.to)}
</button>
<button onclick="SkipManager.removeSkip(${index})">Remove</button>
</li>
`
)
.join("");
},
};
// YouTube API callback
function onYouTubeIframeAPIReady() {
VideoManager.player = new YT.Player("player", {
height: "360",
width: "640",
videoId: VideoManager.videoId,
events: {
onReady: () => VideoManager.onPlayerReady(),
},
});
}
window.addEventListener("DOMContentLoaded", () => VideoManager.init());
</script>