-
Notifications
You must be signed in to change notification settings - Fork 7
/
fetch_youtube_mp3.py
90 lines (78 loc) · 2.73 KB
/
fetch_youtube_mp3.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
from yt_dlp import YoutubeDL
from os.path import exists, join, splitext
import os
urls = [
"https://www.youtube.com/watch?v=ZY0DG8rUnCA",
#"https://www.youtube.com/watch?v=0tvebuNmp-I"
# "https://www.youtube.com/watch?v=GGoCBAo9N_g"
# "https://www.youtube.com/watch?v=JN3KPFbWCy8", # Elon Musk / Lex Fridman Round 4
# "https://www.youtube.com/watch?v=DxREm3s1scA", # Elon Musk / Lex Fridman Round 3
# "https://www.youtube.com/watch?v=smK9dgdTl40", # Elon Musk / Lex Fridman Round 2
# "https://www.youtube.com/watch?v=dEv99vxKjVI", # Elon Musk / Lex Fridman Round 1
]
directory = "input"
def fetch_youtube(
url: str,
filetype: str,
directory: str = "downloaded_files"
):
"""
Downloads a specific type of file (video, audio, or muted video)
from the provided YouTube URL.
Args:
url (str): The URL of the YouTube video to be downloaded.
filetype (str): Type of file to download - 'video', 'audio',
or 'muted_video'.
directory (str): The directory to download the file to.
Returns:
str: The filename of the downloaded file.
"""
if directory and not exists(directory):
os.makedirs(directory)
if filetype == 'video':
# Download video with audio
outtmpl = join(directory, '%(title)s.%(ext)s')
ydl_opts = {
'format': 'best',
'outtmpl': outtmpl,
'noplaylist': True,
}
elif filetype == 'audio':
# Download audio only
outtmpl = join(directory, '%(title)s_audio.%(ext)s')
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': outtmpl,
'noplaylist': True,
}
elif filetype == 'mp3_audio':
# Download audio as MP3
outtmpl = join(directory, '%(title)s.%(ext)s')
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': outtmpl,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'noplaylist': True,
}
elif filetype == 'muted_video':
# Download video without audio
outtmpl = join(directory, '%(title)s_mutedvideo.%(ext)s')
ydl_opts = {
'format': 'bestvideo',
'outtmpl': outtmpl,
'noplaylist': True,
}
else:
raise ValueError(
"Invalid filetype. Choose 'video', 'audio', or 'muted_video'."
)
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=True)
downloaded_file = ydl.prepare_filename(info)
return downloaded_file
for url in urls:
audio_file = fetch_youtube(url, 'mp3_audio', directory)