-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
35 lines (31 loc) · 982 Bytes
/
youtube.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
from pytube import YouTube
from pytube.exceptions import RegexMatchError
from tqdm.auto import tqdm
import json
# load the video urls
def get_video_urls(url_file="data/video_urls.json"):
with open(url_file, "r") as f:
return json.load(f)
# download the videos
def download_youtube_mp3(video_urls, output_path):
print(video_urls)
for url in tqdm(video_urls):
try:
yt = YouTube(url)
except RegexMatchError:
print(f"RegexMatchError for '{url}'")
continue
itag = None
files = yt.streams.filter(only_audio=True)
for file in files:
if file.mime_type == 'audio/mp4':
itag = file.itag
break
if itag is None:
print("NO MP3 AUDIO FOUND")
continue
stream = yt.streams.get_by_itag(itag)
stream.download(
output_path=output_path,
filename=f"{yt.streams[0].title}.mp3"
)