Skip to content

ytmusic

YTMusic module for downloading and searching songs.

YouTubeMusic(*args, **kwargs) ¤

Bases: AudioProvider

YouTube Music audio provider class

Arguments¤
  • args: Arguments passed to the AudioProvider class.
  • kwargs: Keyword arguments passed to the AudioProvider class.
Source code in spotdl/providers/audio/ytmusic.py
27
28
29
30
31
32
33
34
35
36
37
38
def __init__(self, *args: Any, **kwargs: Any) -> None:
    """
    Initialize the YouTube Music API

    ### Arguments
    - args: Arguments passed to the `AudioProvider` class.
    - kwargs: Keyword arguments passed to the `AudioProvider` class.
    """

    super().__init__(*args, **kwargs)

    self.client = YTMusic(language="de")

get_results(search_term, **kwargs) ¤

Get results from YouTube Music API and simplify them

Arguments¤
  • search_term: The search term to search for.
  • kwargs: other keyword arguments passed to the YTMusic.search method.
Returns¤
  • A list of simplified results (dicts)
Source code in spotdl/providers/audio/ytmusic.py
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
def get_results(self, search_term: str, **kwargs) -> List[Result]:
    """
    Get results from YouTube Music API and simplify them

    ### Arguments
    - search_term: The search term to search for.
    - kwargs: other keyword arguments passed to the `YTMusic.search` method.

    ### Returns
    - A list of simplified results (dicts)
    """

    search_results = self.client.search(search_term, **kwargs)

    # Simplify results
    results = []
    for result in search_results:
        if (
            result is None
            or result.get("videoId") is None
            or result.get("artists") in [[], None]
        ):
            continue

        isrc_result = ISRC_REGEX.search(search_term)

        results.append(
            Result(
                source=self.name,
                url=(
                    f'https://{"music" if result["resultType"] == "song" else "www"}'
                    f".youtube.com/watch?v={result['videoId']}"
                ),
                verified=result.get("resultType") == "song",
                name=result["title"],
                result_id=result["videoId"],
                author=result["artists"][0]["name"],
                artists=tuple(map(lambda a: a["name"], result["artists"])),
                duration=parse_duration(result.get("duration")),
                isrc_search=isrc_result is not None,
                search_query=search_term,
                explicit=result.get("isExplicit"),
                album=(
                    result.get("album", {}).get("name")
                    if result.get("album")
                    else None
                ),
            )
        )

    return results