-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathaudio-stream.js
More file actions
34 lines (30 loc) · 1.13 KB
/
Copy pathaudio-stream.js
File metadata and controls
34 lines (30 loc) · 1.13 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
const ytdl = require('youtube-dl-exec');
const fetch = require("node-fetch");
const ffmpeg = require('fluent-ffmpeg');
const config = require('./config');
const {sendChannelMessage, isYouTubeLink} = require("./utils");
if (config.ffmpegExecutablePath)
ffmpeg.setFfmpegPath(config.ffmpegExecutablePath);
const ytdlOptions = {
dumpSingleJson: true,
noPlaylist: true,
abortOnError: true,
playlistEnd: 1,
youtubeSkipDashManifest: true,
};
async function stream(url, client) {
return ytdl(url, {
...ytdlOptions,
...(config.cookiesEnabled && isYouTubeLink(url)) && {cookie: `${__dirname}/cookies.txt`}
}).then(output => {
if (output?.url) return output.url;
else if (output?.requested_formats) return output?.requested_formats.filter(f => f.format.includes("audio"))[0].url;
else return output.entries.filter(f => f.format.includes("audio"))[0].url;
}).then(url => fetch(url)).then(res => res.body).then(body => ffmpeg().input(body)
.addOption('-f s16le')
.addOption('-acodec pcm_s16le')
.addOption('-ac 2')
.addOption('-ar 44100')
.on('error', (err) => err));
}
module.exports = stream;