-
Notifications
You must be signed in to change notification settings - Fork 0
/
youtube_download.py
executable file
·221 lines (186 loc) · 6.57 KB
/
youtube_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
70
71
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from subprocess import call
from typing import Union
from pytube import YouTube, Playlist, Stream, StreamQuery
from os import getlogin, remove, makedirs
from os.path import join, exists, dirname
from html import unescape
from werkzeug.utils import secure_filename
from threading import Thread
import click
from frontend import Frontend
user: str = getlogin()
def get_highest_resolution(
streams: StreamQuery, absolutebest: bool
) -> Union[Stream, tuple[Stream]]:
true_best: Stream
if (
streams.get_highest_resolution().resolution
!= (true_best := streams.order_by("resolution").last()).resolution
) and absolutebest:
return (true_best, streams.order_by("abr").last())
return streams.get_highest_resolution()
def download(
video: Union[Stream, tuple[Stream]], path: str, name: str, format: str
) -> str:
if type(video) == tuple:
video_stream: Stream = video[0]
audio_stream: Stream = video[1]
video_name: str = video_stream.download(path, filename=secure_filename(name))
audio_name: str = audio_stream.download(
path, filename="0" + secure_filename(name)
)
call(
[
"ffmpeg-bar",
"-i",
video_name,
"-i",
audio_name,
join(path, "00" + secure_filename(name)) + "." + format,
]
)
remove(video_name)
remove(audio_name)
# YouTube sometimes have videos with th wrong codecs. make_format fixes it
video_dir: str = dirname(video.download(path, filename=secure_filename(name)))
make_format(secure_filename(name), name + "." + format, video_dir)
def make_format(name: str, name_second: str, path: str) -> None:
"""Function that uses ffmpeg to convert a video from one format to another.
name is the name of the file to convert
name_second is the name of the file once converted
path is the directory where both name and name_second files are"""
call(
[
"ffmpeg",
"-i",
join(path, name),
"-loglevel",
"error",
join(path, name_second),
],
)
remove(join(path, name))
def rundownload(
link: Union[str, tuple],
suffix: str = "",
format: str = "mp4",
thread_nb: int = 0,
absolutebest: bool = False,
search: bool = False,
):
"""Actual function for downloading a youtube video
link can be wether a string, which is then interpreted as an url, or a tuple, and it then searches the url matching for search the content of it
suffix is the sub folder, for playlist downloads, for example
format is the format that the file should have"""
try:
# Check if the link is to search or to interpret as an url
# Once we got the good url, we can just call again this function, as it will download the video
if search:
global YoutubeSearch
results: list = YoutubeSearch(link).to_dict()
assert results, "Aucun résultats"
result: dict = results[0]
link = "https://youtube.com/" + result["url_suffix"]
for _ in rundownload(link, format=format):
yield _
return
if link.startswith("https://www.youtube.com/playlist"):
# Here, we get the Playlist object, which contains a list of the videos' URLs
# We then just call the same function to download them
playlist: Playlist = Playlist(link)
yield (
f"[{thread_nb}] Let's download '{playlist.title}{' as ' + format} files !",
"",
)
url: str
for url in playlist.video_urls:
for _ in rundownload(url, unescape(playlist.title), format=format):
yield _
yield (
f"[{thread_nb}] Done downloading '{playlist.title}{' as ' + format} files !",
"",
)
return
if not exists(join(f"/home/{user}/Desktop/YoutubeVideos", suffix)):
makedirs(join(f"/home/{user}/Desktop/YoutubeVideos", suffix))
video: YouTube = YouTube(link)
# Here, we change a little bit the video name for us to be able to download it
name: str = video.title.replace("/", "")
download_video: Union[Stream, tuple[Stream]] = get_highest_resolution(
video.streams, absolutebest
)
yield (
f"[{thread_nb}] Let's download '{video.title}' with a resolution of '{download_video.resolution}'{' as a ' + format} !",
"",
)
download(
download_video,
join(f"/home/{user}/Desktop/YoutubeVideos", suffix),
name,
format,
)
yield (
f"[{thread_nb}] Done downloading '{video.title}' with a resolution of '{download_video.resolution}'{' as a ' + format} !",
"",
)
except Exception as e:
# Showing error (probably temporary, i should make error management)
yield (f"[{thread_nb}] Error " + str(e), "")
return
@click.command()
@click.option(
"--url",
"-u",
"urls",
required=True,
help="Which url to download",
multiple=True,
metavar="URL",
)
@click.option(
"--search/ --nosearch",
"-s/ -n",
"searches",
is_flag=True,
multiple=True,
help="Sets wether the video has to be searched for or not",
)
@click.option(
"--format",
"-f",
help="Sets in which format the video has to be saved",
metavar="FORMAT",
default="mp4",
)
@click.option(
"--absolutebest/ --notabsolutebest",
"-a/ -b",
help="Sets the video to the highest quality possible (if false, it will just get (in the worse cases) an 360p video)",
is_flag=True,
default=False,
)
def maincommand(
urls: tuple[str], searches: tuple[bool], format: str, absolutebest: bool
) -> None:
if len(searches) != len(urls):
print(
"You must have done something wrong, the amount of urls and of search/nosearch flag isn't concordant"
)
exit(1)
if True in searches:
global YoutubeSearch
from youtube_search import YoutubeSearch
thread_nb: int = 0
frontend: function = Frontend(print, rundownload)
url: str
search: bool
for url, search in zip(urls, searches):
url.replace("+", " ")
Thread(
target=frontend, args=(url, "", format, thread_nb, absolutebest, search)
).start()
thread_nb += 1
if __name__ == "__main__":
maincommand()
else:
from youtube_search import YoutubeSearch