A custom YouTube audio downloader for Discord bots
Project description
DiscordMusicLib
A custom YouTube audio downloader library specifically designed for Discord bots. This library provides a simple way to extract audio streams from YouTube videos without relying on external dependencies like yt-dlp.
Features
- ✅ Extract audio streams from YouTube videos
- ✅ Discord bot integration ready
- ✅ Async/await support
- ✅ Multiple extraction methods (webpage and embed)
- ✅ Built-in signature deciphering
- ✅ Search functionality
- ✅ Simple API wrapper
Installation
pip install discordmusiclib
Quick Start
Basic Usage
import asyncio
from discordmusiclib import YouTubeAudioDownloader
async def main():
async with YouTubeAudioDownloader() as downloader:
# Get video info
info = await downloader.extract_video_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
print(f"Title: {info['title']}")
# Get stream URL
stream_url, _ = await downloader.get_audio_stream_url("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
print(f"Stream URL: {stream_url}")
asyncio.run(main())
Discord Bot Integration
import discord
from discord.ext import commands
from discordmusiclib import DiscordMusicBot
class MusicBot(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.music_bot = DiscordMusicBot()
@commands.command(name='play')
async def play_music(self, ctx, *, url):
"""Play music from YouTube URL"""
if not ctx.voice_client:
if ctx.author.voice:
await ctx.author.voice.channel.connect()
else:
await ctx.send("You need to be in a voice channel!")
return
await ctx.send("🎵 Searching...")
# Get audio stream
result = await self.music_bot.play_youtube_audio(url)
if not result['success']:
await ctx.send(f"❌ Error: {result['error']}")
return
# Create audio source
audio_source = discord.FFmpegPCMAudio(
result['stream_url'],
before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'
)
# Play audio
ctx.voice_client.play(audio_source)
embed = discord.Embed(
title="🎵 Now Playing",
description=result['title'],
color=0x00ff00
)
embed.add_field(name="Duration", value=f"{result['duration']} seconds")
embed.add_field(name="Uploader", value=result['uploader'])
await ctx.send(embed=embed)
# Bot setup
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
await bot.add_cog(MusicBot(bot))
bot.run('YOUR_BOT_TOKEN')
Simple Synchronous API
from discordmusiclib import SimpleYouTubeAudio
# Get video info
info = SimpleYouTubeAudio.get_audio_info("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
print(f"Title: {info['title']}")
# Get stream URL
stream_url = SimpleYouTubeAudio.get_stream_url("https://www.youtube.com/watch?v=dQw4w9WgXcQ")
print(f"Stream URL: {stream_url}")
YouTube Search
from discordmusiclib import YouTubeSearcher
async def search_example():
async with YouTubeSearcher() as searcher:
results = await searcher.search_youtube("Rick Astley Never Gonna Give You Up")
for result in results:
print(f"Title: {result['title']}")
print(f"URL: {result['url']}")
print(f"Duration: {result['duration']} seconds")
API Reference
YouTubeAudioDownloader
Main class for extracting YouTube audio streams.
Methods
extract_video_info(url: str) -> Dict: Extract video informationget_audio_stream_url(url: str, prefer_format: str = 'best') -> Tuple[str, Dict]: Get audio stream URL
DiscordMusicBot
Helper class for Discord bot integration.
Methods
play_youtube_audio(url: str) -> Dict: Get YouTube audio stream for Discord playback
SimpleYouTubeAudio
Synchronous wrapper for simple usage.
Methods
get_audio_info(url: str) -> dict: Get video information synchronouslyget_stream_url(url: str) -> str: Get stream URL synchronously
YouTubeSearcher
Search YouTube videos.
Methods
search_youtube(query: str, max_results: int = 5) -> List[Dict]: Search for videos
Requirements
- Python 3.8+
- aiohttp
- discord.py (for Discord bot features)
License
MIT License
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Disclaimer
This library is for educational purposes only. Please respect YouTube's Terms of Service and consider using official APIs when possible.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file discordmusiclib-1.0.0.tar.gz.
File metadata
- Download URL: discordmusiclib-1.0.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92bddb7ab7279fb868e7eac063a3a769f82789a1b0e75e22cd65cd3e60e655b4
|
|
| MD5 |
ab5bf6d1cd0b3e8ae7ba7b3079d72332
|
|
| BLAKE2b-256 |
1570ee0a1f7f158fdea67050534e9163a8637e07b9b26cf00747ec8c4a626615
|
File details
Details for the file discordmusiclib-1.0.0-py3-none-any.whl.
File metadata
- Download URL: discordmusiclib-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
51676b7f6474371a49616cc0dc5ac90094d214e2c137f1ca74e88a7432a6a1fb
|
|
| MD5 |
7b7a77d7aab9acf8dec8fe4fc02449c9
|
|
| BLAKE2b-256 |
06dae4e2124f2f72040aeabb419e99a0ce7aec28b2455a399d11ed99848b5cc0
|