-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyoutubeMp3.py
47 lines (38 loc) · 1.14 KB
/
youtubeMp3.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
from youtubesearchpython import VideosSearch
import json
import youtube_dl
import os
os.chdir('') #give specific location u need
def youtube(name, link):
video_info = youtube_dl.YoutubeDL().extract_info(url = link,download=False)
filename = f"{video_info['title']}"
options={
'format':'bestaudio/best',
'keepvideo':False,
'outtmpl':filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info['webpage_url']])
return filename
file1 = open('export.txt', 'r',encoding='utf8')
count = 0
error = []
for line in file1:
count += 1
name = line.strip()
videosSearch = VideosSearch(f'{name}', limit = 2)
feed = json.dumps(videosSearch.result())
search = json.loads(feed)
try:
link = search["result"][0]["link"]
except IndexError:
pass
try:
filename = youtube(name, link)
except youtube_dl.utils.DownloadError:
error.append(name)
os.remove(filename)
print(count)
pass
print(count, name, f"\t Download Complete {name} ----> {filename}" )
file1.close()