-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
37 lines (33 loc) · 1.33 KB
/
index.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
const UpdateYoutube = require('./lib/updateYoutubeDL');
const { downloadVideo, getManifest } = require('./lib/youtubeDL');
const Q = require('bluebird');
const { flatten } = require('lodash')
const { readFileSync } = require('fs')
const parseString = Q.promisify(require('xml2js').parseString);
module.exports = (videoIDs = [], options = { updateYoutube: false }) => {
const { updateYoutube } = options
return UpdateYoutube(updateYoutube)
.then(youtubeDLPath => {
return Q.map(videoIDs, id => {
return getManifest(id)
.then(manifestString => {
return parseString(manifestString).then(xml => {
const { Period } = xml.MPD
const { AdaptationSet } = Period[0]
const videoAdaptation = AdaptationSet[1]
const { Representation } = videoAdaptation
return Q.map(Representation, r => {
const { SegmentBase, $ } = r
const BaseURL = r.BaseURL[0]._
const ext = BaseURL.indexOf('video/mp4') > -1 ? "mp4" : "webm"
return downloadVideo(id, Object.assign({}, options, {
format: $.id,
ext: ext
}))
}, { concurrency: 1 })
})
})
}, { concurrency: 1 })
.then(results=> flatten(results))
})
}