/disnake-ext-music

An easy-to-use music extension for disnake.

Primary LanguagePythonMIT LicenseMIT

pypi-total-downloads python-ver pypi-release-ver

disnake-ext-music

An easy-to-use music extension for disnake. This is a fork of discord-ext-music with a few changes to support disnake (imports change and some methods), pure credits goes to mansuf as they are the original author of this extension and I have just made a fork of it.

Key features

Installation

Python 3.8 or higher required.

You also can install development version by cloning the repository, see below:

git clone https://github.com/KortaPo/disnake-ext-music.git
cd disnake-ext-music

Optional packages

These are optional packages that you are not required to install it, but you get extra benefit once you install it.

  • scipy and pydub for equalizer support.
  • miniaudio for miniaudio music source support.
  • PyAV for FFmpeg libraries music source support.

What type of audio formats disnake-ext-music can play ?

basically, you can play these formats without additional packages:

  • Raw PCM
  • WAV

with miniaudio, you can play these formats:

  • MP3
  • FLAC
  • All formats that vorbis encoded
  • WAV

with PyAV, you can almost play anything that supported by ffmpeg libraries

What sources that disnake-ext-music can play ?

Without additional packages or with miniaudio you can only play local file.

But, with PyAV you can almost play any sources that supported by ffmpeg protocols

Quick usage

import disnake
from disnake.ext.commands import Bot
from disnake.ext import commands
from disnake.ext.music import MusicClient, WAVAudio, Track

bot = Bot(command_prefix='!', case_insensitive=True, intents=disnake.Intents.all())


@bot.command(name='play', aliases=['p'], description='Plays an audio track')
async def play(ctx: commands.Context):
    voice_user = ctx.message.author.voice
    music_client = await voice_user.channel.connect(cls=MusicClient)
    track = Track(
        WAVAudio('audio.wav'),  # AudioSource
        'This is audio'  # name
    )
    await music_client.play(track)


bot.run('token')

Opus encoder

When you're installing disnake-ext-music, opus encoder already shipped with it (because of disnake audio library). This is called native opus encoder good for compatibility and stability. But, if you want to have much better performance and low CPU usage you can use alternative opus encoder using PyAV library (by installing av package pip install av).

By default, disnake-ext-music auto-detects opus encoder. If you have PyAV installed, it will use PyAV opus encoder, otherwise it will use native encoder.

Alternatively, you can set environment to override opus encoder.

For windows:

# PyAV opus encoder
set OPUS_ENCODER=av

# Native opus encoder
set OPUS_ENCODER=native

For linux / Mac OS:

# PyAV opus encoder
export OPUS_ENCODER=av

# Native opus encoder
export OPUS_ENCODER=native

Notes

Reusable audio sources

Because disnake-ext-music is specialized for music, all audio sources in disnake-ext-music are reusable. So if you call MusicClient.stop(), MusicClient.next_track(), MusicClient.previous_track(), or MusicClient.play_track_from_pos() the audio source will be recreated with same configurations (NOTE: Some audio sources are not reusable, for example: if you pass unseekable stream to RawPCMAudio or WAVAudio it will become non-reusable). And if the audio ended, the audio sources will not cleaned up, it will stay there until you removed it from playlist or reused by the library. Meanwhile, all audio sources in discord.py library are not reusable and cannot be played in MusicClient. But, the audio sources in discord-ext-music can be played in VoiceClient.

disnake-ext-music does not play audio from YouTube or any other sites.

To be clear, disnake-ext-music is just music extension with: playlist integrated with voice client, equalizer (if you install scipy and pydub), audio playback with thread-safe controls, and audio source that play streamable url. If you want to play youtube stream you must install additional packages like youtube-dl to extract streamable url and play it under discord-ext-music library.