Youtube module for downloading and searching songs.
YouTube(output_format='mp3', cookie_file=None, search_query=None, filter_results=True, yt_dlp_args=None)
Bases: AudioProvider
YouTube audio provider class
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.
Source code in spotdl/providers/audio/base.py
72
73
74
75
76
77
78
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 | 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"
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,
}
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.
- args: Unused.
- kwargs: Unused.
Returns
- A list of YouTube results if found, None otherwise.
Source code in spotdl/providers/audio/youtube.py
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
70
71 | 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.
- args: Unused.
- kwargs: Unused.
### Returns
- A list of YouTube results if found, None otherwise.
"""
search_results: Optional[List[PyTube]] = Search(search_term).results
if not search_results:
return []
results = []
for result in search_results:
if result.watch_url:
try:
duration = result.length
except Exception:
duration = 0
try:
views = result.views
except Exception:
views = 0
results.append(
Result(
source=self.name,
url=result.watch_url,
verified=False,
name=result.title,
duration=duration,
author=result.author,
search_query=search_term,
views=views,
result_id=result.video_id,
)
)
return results
|