Music Library - YouTube Manager¶
File: cogs/music_lib/youtube.py
The YouTubeManager is the sole interface for all interactions with YouTube. It handles searching for videos, downloading audio, and retrieving video metadata.
YouTubeManager Class¶
__init__(self, time_limit=1800)¶
Initializes the manager.
- Parameters:
time_limit(int): A limit in seconds for the duration of downloadable videos (defaults to 1800s / 30 minutes).
- Process: It loads FFmpeg and
yt-dlpconfigurations from the mainsettings.jsonfile.
create(cls, ...)¶
An asynchronous class method for creating an instance of the manager. It also performs a crucial check to ensure that FFmpeg is installed and accessible on the system.
Key Methods¶
async search_videos(self, query, ...)¶
Searches YouTube for videos matching a given query.
- Process: It uses the
youtube_searchlibrary to perform the search and then processes the results to ensure they are in a consistent format, including converting duration strings (e.g., "3:21") into seconds. - Returns: A list of dictionaries, with each dictionary containing metadata for a video.
async download_audio(self, url, folder, ...)¶
Downloads the audio from a given YouTube URL and saves it as an MP3 file.
- Process: This is a complex method that uses the
yt-dlplibrary.- It first extracts the video's metadata without downloading to check if it's a live stream or if its duration exceeds the
time_limit. - If it's a live stream, it returns the stream URL without downloading.
- If it's a standard video, it proceeds to download the best available audio stream.
- It uses FFmpeg (via
yt-dlp's postprocessor settings) to convert the audio to MP3. - It includes a retry mechanism for the download process.
- It first extracts the video's metadata without downloading to check if it's a live stream or if its duration exceeds the
- Returns: A tuple
(video_info, error).video_infois a dictionary containing the song's metadata, including the localfile_path.erroris a string if the download failed.
async get_video_info_without_download(self, url, ...)¶
A lighter version of download_audio that only fetches the video's metadata without downloading the audio file. This is used when adding songs to the queue while another song is already playing to avoid unnecessary downloads.
async download_playlist(self, url, folder, ...)¶
Handles the downloading of an entire YouTube playlist.
- Process:
- It uses
yt-dlpto extract the information for all videos in the playlist. - To provide a fast response, it immediately downloads the first song of the playlist.
- It returns the metadata for the first song (including its
file_path) along with a list of metadata for all other songs in the playlist, which will be downloaded later as they move up the queue.
- It uses
async get_related_videos(self, video_id, ...)¶
Provides the logic for the autoplay feature. It uses a multi-layered strategy to find relevant songs:
1. First, it tries searching for other songs by the same artist/channel.
2. If that yields few results, it searches using a "cleaned" version of the original song's title (removing terms like "official video", "lyrics", etc.).
3. As a final fallback, it uses yt-dlp's "up next" feature to get YouTube's own recommendations.