-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
178 lines (149 loc) · 4.21 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
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
import { EventEmitter } from 'events'
import {
execFile as execFileCb,
spawn
} from 'child_process'
import assert from 'webassert'
import { promisify } from 'util'
import { defaultOptions } from './lib/default-options.js'
import { emitYouTubeDLEvents } from './lib/youtube-dl-events.js'
import { createError } from './lib/create-error.js'
export { downloadFromGithub } from './lib/download-yt-dlp.js'
const execFile = promisify(execFileCb)
const executableName = 'yt-dlp'
export class BcDLP {
constructor ({
binPath = executableName,
tag,
assetName,
platform,
arch
}) {
this.binPath = binPath
this.tag = tag
this.assetName = assetName
this.platform = platform
this.arch = arch
}
exec (
ytDlpArguments = [],
options = {}
) {
options = {
...defaultOptions(),
...options
}
assert(Array.isArray(ytDlpArguments), 'ytDlpArguments is an array')
assert(typeof options === 'object', 'options is an object')
const execEventEmitter = new EventEmitter()
const child = spawn(this.binPath, ytDlpArguments, options)
execEventEmitter.child = child
let stderrData = ''
let processError
child.stdout.on('data', (data) => emitYouTubeDLEvents({
stringData: data.toString(),
emitter: execEventEmitter
}))
child.stderr.on('data', (data) => (stderrData += data.toString()))
child.on('error', (error) => (processError = error))
child.on('close', (code) => {
if (code === 0 || child.killed) {
execEventEmitter.emit('close', code)
} else {
execEventEmitter.emit('error', createError({ code, processError, stderrData }))
}
})
return execEventEmitter
}
async execPromise (
ytDlpArguments = [],
options = {}
) {
const { binPath } = this
options = {
...defaultOptions(),
...options
}
try {
const { stdout } = await execFile(
binPath,
ytDlpArguments,
options
)
return stdout
} catch (err) {
throw createError({ code: err, stderrData: err.stderr })
}
}
execStream (
ytDlpArguments = [],
options = {}
) {
options = {
...defaultOptions(),
...options
}
const { binPath } = this
ytDlpArguments = ytDlpArguments.concat(['-o', '-'])
const execEventEmitter = new EventEmitter()
const child = spawn(binPath, ytDlpArguments, options)
execEventEmitter.child = child
let stderrData = ''
let processError
child.stderr.on('data', (data) => {
const stringData = data.toString()
emitYouTubeDLEvents({
stringData: data.toString(),
emitter: execEventEmitter
})
stderrData += stringData
})
child.on('error', (error) => (processError = error))
child.on('close', (code) => {
if (code === 0 || child.killed) {
execEventEmitter.emit('close', code)
} else {
execEventEmitter.emit('error', createError({ code, processError, stderrData }))
}
})
return {
readStream: child.stdout,
execEventEmitter
}
}
async getExtractors () {
const ytDlpStdout = await this.execPromise(['--list-extractors'])
return ytDlpStdout.split('\n')
}
async getExtractorDescriptions () {
const ytDlpStdout = await this.execPromise(['--extractor-descriptions'])
return ytDlpStdout.split('\n')
}
async getHelp () {
const ytDlpStdout = await this.execPromise(['--help'])
return ytDlpStdout
}
async getUserAgent () {
const ytDlpStdout = await this.execPromise(['--dump-user-agent'])
return ytDlpStdout
}
async getVersion () {
const ytDlpStdout = await this.execPromise(['--version'])
return ytDlpStdout
}
async getVideoInfo (ytDlpArguments) {
if (typeof ytDlpArguments === 'string') { ytDlpArguments = [ytDlpArguments] }
if (
!ytDlpArguments.includes('-f') &&
!ytDlpArguments.includes('--format')
) { ytDlpArguments = ytDlpArguments.concat(['-f', 'best']) }
const ytDlpStdout = await this.execPromise(ytDlpArguments.concat(['--dump-json']))
try {
return JSON.parse(ytDlpStdout)
} catch (e) {
return JSON.parse(
'[' + ytDlpStdout.replace(/\n/g, ',').slice(0, -1) + ']'
)
}
}
}