This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
212 lines (187 loc) · 5.06 KB
/
index.ts
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
import ytdl, { downloadOptions } from 'ytdl-core';
import { opus as Opus, FFmpeg } from 'prism-media';
import { Readable, Duplex } from 'stream';
// ytdl events
const evn = [
'info',
'progress',
'abort',
'request',
'response',
'error',
'redirect',
'retry',
'reconnect',
];
interface YTDLStreamOptions extends downloadOptions {
seek?: number;
encoderArgs?: string[];
fmt?: string;
opusEncoded?: boolean;
}
interface StreamOptions {
seek?: number;
encoderArgs?: string[];
fmt?: string;
opusEncoded?: boolean;
}
/**
* Create an opus stream for your video with provided encoder args
* @param url - YouTube URL of the video
* @param options - YTDL options
* @example const ytdl = require("discord-ytdl-core");
* const stream = ytdl("VIDEO_URL", {
* seek: 3,
* encoderArgs: ["-af", "bass=g=10"],
* opusEncoded: true
* });
* VoiceConnection.play(stream, {
* type: "opus"
* });
*/
const StreamDownloader = (url: string, options?: YTDLStreamOptions) => {
if (!url) {
throw new Error('No input url provided');
}
if (typeof url !== 'string') {
throw new SyntaxError(
`input URL must be a string. Received ${typeof url}!`
);
}
options ??= {};
let FFmpegArgs: string[] = [
'-analyzeduration',
'0',
'-loglevel',
'0',
'-f',
`${typeof options.fmt === 'string' ? options.fmt : 's16le'}`,
'-ar',
'48000',
'-ac',
'2',
];
if (!isNaN(options.seek)) {
FFmpegArgs.unshift('-ss', options.seek.toString());
}
if (Array.isArray(options.encoderArgs)) {
FFmpegArgs = FFmpegArgs.concat(options.encoderArgs);
}
const transcoder = new FFmpeg({
args: FFmpegArgs,
});
const inputStream = ytdl(url, options);
const output = inputStream.pipe(transcoder);
if (options && !options.opusEncoded) {
for (const event of evn) {
inputStream.on(event, (...args) => output.emit(event, ...args));
}
inputStream.on('error', () => transcoder.destroy());
output.on('close', () => transcoder.destroy());
return output;
}
const opus = new Opus.Encoder({
rate: 48000,
channels: 2,
frameSize: 960,
});
const outputStream = output.pipe(opus);
output.on('error', (e) => outputStream.emit('error', e));
for (const event of evn) {
inputStream.on(event, (...args) => outputStream.emit(event, ...args));
}
outputStream.on('close', () => {
transcoder.destroy();
opus.destroy();
});
return outputStream;
};
/**
* Creates arbitraryStream
* @param stream Any readable stream source
* @param options Stream options
* @example const streamSource = "https://listen.moe/kpop/opus";
* let stream = ytdl.arbitraryStream(streamSource, {
* encoderArgs: ["-af", "asetrate=44100*1.25"],
* fmt: "mp3"
* });
*
* stream.pipe(fs.createWriteStream("kpop.mp3"));
*/
const arbitraryStream = (
stream: string | Readable | Duplex,
options?: StreamOptions
) => {
if (!stream) {
throw new Error('No stream source provided');
}
options ??= {};
let FFmpegArgs: string[];
if (typeof stream === 'string') {
FFmpegArgs = [
'-reconnect',
'1',
'-reconnect_streamed',
'1',
'-reconnect_delay_max',
'5',
'-i',
stream,
'-analyzeduration',
'0',
'-loglevel',
'0',
'-f',
`${typeof options.fmt === 'string' ? options.fmt : 's16le'}`,
'-ar',
'48000',
'-ac',
'2',
];
} else {
FFmpegArgs = [
'-analyzeduration',
'0',
'-loglevel',
'0',
'-f',
`${typeof options.fmt === 'string' ? options.fmt : 's16le'}`,
'-ar',
'48000',
'-ac',
'2',
];
}
if (!isNaN(options.seek)) {
FFmpegArgs.unshift('-ss', options.seek.toString());
}
if (Array.isArray(options.encoderArgs)) {
FFmpegArgs = FFmpegArgs.concat(options.encoderArgs);
}
let transcoder = new FFmpeg({
args: FFmpegArgs,
});
if (typeof stream !== 'string') {
transcoder = stream.pipe(transcoder);
stream.on('error', () => transcoder.destroy());
}
if (options && !options.opusEncoded) {
transcoder.on('close', () => transcoder.destroy());
return transcoder;
}
const opus = new Opus.Encoder({
rate: 48000,
channels: 2,
frameSize: 960,
});
const outputStream = transcoder.pipe(opus);
outputStream.on('close', () => {
transcoder.destroy();
opus.destroy();
});
return outputStream;
};
StreamDownloader.arbitraryStream = arbitraryStream;
StreamDownloader.version = require('./package.json').version;
const DiscordYTDLCore = Object.assign(StreamDownloader, ytdl);
export = DiscordYTDLCore;