This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvp.js
325 lines (293 loc) · 7.97 KB
/
pvp.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
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
'use strict'
let control
console.log('Precise Video Playback is up. Watching for video players...')
function collectCutTiming(cutBar) {
return [...cutBar.querySelectorAll('div > button:nth-child(1)')].map((x) =>
Number(x.innerText)
)
}
function createCutButton(time, videoElement) {
const btnJump = document.createElement('button')
const btnRemove = document.createElement('button')
const btnContainer = document.createElement('div')
btnJump.innerText = time
btnRemove.innerText = 'x'
btnJump.addEventListener('click', () => {
videoElement.currentTime = time
})
btnRemove.addEventListener('click', () => {
btnContainer.style.display = 'none'
})
applyStyle(btnContainer, {
marginRight: '0.5vw',
flexShrink: '0',
marginTop: '3px',
})
btnContainer.append(btnJump, btnRemove)
return btnContainer
}
function getVideoId(url) {
return String(url).match(/v=([^&#]+)/)[1]
}
function applyStyle(elem, styles) {
for (const [key, value] of Object.entries(styles)) {
elem.style[key] = value
}
}
function parseTime(str) {
const hms = str.split(':')
let time = 0
for (const i of hms) {
time *= 60
time += Number(i)
if (isNaN(time)) return -1
}
return time
}
function generateControl() {
const app = document.createElement('div')
const cutBar = document.createElement('div')
const inputFrom = document.createElement('input')
inputFrom.placeholder = 'from 0'
const inputTo = document.createElement('input')
inputTo.placeholder = 'to ...'
const currentTime = document.createElement('span')
const btn = document.createElement('button')
const btnStop = document.createElement('button')
const btnExport = document.createElement('button')
const btnCut = document.createElement('button')
applyStyle(app, {
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
maxWidth: '700px',
marginTop: '15px',
marginLeft: 'auto',
marginRight: 'auto',
width: '80vw',
})
applyStyle(cutBar, {
display: 'flex',
flexWrap: 'wrap',
marginTop: '1vh',
})
applyStyle(currentTime, {
fontSize: '1.3rem',
minWidth: '8.1rem',
textAlign: 'center',
color: 'var(--yt-spec-text-primary)',
})
const inputCommonStyle = {
width: '120px',
}
applyStyle(inputFrom, inputCommonStyle)
applyStyle(inputTo, inputCommonStyle)
btn.innerText = 'Jump'
btnStop.innerText = 'Stop'
btnExport.innerText = 'Export'
btnCut.innerText = 'Cut'
app.appendChild(inputFrom)
app.appendChild(inputTo)
app.appendChild(currentTime)
app.appendChild(btn)
app.appendChild(btnStop)
app.appendChild(btnExport)
app.appendChild(btnCut)
return {
app,
cutBar,
inputFrom,
inputTo,
currentTime,
btn,
btnStop,
btnExport,
btnCut,
}
}
function generateFullControl(videoElement, videoId) {
const control = generateControl()
// States
let fromValue = 0
let toValue = 0
let stopAtEndTime = false
// Initial state update attempt
const urlTime = window.location.hash.match(
/#pvp([0-9]+\.?[0-9]?)-([0-9]+\.?[0-9]?)/
)
if (urlTime !== null) {
console.log('Attempting to recover time from URL...')
control.inputFrom.value = fromValue = Number(urlTime[1]) || 0
control.inputTo.value = toValue = Number(urlTime[2]) || 0
}
// Current playback time
function updateCurrentTime() {
const currTime = videoElement.currentTime
control.currentTime.innerText = Number(currTime).toFixed(2)
if (stopAtEndTime && currTime >= toValue) {
videoElement.currentTime = fromValue
}
requestAnimationFrame(updateCurrentTime)
}
requestAnimationFrame(updateCurrentTime)
// Repeat playback
function onTimeUpdate() {
if (videoElement.currentTime >= Number(toValue)) {
videoElement.currentTime = Number(fromValue)
}
}
control.btn.addEventListener('click', (evt) => {
evt.preventDefault()
videoElement.pause()
videoElement.currentTime = fromValue
if (fromValue < toValue) {
videoElement.play()
stopAtEndTime = true
} else {
stopAtEndTime = false
}
})
control.btnStop.addEventListener('click', (evt) => {
evt.preventDefault()
stopAtEndTime = false
videoElement.pause()
})
control.btnCut.addEventListener('click', () => {
const nowTime = Number(videoElement.currentTime).toFixed(2)
const btn = createCutButton(nowTime, videoElement)
control.cutBar.append(btn)
})
control.btnCut.addEventListener('contextmenu', (evt) => {
evt.preventDefault()
if (!control.cutBar) return
const timings = collectCutTiming(control.cutBar)
const newTimings = prompt(
'This is your current cut list. Change it to import cut from others.',
JSON.stringify(timings)
)
if (newTimings === null) return
const parsedNewTimings = (() => {
try {
return JSON.parse(newTimings)
} catch {
console.warn('Failed to parse the new cut list.')
return []
}
})()
if (JSON.stringify(timings) === JSON.stringify(parsedNewTimings)) {
console.log('No changes on the cut list.')
return
}
control.cutBar.innerHTML = ''
for (const i of parsedNewTimings) {
const btn = createCutButton(i, videoElement)
control.cutBar.append(btn)
}
})
// Start/end time setting
function updateURL() {
history.pushState(null, null, `#pvp${fromValue}-${toValue}`)
}
control.inputFrom.addEventListener('change', () => {
const input = control.inputFrom.value
if (input === '') {
fromValue = 0
control.inputFrom.placeholder = 'from 0'
return
}
const time = parseTime(input)
if (time === -1) {
control.btn.disabled = true
return
}
control.btn.disabled = false
fromValue = time
updateURL()
})
control.inputTo.addEventListener('change', () => {
const input = control.inputTo.value
if (input === '') {
toValue = videoElement.duration || 0
control.inputTo.placeholder = `to ${toValue.toFixed(2)}`
control.btn.innerText = 'Jump'
return
}
control.btn.innerText = 'Repeat'
const time = parseTime(input)
if (time === -1) {
control.btn.disabled = true
return
}
control.btn.disabled = false
toValue = time
updateURL()
})
// Button export
control.btnExport.addEventListener('click', (evt) => {
evt.preventDefault()
alert(`youtube-dl -f bestaudio "https://www.youtube.com/watch?v=${videoId}" \\
-x --audio-format mp3 --audio-quality 192k \\
--postprocessor-args "-ss ${fromValue} -to ${toValue} -af loudnorm=I=-16:TP=-2:LRA=11" \\
-o "output-%(id)s-${fromValue}-${toValue}.%(ext)s"`)
})
function setInitialDuration(dur) {
control.inputTo.placeholder = `to ${dur.toFixed(2)}`
const input = control.inputTo.value
if (input !== '') return
toValue = dur
}
if (videoElement.duration) {
setInitialDuration(videoElement.duration)
} else {
videoElement.addEventListener('loadedmetadata', () => {
setInitialDuration(videoElement.duration)
})
}
return control
}
class mockVideo {
constructor(player) {
this.pl = player
}
get currentTime() {
return this.pl.getCurrentTime()
}
set currentTime(sec) {
this.pl.seekTo(sec, true)
}
get duration() {
return this.pl.getDuration()
}
pause() {
this.pl.pauseVideo()
}
play() {
this.pl.playVideo()
}
addEventListener(name, cb) {
switch (name) {
case 'timeupdate': {
}
case 'loadedmetadata': {
this.pl.addEventListener('onready', cb)
}
}
}
}
class Pvp {
constructor(obj) {
this.videoId = obj.videoId // not used yet
this.player = obj.player
this.container = obj.container
this.initPvp()
}
initPvp() {
console.log('New video playback page found. Trying to insert the widget...')
const anchor = this.container
const mock = new mockVideo(this.player)
control = generateFullControl(mock, this.videoId)
anchor.append(control.app)
anchor.append(control.cutBar)
console.log(control)
}
}