Skip to content

Instantly share code, notes, and snippets.

@Saransh-cpp
Created June 10, 2022 07:37
Show Gist options
  • Save Saransh-cpp/1c2ad4f2761dcab179e7a4b54b33e4b6 to your computer and use it in GitHub Desktop.
Save Saransh-cpp/1c2ad4f2761dcab179e7a4b54b33e4b6 to your computer and use it in GitHub Desktop.
Download multiple songs from YouTube in one go
import googleapiclient.discovery
from creds import API_KEY
import youtube_dl
import os
# API information
api_service_name = "youtube"
api_version = "v3"
DEVELOPER_KEY = API_KEY
"""
Add a list of songs here
"""
# songs = [
# "As it was Harry Styles",
# "Sax money feelings die Lykke Li",
# "Softcore the neighbourhood",
# "pasoori Shae Gill, Ali Sethi",
# "Fourth of July sufjan stevens",
# "going out role model",
# "hell or flying jeremy zucker",
# "my gospel charlie puth",
# "should have known better sufjan stevens",
# "formula labrinth",
# "firefly jeremy zucker",
# "billie bossa nova billie eilish",
# "little dark age",
# "futile devices sufjan stevens",
# "ghosts jeremy zucker",
# "dandelions ruth b." "sweater weather the neighbourhood",
# "cold (feat. future) maroon 5",
# "getting older billie eilish",
# "feels calvin harris, pharrel williams",
# "eastside benny blanco, halsey, khalid",
# "into you ariana grande",
# "bellyache billie eilish",
# "brookly boy jeremy zucker",
# "team lorde",
# "grapejuice Harry Styles",
# "what a time julia michaes niall horan",
# "dark red steve lacy",
# "eyes off you prettymuch",
# "lahu munh lag gaya shail hada",
# ]
songs = [
"i wanted to leave syml",
"only ry x",
"all for us labrinth zendaya",
"lovel khalid billie",
"time hans zimmer",
"always, I'll care jeremy zucker",
"choomantar sohail sen benny dayal",
"no time to die billie eilish",
"cornfield chase hans zimmer",
"fantasy bazzi",
"burning pile mother mother",
"je te laisserai des mots patrick watson",
"sex money feelings die Lykke Li",
"something about you eyedress dent may",
"monster shawn mendes",
"lost with you patrick watson",
"until i found you stephen sanchez",
"nda billie eilish",
"better khalid",
"claudia finneas",
"day one hans zimmer",
"everything I wanted billie eilish",
"brooks jeremy zucker",
"drugs upsahl",
"daddy issues the neighbourhood",
"genius sia diplo labrinth lsd",
"oh mexico jeremy zucker",
"striptease carwash",
"stargazing the neighbourhood",
"light it up - remix major lazer nyla fuse odg",
]
def extract_url(songs):
youtube = googleapiclient.discovery.build(
api_service_name, api_version, developerKey=DEVELOPER_KEY
)
ret_dict = {}
for song in songs:
request = youtube.search().list(part="id", type="video", q=song, maxResults=1)
response = request.execute()
url = f"https://www.youtube.com/watch?v={response['items'][0]['id']['videoId']}"
ret_dict[song] = url
return ret_dict
def download_songs(songs_name_and_url):
print()
for k, v in songs_name_and_url.items():
print(k)
print()
video_url = v
video_info = youtube_dl.YoutubeDL().extract_info(url=video_url, download=False)
filename = f"{video_info['title']}.mp3"
options = {
"format": "bestaudio/best",
"keepvideo": False,
"outtmpl": filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info["webpage_url"]])
print("Download complete... {}".format(k))
print()
# songs_name_and_url = {
# "As it was Harry Styles": "https://www.youtube.com/watch?v=H5v3kku4y6Q",
# "Sax money feelings die Lykke Li": "https://www.youtube.com/watch?v=BFD9Chg2Nmw",
# "Softcore the neighbourhood": "https://www.youtube.com/watch?v=ggG9ySCChYw",
# "pasoori Shae Gill, Ali Sethi": "https://www.youtube.com/watch?v=5Eqb_-j3FDA",
# "Fourth of July sufjan stevens": "https://www.youtube.com/watch?v=JTeKpWp8Psw",
# "going out role model": "https://www.youtube.com/watch?v=pEJ1nNCcY5A",
# "hell or flying jeremy zucker": "https://www.youtube.com/watch?v=JCs0OBS_sPI",
# "my gospel charlie puth": "https://www.youtube.com/watch?v=ADcVtF2cTWc",
# "should have known better sufjan stevens": "https://www.youtube.com/watch?v=lJJT00wqlOo",
# "formula labrinth": "https://www.youtube.com/watch?v=N7EKS2aW_oc",
# "firefly jeremy zucker": "https://www.youtube.com/watch?v=1XLo0bDwQZY",
# "billie bossa nova billie eilish": "https://www.youtube.com/watch?v=4tZ969oc-yI",
# "little dark age": "https://www.youtube.com/watch?v=rtL5oMyBHPs",
# "futile devices sufjan stevens": "https://www.youtube.com/watch?v=8foWYFO5eLA",
# "ghosts jeremy zucker": "https://www.youtube.com/watch?v=Ua6SVINecqU",
# "dandelions ruth b.sweater weather the neighbourhood": "https://www.youtube.com/watch?v=MckM6SFP94c",
# "cold (feat. future) maroon 5": "https://www.youtube.com/watch?v=XatXy6ZhKZw",
# "getting older billie eilish": "https://www.youtube.com/watch?v=7AS9r_E0PY4",
# "feels calvin harris, pharrel williams": "https://www.youtube.com/watch?v=ozv4q2ov3Mk",
# "eastside benny blanco, halsey, khalid": "https://www.youtube.com/watch?v=56WBK4ZK_cw",
# "into you ariana grande": "https://www.youtube.com/watch?v=1ekZEVeXwek",
# "bellyache billie eilish": "https://www.youtube.com/watch?v=gBRi6aZJGj4",
# "brookly boy jeremy zucker": "https://www.youtube.com/watch?v=Hp3e7ZB182g",
# "team lorde": "https://www.youtube.com/watch?v=f2JuxM-snGc",
# "grapejuice Harry Styles": "https://www.youtube.com/watch?v=95pX92cUwV8",
# "what a time julia michaes niall horan": "https://www.youtube.com/watch?v=bPYefBD1Rzs",
# "dark red steve lacy": "https://www.youtube.com/watch?v=xRzbLQ_WKPs",
# "eyes off you prettymuch": "https://www.youtube.com/watch?v=NWA_1lXzGnU",
# "lahu munh lag gaya shail hada": "https://www.youtube.com/watch?v=szVW_w-W8cQ",
# }
# songs_name_and_url = {
# "i wanted to leave syml": "https://www.youtube.com/watch?v=jVoyuNsAcg8",
# "only ry x": "https://www.youtube.com/watch?v=bbokXheXhxY",
# "all for us labrinth zendaya": "https://www.youtube.com/watch?v=k-9DOwrLdkg",
# "lovel khalid billie": "https://www.youtube.com/watch?v=V1Pl8CzNzCw",
# "time hans zimmer": "https://www.youtube.com/watch?v=RxabLA7UQ9k",
# "always, I'll care jeremy zucker": "https://www.youtube.com/watch?v=N6SQ9QoSjCI",
# "choomantar sohail sen benny dayal": "https://www.youtube.com/watch?v=F_jIFDLu30Y",
# "no time to die billie eilish": "https://www.youtube.com/watch?v=BboMpayJomw",
# "cornfield chase hans zimmer": "https://www.youtube.com/watch?v=zE-qxNVoUSo",
# "fantasy bazzi": "https://www.youtube.com/watch?v=yvw-7dEonQI",
# "burning pile mother mother": "https://www.youtube.com/watch?v=asRCJ5F_1zM",
# "je te laisserai des mots patrick watson": "https://www.youtube.com/watch?v=mcdO9UP0hp8",
# "sex money feelings die Lykke Li": "https://www.youtube.com/watch?v=BFD9Chg2Nmw",
# "something about you eyedress dent may": "https://www.youtube.com/watch?v=0VAAS9xnS5U",
# "monster shawn mendes": "https://www.youtube.com/watch?v=MPbUaIZAaeA",
# "lost with you patrick watson": "https://www.youtube.com/watch?v=EwqNIMlmkzs",
# "until i found you stephen sanchez": "https://www.youtube.com/watch?v=MlThQTo6D8A",
# "nda billie eilish": "https://www.youtube.com/watch?v=OORBa32WFcM",
# "better khalid": "https://www.youtube.com/watch?v=x3bfa3DZ8JM",
# "claudia finneas": "https://www.youtube.com/watch?v=Gt37rGlVU3w",
# "day one hans zimmer": "https://www.youtube.com/watch?v=DITy3T2BgbI",
# "everything I wanted billie eilish": "https://www.youtube.com/watch?v=EgBJmlPo8Xw",
# "brooks jeremy zucker": "https://www.youtube.com/watch?v=6y1Qr-Z4rx0",
# "drugs upsahl": "https://www.youtube.com/watch?v=6_LNMiSqpMU",
# "daddy issues the neighbourhood": "https://www.youtube.com/watch?v=vnLAa6_hB9A",
# "genius sia diplo labrinth lsd": "https://www.youtube.com/watch?v=HhoATZ1Imtw",
# "oh mexico jeremy zucker": "https://www.youtube.com/watch?v=-uxeu0MbNR0",
# "striptease carwash": "https://www.youtube.com/watch?v=-uQROmIOSe8",
# "stargazing the neighbourhood": "https://www.youtube.com/watch?v=8giBPUpzKRw",
# "light it up - remix major lazer nyla fuse odg": "https://www.youtube.com/watch?v=r2LpOUwca94",
# }
"""
comment this once this dict is printed and add the dict to code above (to save API usage)
"""
songs_name_and_url = extract_url(songs)
print(songs_name_and_url)
download_songs(songs_name_and_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment