""" YTAudio - YouTube audio for Red-DiscordBot via yt-dlp -> mp3 -> Lavalink (local source). This cog OWNS the shared Red-Lavalink client connection: * On startup it calls ``lavalink.initialize()`` (once the bot is ready), connecting the in-process Lavalink websocket client to the standalone Lavalink node running on localhost:2333 (managed by systemd). * On unload it calls ``lavalink.close()``. Because the Red-Lavalink client is a process-global singleton, exactly ONE cog should own initialize()/close(). Other cogs (e.g. ttstoy) simply call ``lavalink.get_player(guild_id)`` and share this connection. Playback path: yt-dlp resolves metadata (download=False, ~1s), then we run yt-dlp again with ``-o -`` writing the raw bestaudio stream into a pre-created regular file. Once that file has data, Lavalink's ``local`` source plays from it while yt-dlp keeps filling it (its own ffmpeg decodes it live), so audio flows through the shared Lavalink player without touching Lavalink's unreliable youtube/http sources and without transcoding on our side. A regular file is used instead of a FIFO because this Lavalink build's local loader rejects non-regular files. No full download before playback, so tracks start within a few seconds and skipping (killing the yt-dlp process) is instant. If streaming fails it falls back to downloading the audio to mp3 and playing it through the ``local`` source. """ import asyncio import functools import logging import os import shutil import subprocess import sys import time as _time import uuid from typing import Dict, List, Optional import discord import lavalink import yt_dlp from redbot.core import Config, commands from redbot.core.bot import Red from redbot.core.data_manager import cog_data_path log = logging.getLogger("red.scrapyard.ytaudio") # --- Lavalink node connection (must match the standalone application.yml) --- LL_HOST = "localhost" LL_PORT = 2333 LL_PASSWORD = "youshallnotpass" LL_SECURED = False MP3_BITRATE = "192" YTDLP_BASE_OPTS = { "format": "bestaudio/best", "quiet": True, "no_warnings": True, "noplaylist": True, "ignoreerrors": False, "default_search": "ytsearch", "source_address": "0.0.0.0", "socket_timeout": 20, "retries": 2, "postprocessors": [ { "key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": MP3_BITRATE, } ], } # Max seconds to wait for yt-dlp to resolve a stream URL before giving up. RESOLVE_TIMEOUT = 30 # How long to wait for Lavalink to load the growing stream file as a track. STREAM_LOAD_TIMEOUT = 15 # Minimum bytes yt-dlp must have written before we let Lavalink probe/play the file. STREAM_READY_MIN_BYTES = 64 * 1024 class QueuedTrack: """A track queued for a guild, resolved lazily at play time.""" __slots__ = ( "query", "requester", "channel", "title", "stream_url", "stream_ext", "mp3_path", "stream_path", "procs", "skip_evt", "status_msg", ) def __init__(self, query: str, requester: discord.abc.User, channel): self.query = query self.requester = requester self.channel = channel self.title: Optional[str] = None self.stream_url: Optional[str] = None self.stream_ext: Optional[str] = "webm" self.mp3_path: Optional[str] = None self.stream_path: Optional[str] = None self.procs: List[subprocess.Popen] = [] self.skip_evt: Optional[asyncio.Event] = None self.status_msg = None class GuildPlayer: """Per-guild queue + player-loop state. One per guild. The queue model is explicit and always inspectable: * ``pending`` is the list of tracks waiting to play (index 0 is next). * ``current`` is the track playing right now (or None). Commands read these directly, so nowplaying/queue always reflect reality. """ def __init__(self, cog: "YTAudio", guild_id: int): self.cog = cog self.guild_id = guild_id self.pending: List[QueuedTrack] = [] self.current: Optional[QueuedTrack] = None self.task: Optional[asyncio.Task] = None self.next_event = asyncio.Event() # fired when the current track ends self.wakeup = asyncio.Event() # fired when a track is added self.volume = 100 def start(self): if self.task is None or self.task.done(): self.task = asyncio.create_task(self._player_loop()) def enqueue(self, track: "QueuedTrack"): self.pending.append(track) self.wakeup.set() def clear_pending(self): cleared = self.pending self.pending = [] for t in cleared: self.cog._cleanup_file(t) async def _player_loop(self): while True: if not self.pending: self.wakeup.clear() if not self.pending: try: await self.wakeup.wait() except asyncio.CancelledError: return continue track = self.pending.pop(0) self.current = track self.next_event.clear() try: await self.cog._play_track(self.guild_id, track, self.next_event) await self.next_event.wait() except asyncio.CancelledError: self.cog._cleanup_file(track) self.current = None return except Exception: log.exception("Error playing track in guild %s", self.guild_id) finally: self.cog._cleanup_file(track) self.current = None class YTAudio(commands.Cog): """YouTube audio via yt-dlp -> mp3 -> Lavalink.""" def __init__(self, bot: Red): self.bot = bot self.config = Config.get_conf(self, identifier=0x59544155_44494F, force_registration=True) self.config.register_guild(volume=100) self.players: Dict[int, GuildPlayer] = {} self._ll_ready = False self._connect_task: Optional[asyncio.Task] = None self.mp3_dir = cog_data_path(self) / "mp3cache" self.mp3_dir.mkdir(parents=True, exist_ok=True) self.stream_dir = cog_data_path(self) / "streams" self.stream_dir.mkdir(parents=True, exist_ok=True) self._spawned = set() self._ytdlp_opts = dict(YTDLP_BASE_OPTS) self._ytdlp_opts["outtmpl"] = str(self.mp3_dir / "%(id)s.%(ext)s") # ------------------------------------------------------------------ # Lavalink client ownership # ------------------------------------------------------------------ def start_up_task(self): """Called from setup() after add_cog.""" self._connect_task = asyncio.create_task(self._connect_lavalink()) async def _connect_lavalink(self): await self.bot.wait_until_red_ready() # If a node is already connected (e.g. built-in Audio still loaded), # don't double-initialize, just share it. if lavalink.get_all_nodes(): self._ll_ready = True log.info("YTAudio: a Lavalink node is already connected; sharing it.") return max_retries = 5 for attempt in range(1, max_retries + 1): try: await lavalink.initialize( bot=self.bot, host=LL_HOST, password=LL_PASSWORD, port=LL_PORT, timeout=30, secured=LL_SECURED, ) self._ll_ready = True lavalink.register_event_listener(self._track_event_handler) log.info("YTAudio: connected Lavalink client to %s:%s", LL_HOST, LL_PORT) return except lavalink.AbortingNodeConnection: await self._safe_close() log.warning("YTAudio: Lavalink connection aborted.") return except asyncio.TimeoutError: await self._safe_close() log.warning( "YTAudio: Lavalink connect timed out (attempt %s/%s), retrying...", attempt, max_retries, ) await asyncio.sleep(2) except Exception: log.exception("YTAudio: unexpected error connecting to Lavalink.") await self._safe_close() return log.critical("YTAudio: failed to connect to Lavalink node after %s attempts.", max_retries) async def _safe_close(self): try: await lavalink.close(self.bot) except Exception: pass # ------------------------------------------------------------------ # Track end handling # ------------------------------------------------------------------ async def _track_event_handler(self, player, event, extra): if event in ( lavalink.LavalinkEvents.TRACK_END, lavalink.LavalinkEvents.TRACK_EXCEPTION, lavalink.LavalinkEvents.TRACK_STUCK, ): gp = self.players.get(player.guild.id) if gp is not None: gp.next_event.set() # ------------------------------------------------------------------ # yt-dlp resolution (blocking -> executor) with live progress # ------------------------------------------------------------------ def _make_progress_hook(self, loop, track: QueuedTrack): # Runs inside the executor thread. Schedules throttled message edits # back onto the event loop. Discord rate-limits edits, so we edit at # most once per second and only when the whole-number percent changes. state = {"last_edit": 0.0, "last_pct": -1} def hook(d): if track.status_msg is None: return status = d.get("status") if status == "downloading": total = d.get("total_bytes") or d.get("total_bytes_estimate") downloaded = d.get("downloaded_bytes") or 0 if not total: return pct = int(downloaded * 100 / total) now = _time.monotonic() if pct == state["last_pct"] or (now - state["last_edit"]) < 1.0: if pct < 100: return state["last_pct"] = pct state["last_edit"] = now content = "Downloading: {} ({}%)".format(track.query, pct) loop.call_soon_threadsafe( lambda c=content: asyncio.ensure_future(self._safe_edit(track, c)) ) elif status == "finished": content = "Converting to mp3: {}".format(track.query) loop.call_soon_threadsafe( lambda c=content: asyncio.ensure_future(self._safe_edit(track, c)) ) return hook async def _safe_edit(self, track: QueuedTrack, content: str): if track.status_msg is None: return try: await track.status_msg.edit(content=content) except Exception: pass def _blocking_extract(self, query: str, progress_hook=None) -> dict: opts = dict(self._ytdlp_opts) if progress_hook is not None: opts["progress_hooks"] = [progress_hook] with yt_dlp.YoutubeDL(opts) as ydl: info = ydl.extract_info(query, download=True) if "entries" in info: # search/playlist -> first entry info = info["entries"][0] return info def _blocking_extract_stream(self, query: str) -> dict: # download=False: only resolve metadata + the direct media URL. opts = dict(self._ytdlp_opts) opts.pop("postprocessors", None) opts.pop("outtmpl", None) with yt_dlp.YoutubeDL(opts) as ydl: info = ydl.extract_info(query, download=False) if "entries" in info: # search/playlist -> first entry info = info["entries"][0] or {} return info def _skipped(self, track: QueuedTrack) -> bool: return track.skip_evt is not None and track.skip_evt.is_set() async def _run_resolve_or_skip(self, fut, track: QueuedTrack): # Runs a blocking resolver future; returns its result, or None if a # skip was requested (or the resolver exceeded RESOLVE_TIMEOUT). skip_evt = track.skip_evt if self._skipped(track): return None skip_task = None tasks = {fut} if skip_evt is not None: skip_task = asyncio.create_task(skip_evt.wait()) tasks.add(skip_task) done, pending = await asyncio.wait( tasks, timeout=RESOLVE_TIMEOUT, return_when=asyncio.FIRST_COMPLETED ) for t in pending: t.cancel() if skip_task is not None and skip_evt.is_set(): return None if fut not in done: return None return fut.result() async def _resolve_to_stream(self, track: QueuedTrack): loop = asyncio.get_running_loop() fut = loop.run_in_executor( None, functools.partial(self._blocking_extract_stream, track.query) ) info = await self._run_resolve_or_skip(fut, track) if info is None: return None track.title = info.get("title", track.query) url = info.get("url") if not url: raise ValueError("No streamable URL for: {}".format(track.query)) track.stream_url = url track.stream_ext = info.get("ext") or "webm" return track async def _resolve_to_mp3(self, track: QueuedTrack): loop = asyncio.get_running_loop() hook = self._make_progress_hook(loop, track) fut = loop.run_in_executor( None, functools.partial(self._blocking_extract, track.query, hook) ) info = await self._run_resolve_or_skip(fut, track) if info is None: return None track.title = info.get("title", track.query) vid = info.get("id") mp3_path = self.mp3_dir / f"{vid}.mp3" track.mp3_path = str(mp3_path) return track # ------------------------------------------------------------------ # Live streaming: yt-dlp raw stream -> growing file -> Lavalink local source # ------------------------------------------------------------------ @staticmethod def _fetch_target(query: str) -> str: q = query.strip() if q.startswith(("http://", "https://", "www.")): return q return "ytsearch1:" + q def _spawn_stream_pipeline(self, track: QueuedTrack) -> bool: if not track.stream_path: return False ytdlp_cmd = [ sys.executable, "-m", "yt_dlp", "--quiet", "--no-warnings", "--no-playlist", "-f", "bestaudio/best", "-o", "-", self._fetch_target(track.query), ] # Create the regular file ourselves so it exists immediately (Lavalink's # local loader rejects non-regular files like FIFOs) and let yt-dlp stream # its stdout straight into it as the node plays. try: fh = open(track.stream_path, "wb") except OSError: log.exception("YTAudio: could not create stream file for %s", track.query) return False try: proc = subprocess.Popen(ytdlp_cmd, stdout=fh, stderr=subprocess.DEVNULL) except Exception: fh.close() log.exception("YTAudio: failed to spawn yt-dlp stream for %s", track.query) return False fh.close() track.procs = [proc] self._spawned.add(proc) asyncio.get_running_loop().create_task(self._watch_procs(track.procs)) return True async def _wait_stream_ready(self, track: QueuedTrack) -> bool: deadline = _time.monotonic() + STREAM_LOAD_TIMEOUT while _time.monotonic() < deadline: try: if os.path.getsize(track.stream_path) >= STREAM_READY_MIN_BYTES: return True except OSError: pass if track.procs and track.procs[0].poll() is not None: # yt-dlp exited before producing enough data. return False await asyncio.sleep(0.2) return False async def _watch_procs(self, procs): for proc in procs: try: await asyncio.get_running_loop().run_in_executor(None, proc.wait) except Exception: pass for proc in procs: self._spawned.discard(proc) def _cleanup_file(self, track: Optional[QueuedTrack]): if not track: return for proc in track.procs: try: if proc.poll() is None: proc.kill() except Exception: pass track.procs = [] for path in (track.stream_path, track.mp3_path): if path and os.path.exists(path): try: os.remove(path) except OSError: log.warning("Could not delete file: %s", path) # ------------------------------------------------------------------ # Playback # ------------------------------------------------------------------ async def _load_local(self, player, track: QueuedTrack) -> bool: loaded = await asyncio.wait_for( player.load_tracks(track.stream_path or track.mp3_path), timeout=STREAM_LOAD_TIMEOUT, ) ok_types = ( lavalink.enums.LoadType.TRACK_LOADED, lavalink.enums.LoadType.PLAYLIST_LOADED, ) if loaded.has_error or loaded.load_type not in ok_types or not loaded.tracks: return False player.add(requester=track.requester, track=loaded.tracks[0]) if not player.is_playing: await player.play() return True async def _play_stream(self, player, track: QueuedTrack) -> Optional[bool]: # Returns True (playing), False (streaming failed), or None (skipped). if self._skipped(track): return None info_ok = await self._resolve_to_stream(track) if info_ok is None: return None if self._skipped(track): return None track.stream_path = str( self.stream_dir / "stream-{}.{}".format(uuid.uuid4().hex, track.stream_ext) ) if not self._spawn_stream_pipeline(track): return False if not await self._wait_stream_ready(track): log.warning("YTAudio: stream never became ready for %s", track.query) return False if self._skipped(track): return None return await self._load_local(player, track) async def _play_track(self, guild_id: int, track: QueuedTrack, done_event: asyncio.Event): status_msg = None if track.channel is not None: try: status_msg = await track.channel.send("Loading: {}".format(track.query)) except Exception: status_msg = None track.status_msg = status_msg player = lavalink.get_player(guild_id) gp = self.players.get(guild_id) if gp: await player.set_volume(gp.volume) # Fast path: live-stream via yt-dlp -> growing file -> Lavalink local source. result = False try: result = await self._play_stream(player, track) except asyncio.TimeoutError: log.warning("YTAudio: stream load timed out for %s", track.query) except Exception: log.exception("YTAudio: streaming failed for %s", track.query) if result is None: done_event.set() return # Fallback: download + transcode to mp3, play via local source. if not result: await self._safe_edit(track, "Stream unavailable, downloading: {}".format(track.query)) self._cleanup_file(track) try: resolved = await self._resolve_to_mp3(track) if resolved is not None: result = await self._load_local(player, track) except Exception: log.exception("YTAudio: fallback download failed for %s", track.query) if not result: log.error("YTAudio: failed to load track into Lavalink: %s", track.query) await self._safe_edit(track, "Failed to load: {}".format(track.query)) done_event.set() return await self._safe_edit(track, "Now playing: {}".format(track.title or track.query)) def _get_gp(self, guild_id: int) -> GuildPlayer: gp = self.players.get(guild_id) if gp is None: gp = GuildPlayer(self, guild_id) self.players[guild_id] = gp gp.start() return gp async def _ensure_connected(self, ctx: commands.Context) -> bool: if not self._ll_ready and not lavalink.get_all_nodes(): await ctx.send("Lavalink is not connected yet. Try again in a moment.") return False if ctx.author.voice is None or ctx.author.voice.channel is None: await ctx.send("You need to be in a voice channel.") return False target = ctx.author.voice.channel me_voice = ctx.guild.me.voice if me_voice is None or me_voice.channel is None: await lavalink.connect(target) elif me_voice.channel.id != target.id: # Already connected to a different channel: move instead of reconnecting. try: player = lavalink.get_player(ctx.guild.id) await player.move_to(target) except Exception: await lavalink.connect(target) return True # ------------------------------------------------------------------ # Commands # ------------------------------------------------------------------ @commands.command() @commands.guild_only() async def play(self, ctx: commands.Context, *, query: str): """Play a YouTube URL or search query.""" if not await self._ensure_connected(ctx): return gp = self._get_gp(ctx.guild.id) gp.volume = await self.config.guild(ctx.guild).volume() track = QueuedTrack(query, ctx.author, ctx.channel) track.skip_evt = asyncio.Event() already_active = gp.current is not None or bool(gp.pending) gp.enqueue(track) if already_active: position = len(gp.pending) await ctx.send("Added to queue (position {}): {}".format(position, query)) @commands.command() @commands.guild_only() async def skip(self, ctx: commands.Context): """Skip the current track.""" gp = self.players.get(ctx.guild.id) if gp is None or gp.current is None: return await ctx.send("Nothing is playing.") try: player = lavalink.get_player(ctx.guild.id) except Exception: return await ctx.send("Nothing is playing.") # Abort any in-flight resolve/spawn for the current track. if gp.current is not None and gp.current.skip_evt is not None: gp.current.skip_evt.set() # Stopping fires TRACK_END, which the event handler uses to advance # the queue. Do not set next_event here or it would skip two tracks. await player.stop() await ctx.send("Skipped.") @commands.command() @commands.guild_only() async def stop(self, ctx: commands.Context): """Stop playback and clear the queue.""" gp = self.players.get(ctx.guild.id) if gp: if gp.current is not None and gp.current.skip_evt is not None: gp.current.skip_evt.set() gp.clear_pending() gp.next_event.set() try: player = lavalink.get_player(ctx.guild.id) await player.stop() except Exception: pass await ctx.send("Stopped and cleared the queue.") @commands.command() @commands.guild_only() async def pause(self, ctx: commands.Context): """Pause playback.""" try: player = lavalink.get_player(ctx.guild.id) except Exception: return await ctx.send("Nothing is playing.") await player.pause(True) await ctx.send("Paused.") @commands.command() @commands.guild_only() async def resume(self, ctx: commands.Context): """Resume playback.""" try: player = lavalink.get_player(ctx.guild.id) except Exception: return await ctx.send("Nothing is playing.") await player.pause(False) await ctx.send("Resumed.") @commands.command(name="queue", aliases=["q"]) @commands.guild_only() async def queue_cmd(self, ctx: commands.Context): """Show the current track and the queue.""" gp = self.players.get(ctx.guild.id) if not gp or (gp.current is None and not gp.pending): return await ctx.send("The queue is empty.") lines = [] if gp.current: lines.append("Now playing: {}".format(gp.current.title or gp.current.query)) if gp.pending: lines.append("Up next:") for i, t in enumerate(gp.pending, 1): lines.append("{}. {}".format(i, t.title or t.query)) else: lines.append("Queue is empty.") # Keep within Discord message limits. out = "\n".join(lines) if len(out) > 1900: out = out[:1900] + "\n..." await ctx.send(out) @commands.command(name="nowplaying", aliases=["np"]) @commands.guild_only() async def nowplaying(self, ctx: commands.Context): """Show the current track.""" gp = self.players.get(ctx.guild.id) if not gp or gp.current is None: return await ctx.send("Nothing is playing.") await ctx.send("Now playing: {}".format(gp.current.title or gp.current.query)) @commands.command() @commands.guild_only() async def volume(self, ctx: commands.Context, level: int): """Set volume (0-150).""" level = max(0, min(150, level)) await self.config.guild(ctx.guild).volume.set(level) gp = self.players.get(ctx.guild.id) if gp: gp.volume = level try: player = lavalink.get_player(ctx.guild.id) await player.set_volume(level) except Exception: pass await ctx.send("Volume set to {}.".format(level)) @commands.command(name="summon", aliases=["ytjoin"]) @commands.guild_only() async def join(self, ctx: commands.Context): """Summon the bot to your voice channel.""" if await self._ensure_connected(ctx): await ctx.send("Joined your voice channel.") @commands.command(name="ytleave", aliases=["ytdc"]) @commands.guild_only() async def leave(self, ctx: commands.Context): """Leave the voice channel and clear state.""" gp = self.players.get(ctx.guild.id) if gp: if gp.current is not None and gp.current.skip_evt is not None: gp.current.skip_evt.set() gp.clear_pending() gp.next_event.set() try: player = lavalink.get_player(ctx.guild.id) await player.stop() await player.disconnect() except Exception: pass await ctx.send("Left the channel.") # ------------------------------------------------------------------ # Cleanup # ------------------------------------------------------------------ async def cog_unload(self): if self._connect_task: self._connect_task.cancel() for gp in self.players.values(): if gp.task: gp.task.cancel() try: lavalink.unregister_event_listener(self._track_event_handler) except Exception: pass # This cog owns the connection: close it on unload. await self._safe_close() # Kill any in-flight streaming pipelines. for proc in list(self._spawned): try: if proc.poll() is None: proc.kill() except Exception: pass self._spawned.clear() # Sweep leftover mp3s and stream files. for directory in (self.mp3_dir, self.stream_dir): try: shutil.rmtree(directory, ignore_errors=True) directory.mkdir(parents=True, exist_ok=True) except Exception: pass