-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyoutube.py
41 lines (36 loc) · 1.19 KB
/
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
36
37
38
39
40
41
import youtube_dl
def get_youtube_info(url):
yt_opt = {
'no_warnings':True,
'ignoreerrors':True,
'restrict_filenames':True,
'dumpsinglejson':True,
'format':'best[protocol=https]/best[protocol=http]/bestvideo[protocol=https]/bestvideo[protocol=http]'
}
ydl = youtube_dl.YoutubeDL(yt_opt)
with ydl:
result = ydl.extract_info(
url,
download=False # We just want to extract the info
)
return result
def filter_formats(formats):
filter = []
for f in formats:
try:
if '(DASH video)' in f['format']: continue
if f['format_id'] == '136' or f['format_id'] == '135' or f['format_id'] == '134':
if f['filesize']:
filter.append(f)
except:pass
return filter
def getVideoData(url):
try:
videoinfo = get_youtube_info(url)
formats = filter_formats(videoinfo['formats'])
format = formats[-1]
videoname = videoinfo['title'] + '.' + format['ext']
url = format['url']
return {'name':videoname,'url':url}
except:pass
return None