-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
449 lines (359 loc) · 12 KB
/
index.js
File metadata and controls
449 lines (359 loc) · 12 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
const puppeteer = require('puppeteer-core')
const stream = require('stream')
const fs = require('fs')
const path = require('path')
const { exit } = require('process')
const { Stream } = require('stream')
const { create } = require('domain')
const { EventEmitter } = require('events')
const logPrefix = '[videox]'
let deviceModel = puppeteer.devices['iPad']
deviceModel = null
module.exports = NewInstance
// Exposed videox class constructor.
function NewInstance(options) {
EventEmitter.call(this)
// Default options.
this.options = {
debug: false,
headless: true,
downloadBrowser: false,
logTo: process.stdout,
browserExecutePath: '/usr/bin/chromium',
browserArgs: [],
downloadAsFile: true,
downloadPath: '',
checkCompleteLoopInterval: 100,
waitForNextDataTimeout: 5000,
}
Object.assign(this.options, options)
if (this.options.downloadAsFile) {
this.on('data', writeToFile)
}
return this
}
NewInstance.prototype = EventEmitter.prototype
// Open browser and a new page, be prepared for nevigating.
NewInstance.prototype.init = async function () {
options = this.options
if (options.downloadBrowser) {
const browserFetcher = puppeteer.createBrowserFetcher()
const revisionInfo = await this._to(
browserFetcher.download('800071'),
'download browser'
)
options.browserExecutePath = revisionInfo.executablePath
this._log('downloaded browser in path: ' + options.browserExecutePath)
}
this.browser = await this._to(
puppeteer.launch({
headless: options.headless,
args: options.browserArgs,
executablePath: path.resolve(options.browserExecutePath),
}),
'launch puppeteer'
)
}
// Launch download process.
NewInstance.prototype.get = async function (pageUrl) {
let createdDownloadStream = false
let totalBytes = 0
const page = (this.page = await this._to(
this.browser.newPage(),
'open a new page'
))
if (deviceModel) {
await this._to(page.emulate(deviceModel), 'emulate device')
}
await page.exposeFunction('__videoxLog__', async (str) => {
this._log(str)
})
await page.exposeFunction('__videoxLogRaw__', async (str) => {
this._logRaw(str)
})
await page.exposeFunction('__videoxFatal__', async (str) => {
this._log(str)
await page.close()
})
// chunk is base64 string.
await page.exposeFunction(
'__videoxWrite__',
async (objectUrl, mimeCodec, base64Chunk) => {
const buf = Buffer.from(base64Chunk, 'base64')
totalBytes += buf.length
this.emit('data', objectUrl, mimeCodec, buf)
}
)
// Overwrite some functions MSE would use.
// See: https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API
await page.evaluateOnNewDocument(async () => {
await window.__videoxLog__('evaluateOnNewDocument:')
// Used to record some data.
window.__videoxObj = {
mediaSources: {},
sourceBufferIndex: 0,
enumDownloadStatus: {
DS_UNINTIALIZED: 0,
DS_DOWNLOADING: 1,
DS_COMPLETED: 2,
DS_END: 3,
},
}
window.__videoxObj.downloadStatus =
window.__videoxObj.enumDownloadStatus.DS_UNINTIALIZED
const createObjectURLOrigin = URL.createObjectURL
const addSourceBufferOrigin = MediaSource.prototype.addSourceBuffer
const endOfStreamOrigin = MediaSource.prototype.endOfStream
const appendBufferOrigin = SourceBuffer.prototype.appendBuffer
URL.createObjectURL = function () {
const objUrl = createObjectURLOrigin.apply(this, arguments)
if (!arguments[0] instanceof MediaSource) {
return objUrl
}
window.__videoxLog__('createObjectURL: ' + objUrl)
arguments[0].__videoxUrl = objUrl
// Register this mediasource in window.
window.__videoxObj.mediaSources[objUrl] = {
self: arguments[0],
sourceBuffers: {},
}
return objUrl
}
MediaSource.prototype.addSourceBuffer = function () {
window.__videoxLog__('addSourceBuffer, mimeCodec: ' + arguments[0])
const sourceBuf = addSourceBufferOrigin.apply(this, arguments)
const obj = window.__videoxObj
sourceBuf.addEventListener('updateend', seek)
sourceBuf.__videoxMediaSource = this
sourceBuf.__videoxKey = Number.prototype.toString.call(
obj.sourceBufferIndex++
)
obj.mediaSources[this.__videoxUrl].sourceBuffers[
sourceBuf.__videoxKey
] = {
self: sourceBuf,
mimeCodec: arguments[0],
}
window.__videoxLog__('download status: ' + obj.downloadStatus)
switch (obj.downloadStatus) {
case obj.enumDownloadStatus.DS_END:
case obj.enumDownloadStatus.DS_COMPLETED:
case obj.enumDownloadStatus.DS_UNINTIALIZED:
obj.downloadStatus = obj.enumDownloadStatus.DS_DOWNLOADING
break
case obj.enumDownloadStatus.DS_DOWNLOADING:
// There may be multiple sourceBuffer added.
break
default:
window.__videoxFatal__(
'addSourceBuffer wrong download status: ' + obj.downloadStatus
)
break
}
window.__videoxLog__('download status changed to: ' + obj.downloadStatus)
return sourceBuf
}
MediaSource.prototype.endOfStream = function () {
window.__videoxLog__('endOfStream')
const obj = window.__videoxObj
Array.prototype.forEach.call(this.sourceBuffers, (e) => {
e.removeEventListener('updateend', seek)
})
window.__videoxLog__('download status: ' + obj.downloadStatus)
switch (obj.downloadStatus) {
case obj.enumDownloadStatus.DS_DOWNLOADING:
obj.downloadStatus = obj.enumDownloadStatus.DS_COMPLETED
break
case obj.enumDownloadStatus.DS_COMPLETED:
case obj.enumDownloadStatus.DS_UNINTIALIZED:
case obj.enumDownloadStatus.DS_END:
default:
window.__videoxFatal__(
'endOfStream wrong download status: ' + obj.downloadStatus
)
break
}
window.__videoxLog__('download status changed to: ' + obj.downloadStatus)
return endOfStreamOrigin.apply(this, arguments)
}
SourceBuffer.prototype.appendBuffer = function () {
const obj = window.__videoxObj
// Get mimeCodec string corresponding to this sourceBuffer instance.
const mediaSource = this.__videoxMediaSource
const sourceBufferInfo =
obj.mediaSources[mediaSource.__videoxUrl].sourceBuffers[
this.__videoxKey
]
if (obj.downloadStatus !== obj.enumDownloadStatus.DS_DOWNLOADING) {
window.__videoxFatal__(
'appendBuffer wrong download status: ' +
obj.downloadStatus +
mediaSource.__videoxUrl
)
}
if (!sourceBufferInfo.mimeCodec) {
window.__videoxFatal__('empty mimeCodec')
}
// Serialize the bytes chunk. Needed for tranmitting data between browser and program.
const arr = new Uint8Array(arguments[0])
const len = arr.byteLength
let b = ''
for (let i = 0; i < len; i++) {
b += String.fromCharCode(arr[i])
}
const sed = window.btoa(b)
window.__videoxWrite__(
mediaSource.__videoxUrl,
sourceBufferInfo.mimeCodec,
sed
)
return appendBufferOrigin.apply(this, arguments)
}
function seek() {
// Seek to max buffered time.
const seekNext = () => {
const obj = window.__videoxObj
const mediaSource = this.__videoxMediaSource
const sourceBuffers =
obj.mediaSources[mediaSource.__videoxUrl].sourceBuffers
if (Object.keys(sourceBuffers).length === 0) {
window.__videoxFatal__('suorceBuffers size 0')
}
const videoEle = document.querySelector('video')
if (!videoEle) {
window.__videoxFatal__('seek, videoEle null')
}
if (
videoEle.buffered.length === 1 &&
videoEle.currentTime < videoEle.buffered.end(0)
) {
videoEle.currentTime = videoEle.buffered.end(0)
}
// Print download progress in percentage.
let percent =
((videoEle.currentTime / mediaSource.duration) * 100).toFixed(2) + '%'
let str =
'[videox] download progress: ' +
percent +
' video.currentTime: ' +
videoEle.currentTime
window.__videoxLog__(str)
}
// Place seek operation in next eventloop waiting for updated timeEnd value.
setTimeout(seekNext, 1)
}
})
await this._to(
page.goto(pageUrl, { waitUntil: 'networkidle2' }),
'page goto: ' + pageUrl
)
// Check download status.
await this._to(
page.evaluate(
async (options) => {
const obj = window.__videoxObj
if (Object.keys(obj.mediaSources).length === 0) {
await window.__videoxFatal__(
"can't find MediaSource object in this page"
)
}
checkLoop: for (;;) {
const videoEle = document.querySelector('video')
if (!videoEle) {
window.__videoxFatal__('videoEle null')
}
if (videoEle.paused) {
// await window.__videoxLog__('playing video')
//await videoEle.play()
}
switch (obj.downloadStatus) {
case obj.enumDownloadStatus.DS_UNINTIALIZED:
case obj.enumDownloadStatus.DS_DOWNLOADING:
break
case obj.enumDownloadStatus.DS_END:
break checkLoop
case obj.enumDownloadStatus.DS_COMPLETED:
window.__videoxLog__('download status: ' + obj.downloadStatus)
obj.downloadStatus = obj.enumDownloadStatus.DS_END
window.__videoxLog__(
'download status changed to: ' + obj.downloadStatus
)
await new Promise((resolve) =>
setTimeout(() => resolve(), options.waitForNextDataTimeout)
)
continue checkLoop
default:
window.__videoxFatal__(
'endOfStream wrong download status: ' + obj.downloadStatus
)
break
}
await new Promise((resolve) =>
setTimeout(() => resolve(), options.checkCompleteLoopInterval)
)
}
},
{
checkCompleteLoopInterval: this.options.checkCompleteLoopInterval,
waitForNextDataTimeout: this.options.waitForNextDataTimeout,
}
),
'page evaluate'
)
await this._to(page.close(), 'close page: ' + pageUrl)
this._log('downloaded total bytes: ' + totalBytes)
}
// Close the browser.
NewInstance.prototype.destroy = async function () {
await this._to(this.browser.close(), 'close browser')
}
// Catch promise and log.
NewInstance.prototype._to = async function (pms, msg) {
let err, data
this._log(msg + ' ...')
;[err, data] = await to(pms)
if (err) {
throw new Error(msg + ' error: ' + err)
}
this._log(msg + ' OK')
return data
}
NewInstance.prototype._logRaw = function (str) {
tlog(this.options.logTo, str, true)
}
NewInstance.prototype._log = function (str) {
tlog(this.options.logTo, str, false)
}
function tlog(out, msg, raw) {
if (out && out instanceof stream.Writable) {
let str
if (!raw) {
str = `${logPrefix} ${new Date().toISOString()}: ${msg}\n`
} else {
str = msg
}
out.write(str)
}
}
function to(pms) {
return pms.then((data) => [null, data]).catch((err) => [err, null])
}
function writeToFile(objectUrl, mimeCodec, chunk) {
options = this.options
const mimeCodecSplited = String.prototype.split.call(mimeCodec, /\/|;/)
if (mimeCodecSplited.length < 2) {
throw new Error('invalid type: ' + mimeCodec)
}
let videoOrAudio = mimeCodecSplited[0]
let ext = mimeCodecSplited[1]
const childPath = path.normalize(
objectUrl.replace(/("|:|<|>|\||\*|\?|\/|\\)/g, '-')
)
const filePath = path.resolve(
options.downloadPath,
childPath,
videoOrAudio + '.' + ext
)
fs.mkdirSync(path.dirname(filePath), { recursive: true })
fs.writeFileSync(filePath, chunk, { flag: 'as' })
}