youtube-dl.js
8.68 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
'use strict'
const execFile = require('child_process').execFile
const streamify = require('streamify')
const request = require('request')
const hms = require('hh-mm-ss')
const path = require('path')
const http = require('http')
const url = require('url')
const fs = require('fs')
const {
isYouTubeRegex,
isWin,
formatDuration,
has,
isString
} = require('./util')
const detailsPath = path.join(__dirname, '..', 'bin/details')
const TEN_MEGABYTES = 1000 * 1000 * 10
const execFileOpts = { maxBuffer: TEN_MEGABYTES }
let ytdlBinary = (() => {
if (fs.existsSync(detailsPath)) {
const details = JSON.parse(fs.readFileSync(detailsPath))
return details.path
? details.path
: path.resolve(__dirname, '..', 'bin', details.exec)
}
if (!fs.existsSync(ytdlBinary)) {
console.error(
'ERROR: unable to locate youtube-dl details in ' +
path.dirname(ytdlBinary)
)
process.exit(1)
}
})()
function youtubeDl (args, options, cb) {
execFile(ytdlBinary, args, { ...execFileOpts, ...options }, function done (
err,
stdout,
stderr
) {
if (err) return cb(err)
return cb(null, stdout.trim().split(/\r?\n/))
})
}
/**
* Processes data
*
* @param {Object} data
* @param {Object} options
* @param {Object} stream
*/
function processData (data, options, stream) {
const item = !data.length ? data : data.shift()
// fix for pause/resume downloads
const headers = Object.assign(
{ Host: url.parse(item.url).hostname },
data.http_headers
)
if (options && options.start > 0 && options.end > 0) {
headers.Range = 'bytes=' + options.start + '-' + options.end
}
const req = request({ url: item.url, headers: headers, ecdhCurve: 'auto' })
req.on('response', function response (res) {
const size = parseInt(res.headers['content-length'], 10)
if (size) item.size = size
if (options && options.start > 0 && res.statusCode === 416) {
// the file that is being resumed is complete.
return stream.emit('complete', item)
}
if (res.statusCode !== 200 && res.statusCode !== 206) {
return stream.emit('error', new Error('status code ' + res.statusCode))
}
stream.emit('info', item)
stream.on('end', function end () {
if (data.length) stream.emit('next', data)
})
})
return stream.resolve(req)
}
/**
* Downloads a video.
*
* @param {String} videoUrl
* @param {!Array.<String>} args
* @param {!Object} options
*/
const ytdl = (module.exports = function (videoUrl, args, options) {
const stream = streamify({
superCtor: http.ClientResponse,
readable: true,
writable: false
})
if (!isString(videoUrl)) {
processData(videoUrl, options, stream)
return stream
}
ytdl.getInfo(videoUrl, args, options, function getInfo (err, data) {
return err ? stream.emit('error', err) : processData(data, options, stream)
})
return stream
})
/**
* Calls youtube-dl with some arguments and the `cb`
* gets called with the output.
*
* @param {String|Array.<String>}
* @param {Array.<String>} args
* @param {Array.<String>} args2
* @param {Object} options
* @param {Function(!Error, String)} cb
*/
function call (urls, args1, args2, options = {}, cb) {
let args = args1
if (args2) args = args.concat(args2)
// check if encoding is already set
if (isWin && !args.includes('--encoding')) {
args.push('--encoding')
args.push('utf8')
}
if (urls !== null) {
if (isString(urls)) urls = [urls]
for (let i = 0; i < urls.length; i++) {
const video = urls[i]
if (isYouTubeRegex.test(video)) {
// Get possible IDs.
const details = url.parse(video, true)
let id = details.query.v || ''
if (id) {
args.push('http://www.youtube.com/watch?v=' + id)
} else {
// Get possible IDs for youtu.be from urladdr.
id = details.pathname.slice(1).replace(/^v\//, '')
if (id) {
args.push(video)
args.unshift('-i')
}
}
} else {
if (i === 0) args.push('--')
args.push(video)
}
}
}
return youtubeDl(args, options, cb)
}
/**
* Calls youtube-dl with some arguments and the `cb`
* gets called with the output.
*
* @param {String} url
* @param {Array.<String>} args
* @param {Object} options
* @param {Function(!Error, String)} cb
*/
ytdl.exec = function exec (url, args, options, cb) {
return call(url, [], args, options, cb)
}
/**
* @param {Object} data
* @returns {Object}
*/
function parseInfo (data) {
// youtube-dl might return just an url as a string when using the "-g" or "--get-url" flag
if (isString(data) && data.startsWith('http')) {
data = JSON.stringify({ url: data })
}
const info = JSON.parse(data)
info._duration_raw = info.duration
info._duration_hms = info.duration
? hms.fromS(info.duration, 'hh:mm:ss')
: info.duration
info.duration = info.duration ? formatDuration(info.duration) : info.duration
return info
}
/**
* Set path from youtube-dl.
*
* @param {String} path
*/
ytdl.setYtdlBinary = function setYtdlBinary (path) {
ytdlBinary = path
}
/**
* Get path from youtube-dl.
*
* @param {String} path
*/
ytdl.getYtdlBinary = function getYtdlBinary () {
return ytdlBinary
}
/**
* Gets info from a video.
*
* @param {String} url
* @param {Array.<String>} args
* @param {Object} options
* @param {Function(!Error, Object)} cb
*/
ytdl.getInfo = function getInfo (url, args, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
} else if (typeof args === 'function') {
cb = args
options = {}
args = []
}
const defaultArgs = ['--dump-json']
if (
!args ||
(!has(args, '-f') &&
!has(args, '--format') &&
args.every(function (a) {
return a.indexOf('--format=') !== 0
}))
) {
defaultArgs.push('-f')
defaultArgs.push('best')
}
call(url, defaultArgs, args, options, function done (err, data) {
if (err) return cb(err)
let info
// If using the "-g" or "--get-url" flag youtube-dl will return just a string (the URL to the video) which messes up the parsing
// This fixes this behaviour
if (has(args, '-g') || has(args, '--get-url')) {
if (Array.isArray(data) && data.length >= 2) data.splice(0, 1)
}
try {
info = data.map(parseInfo)
} catch (err) {
return cb(err)
}
return cb(null, info.length === 1 ? info[0] : info)
})
}
/**
* @param {String} url
* @param {Object} options
* {Boolean} auto
* {Boolean} all
* {String} lang
* {String} format
* {String} cwd
* @param {Function(!Error, Object)} cb
*/
ytdl.getSubs = function getSubs (url, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}
const args = ['--skip-download']
args.push('--write' + (options.auto ? '-auto' : '') + '-sub')
if (options.all) args.push('--all-subs')
if (options.lang) args.push('--sub-lang=' + options.lang)
if (options.format) args.push('--sub-format=' + options.format)
if (!options.warrning) args.push('--no-warnings')
call(url, args, [], { cwd: options.cwd }, function (err, data) {
if (err) return cb(err)
const files = []
for (let i = 0, len = data.length; i < len; i++) {
const line = data[i]
if (line.indexOf('[info] Writing video subtitles to: ') === 0) {
files.push(line.slice(35))
}
}
return cb(null, files)
})
}
/**
* @param {String} url
* @param {Object} options
* {Boolean} all
* {String} cwd
* @param {Function(!Error, Object)} cb
*/
ytdl.getThumbs = function getThumbs (url, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
}
const args = ['--skip-download']
if (options.all) args.push('--write-all-thumbnails')
else args.push('--write-thumbnail')
if (!options.warrning) args.push('--no-warnings')
call(url, args, [], { cwd: options.cwd }, function (err, data) {
if (err) return cb(err)
const files = []
for (let i = 0, len = data.length; i < len; i++) {
const line = data[i]
const info = 'Writing thumbnail to: '
if (has(line, info)) {
files.push(line.slice(line.indexOf(info) + info.length))
}
}
return cb(null, files)
})
}
/**
* @param {!Boolean} descriptions
* @param {!Object} options
* @param {Function(!Error, Object)} cb
*/
ytdl.getExtractors = function getExtractors (descriptions, options, cb) {
if (typeof options === 'function') {
cb = options
options = {}
} else if (typeof descriptions === 'function') {
cb = descriptions
options = {}
descriptions = false
}
const args = descriptions
? ['--extractor-descriptions']
: ['--list-extractors']
return call(null, args, null, options, cb)
}