npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@spikebot/discord-ytdl-core

v1.0.4

Published

Simple ytdl wrapper for discord bots with custom ffmpeg args support.

Downloads

15

Readme

discord-ytdl-core

Simple ytdl wrapper for discord bots with custom ffmpeg args support.

Installing

First of all, you need to install ytdl-core with npm install --save ytdl-core.

Install ytdl core

$ npm i ytdl-core

Install discord ytdl core

$ npm i discord-ytdl-core

https://www.npmjs.com/package/discord-ytdl-core

Opus [optional]

Please install opus engine if you want to encode the stream to opus format.

Supported Opus Engines

API

YTDL(youtubeURL, options)

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"));

YTDL.arbitraryStream(source, options)

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"))

Options

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 for ogg/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 returns opus encoded stream. If fmt option isn't provided, it returns converted stream type of discord.js. Other values returns unknown stream if opusEncoded is false.

  • fmt: Forcefully changes the stream format. Don't use this option for default value. Even though this option changes the format, it returns opus stream if opusEncoded is set to true.

  • Other options are the options for ytdl-core.

Example

Playing Opus Encoded Stream

const { ytdl } = require("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");

Unknown Stream

const { ytdl } = require("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");

Converted Stream

const { ytdl } = require("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");

Downloading the video

const { ytdl } = require("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"));

Arbitrary Stream

const { ytdl } = require("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");

Other functions

Check out ytdl-core for other functions.

Sponsor this project

Related