Skip to content

youtube

YouTube module for downloading and searching songs using yt-dlp.

YouTube(output_format='mp3', cookie_file=None, search_query=None, filter_results=True, yt_dlp_args=None) ¤

Bases: AudioProvider

YouTube audio provider class using yt-dlp.

Source code in spotdl/providers/audio/base.py
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
def __init__(
    self,
    output_format: str = "mp3",
    cookie_file: Optional[str] = None,
    search_query: Optional[str] = None,
    filter_results: bool = True,
    yt_dlp_args: Optional[str] = None,
) -> None:
    """
    Base class for audio providers.

    ### Arguments
    - output_directory: The directory to save the downloaded songs to.
    - output_format: The format to save the downloaded songs in.
    - cookie_file: The path to a file containing cookies to be used by YTDL.
    - search_query: The query to use when searching for songs.
    - filter_results: Whether to filter results.
    """

    self.output_format = output_format
    self.cookie_file = cookie_file
    self.search_query = search_query
    self.filter_results = filter_results

    if self.output_format == "m4a":
        ytdl_format = "bestaudio[ext=m4a]/bestaudio/best"
    elif self.output_format == "opus":
        ytdl_format = "bestaudio[ext=webm]/bestaudio/best"
    else:
        ytdl_format = "bestaudio/best"

    yt_dlp_options = {
        "format": ytdl_format,
        "quiet": True,
        "no_warnings": True,
        "encoding": "UTF-8",
        "logger": YTDLLogger(),
        "cookiefile": self.cookie_file,
        "outtmpl": str((get_temp_path() / "%(id)s.%(ext)s").resolve()),
        "retries": 5,
        "extractor_args": {},
    }

    yt_dlp_options.update(get_local_deno_yt_dlp_options())

    if yt_dlp_args:
        yt_dlp_options = args_to_ytdlp_options(
            shlex.split(yt_dlp_args), yt_dlp_options
        )

    self.audio_handler = YoutubeDL(yt_dlp_options)

get_results(search_term, *_args, **_kwargs) ¤

Get results from YouTube

Arguments¤
  • search_term: The search term to search for.
Returns¤
  • A list of YouTube results, or an empty list if no results are found.
Source code in spotdl/providers/audio/youtube.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def get_results(
    self, search_term: str, *_args, **_kwargs
) -> List[Result]:  # pylint: disable=W0221
    """
    Get results from YouTube

    ### Arguments
    - search_term: The search term to search for.

    ### Returns
    - A list of YouTube results, or an empty list if no results are found.
    """
    search_opts: Dict[str, Any] = {
        **self.audio_handler.params,
        "skip_download": True,
    }

    with YoutubeDL(search_opts) as ydl:
        info = ydl.extract_info(f"ytsearch10:{search_term}", download=False)

    if not info or "entries" not in info:
        return []

    results = []
    for entry in info["entries"]:
        if not entry:
            continue

        video_id = entry.get("id")
        if not video_id:
            continue

        results.append(
            Result(
                source=self.name,
                url=f"https://www.youtube.com/watch?v={video_id}",
                verified=False,
                name=entry.get("title", ""),
                duration=entry.get("duration") or 0,
                author=entry.get("uploader") or "",
                search_query=search_term,
                views=entry.get("view_count") or 0,
                result_id=video_id,
            )
        )

    return results