Skip to content

Commit a1bff70

Browse files
committed
deps: update ytdl-core to latest version
1 parent 7260efe commit a1bff70

File tree

2 files changed

+34
-38
lines changed

2 files changed

+34
-38
lines changed

index.js

+33-37
Original file line numberDiff line numberDiff line change
@@ -22,44 +22,40 @@ function nextBestFormat(formats, isLive) {
2222
return formats.find(format => !format.bitrate) || formats[0];
2323
}
2424

25-
function download(url, options = {}) {
26-
return new Promise((resolve, reject) => {
27-
ytdl.getInfo(url, (err, info) => {
28-
if (err) return reject(err);
29-
// Prefer opus
30-
const format = info.formats.find(filter);
31-
const canDemux = format && info.length_seconds != 0;
32-
if (canDemux) options = { ...options, filter };
33-
else if (info.length_seconds != 0) options = { ...options, filter: 'audioonly' };
34-
if (canDemux) {
35-
const demuxer = new prism.opus.WebmDemuxer();
36-
return resolve(ytdl.downloadFromInfo(info, options).pipe(demuxer).on('end', () => demuxer.destroy()));
37-
} else {
38-
const bestFormat = nextBestFormat(info.formats, info.player_response.videoDetails.isLiveContent);
39-
if (!bestFormat) return reject('No suitable format found');
40-
const transcoder = new prism.FFmpeg({
41-
args: [
42-
'-reconnect', '1',
43-
'-reconnect_streamed', '1',
44-
'-reconnect_delay_max', '5',
45-
'-i', bestFormat.url,
46-
'-analyzeduration', '0',
47-
'-loglevel', '0',
48-
'-f', 's16le',
49-
'-ar', '48000',
50-
'-ac', '2',
51-
],
52-
});
53-
const opus = new prism.opus.Encoder({ rate: 48000, channels: 2, frameSize: 960 });
54-
const stream = transcoder.pipe(opus);
55-
stream.on('close', () => {
56-
transcoder.destroy();
57-
opus.destroy();
58-
});
59-
return resolve(stream);
60-
}
25+
async function download(url, options = {}) {
26+
const info = await ytdl.getInfo(url);
27+
// Prefer opus
28+
const format = info.formats.find(filter);
29+
const canDemux = format && info.length_seconds != 0;
30+
if (canDemux) options = { ...options, filter };
31+
else if (info.length_seconds != 0) options = { ...options, filter: 'audioonly' };
32+
if (canDemux) {
33+
const demuxer = new prism.opus.WebmDemuxer();
34+
return ytdl.downloadFromInfo(info, options).pipe(demuxer).on('end', () => demuxer.destroy());
35+
} else {
36+
const bestFormat = nextBestFormat(info.formats, info.player_response.videoDetails.isLiveContent);
37+
if (!bestFormat) throw new Error('No suitable format found');
38+
const transcoder = new prism.FFmpeg({
39+
args: [
40+
'-reconnect', '1',
41+
'-reconnect_streamed', '1',
42+
'-reconnect_delay_max', '5',
43+
'-i', bestFormat.url,
44+
'-analyzeduration', '0',
45+
'-loglevel', '0',
46+
'-f', 's16le',
47+
'-ar', '48000',
48+
'-ac', '2',
49+
],
6150
});
62-
});
51+
const opus = new prism.opus.Encoder({ rate: 48000, channels: 2, frameSize: 960 });
52+
const stream = transcoder.pipe(opus);
53+
stream.on('close', () => {
54+
transcoder.destroy();
55+
opus.destroy();
56+
});
57+
return stream;
58+
}
6359
}
6460

6561
module.exports = Object.assign(download, ytdl);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dependencies": {
2929
"@types/node": "^14.0.13",
3030
"prism-media": "^1.2.2",
31-
"ytdl-core": "^3.1.2"
31+
"ytdl-core": "^3.2.2"
3232
},
3333
"devDependencies": {
3434
"eslint": "^7.3.0"

0 commit comments

Comments
 (0)