-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_py.py
66 lines (49 loc) · 1.38 KB
/
test_py.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
#!/usr/bin/env python
# coding: utf-8
import json
import youtube_dl
class MyLogger(object):
def __init__(self):
self.verbose = 0
def debug(self, msg):
if (self.verbose):
print(msg)
def warning(self, msg):
#if (self.verbose):
print(msg)
def error(self, msg):
print(msg)
def my_hook1(d):
if d['status'] == 'finished':
print('Done downloading')
elif d['status'] == 'downloading':
speed = d['downloaded_bytes'] / d['elapsed']
print(f'speed avg {speed:10}')
url = 'http://www.youtube.com/watch?v=BaW_jenozKc'
#'outtmpl': '%(id)s.%(ext)s'
# vcodec=vp9
options = {
'format': 'bestvideo[height=720][vcodec^=avc1]',
'outtmpl': 'v',
'nopart': True,
'continuedl': True,
'logger': MyLogger(),
'progress_hooks': [my_hook1],
}
if __name__ == "__main__":
with youtube_dl.YoutubeDL(options) as ydl:
result = ydl.extract_info(url, download=True)
if 'entries' in result:
# Can be a playlist or a list of videos
video = result['entries'][0]
else:
# Just a video
video = result
print("type(result)", type(video))
print("type(video)", type(video))
#j = json.loads(result)
oname = 'py-o2.json'
with open(oname, 'w') as f:
json.dump(result, f, indent=4, sort_keys=False)
#video_url = video['url']
#print(video_url)