This fork is dedicated to fixing bugs and adding features that are not merged into the original repo as soon as possible.
Simple ytdl wrapper for discord bots with custom ffmpeg args support.
First of all, you need to install
ytdl-core
withnpm install --save ytdl-core
.
$ npm i ytdl-core
$ npm i @rafateoli/discord-ytdl-core
https://www.npmjs.com/package/@rafateoli/discord-ytdl-core
Please install opus engine if you want to encode the stream to opus format.
Similar to ytdl-core but this method allows you to pass custom FFmpeg args in options.
ytdl('https://www.youtube.com/watch?v=QnL5P0tFkwM', {
filter: 'audioonly',
fmt: 'mp3',
encoderArgs: ['-af', 'bass=g=10'],
}).pipe(fs.createWritestream('bass_boosted.mp3'));
This method allows you to play the stream from other sources rather than just youtube
. Stream source must be a string or stream object (internal.Readable | internal.Duplex).
Through URL: https://listen.moe/kpop/opus
Using fs:
let stream = fs.createReadStream('./music.mp4');
ytdl.arbitraryStream(stream, {
fmt: 'mp3',
encoderArgs: ['bass=g=5'],
}).pipe(fs.createWriteStream('./music.mp3'));
This package provides 4 extra options excluding ytdl-core options.
-
seek
: This option takes the time in seconds. If this option is provided, it will return the stream from that frame. Seek option is provided here because discord.js seek doesn't work forogg/opus
&webm/opus
stream. This option is ignored when the supplied parameter type isn't a number. -
encoderArgs
: This option takes the Array of FFmpeg arguments. Invalid args will throw error and crash the process. This option is ignored when the supplied parameter type isn't array. Invalid FFmpeg args might crash the process. -
opusEncoded
: This option takes a Boolean value. If true, it returnsopus encoded
stream. Iffmt
option isn't provided, it returnsconverted
stream type of discord.js. Other values returnsunknown
stream ifopusEncoded
is false. -
fmt
: Forcefully changes the stream format. Don't use this option for default value. Even though this option changes the format, it returnsopus
stream ifopusEncoded
is set totrue
. -
Other options are the options for ytdl-core.
const { ytdl } = require('@rafateoli/discord-ytdl-core');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', (msg) => {
if (msg.author.bot || !msg.guild) return;
if (msg.content === '!play') {
if (!msg.member.voice.channel)
return msg.channel.send("You're not in a voice channel?");
let stream = ytdl('https://www.youtube.com/watch?v=QnL5P0tFkwM', {
filter: 'audioonly',
opusEncoded: true,
encoderArgs: ['-af', 'bass=g=10,dynaudnorm=f=200'],
});
msg.member.voice.channel.join().then((connection) => {
let dispatcher = connection
.play(stream, {
type: 'opus',
})
.on('finish', () => {
msg.guild.me.voice.channel.leave();
});
});
}
});
client.login('TOKEN');
const { ytdl } = require('@rafateoli/discord-ytdl-core');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', (msg) => {
if (msg.author.bot || !msg.guild) return;
if (msg.content === '!play') {
if (!msg.member.voice.channel)
return msg.channel.send("You're not in a voice channel?");
let stream = ytdl('https://www.youtube.com/watch?v=QnL5P0tFkwM', {
filter: 'audioonly',
opusEncoded: false,
fmt: 'mp3',
encoderArgs: ['-af', 'bass=g=10,dynaudnorm=f=200'],
});
msg.member.voice.channel.join().then((connection) => {
let dispatcher = connection
.play(stream, {
type: 'unknown',
})
.on('finish', () => {
msg.guild.me.voice.channel.leave();
});
});
}
});
client.login('TOKEN');
const { ytdl } = require('@rafateoli/discord-ytdl-core');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', (msg) => {
if (msg.author.bot || !msg.guild) return;
if (msg.content === '!play') {
if (!msg.member.voice.channel)
return msg.channel.send("You're not in a voice channel?");
let stream = ytdl('https://www.youtube.com/watch?v=QnL5P0tFkwM', {
filter: 'audioonly',
opusEncoded: false,
encoderArgs: ['-af', 'bass=g=10,dynaudnorm=f=200'],
});
msg.member.voice.channel.join().then((connection) => {
let dispatcher = connection
.play(stream, {
type: 'converted',
})
.on('finish', () => {
msg.guild.me.voice.channel.leave();
});
});
}
});
client.login('TOKEN');
const { ytdl } = require('@rafateoli/discord-ytdl-core');
const { createWriteStream } = require('fs');
const url = 'https://www.youtube.com/watch?v=QnL5P0tFkwM';
let stream = ytdl(url, {
encoderArgs: ['-af', 'asetrate=44100*1.25,bass=g=20,dynaudnorm=f=150'],
fmt: 'mp3',
opusEncoded: false,
});
stream.pipe(createWriteStream(__dirname + '/test.mp3'));
const { ytdl } = require('@rafateoli/discord-ytdl-core');
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log('ready');
});
client.on('message', (msg) => {
if (msg.author.bot || !msg.guild) return;
if (msg.content === '!play') {
if (!msg.member.voice.channel)
return msg.channel.send("You're not in a voice channel?");
let stream = ytdl.arbitraryStream('https://listen.moe/kpop/opus', {
opusEncoded: true,
encoderArgs: ['-af', 'bass=g=10,dynaudnorm=f=200'],
});
msg.member.voice.channel.join().then((connection) => {
let dispatcher = connection
.play(stream, {
type: 'opus',
})
.on('finish', () => {
msg.guild.me.voice.channel.leave();
});
});
}
});
client.login('TOKEN');
Check out ytdl-core for other functions.