-
Notifications
You must be signed in to change notification settings - Fork 0
/
ydlAPI.py
148 lines (136 loc) · 4.67 KB
/
ydlAPI.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import re
import subprocess
def add_global_options(opt_dict):
cmd_str = ['./youtube-dl']
if 'proxy' in opt_dict:
cmd_str.append('--proxy')
cmd_str.append(opt_dict['proxy'])
if 'output_path' in opt_dict:
cmd_str.append('-o')
cmd_str.append(opt_dict['output_path'])
return cmd_str
def get_available_streams(url, opt_dict=None):
if opt_dict is None:
opt_dict = {}
cmd_str = add_global_options(opt_dict)
cmd_str.append('-F')
cmd_str.append(url)
out = subprocess.Popen(cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
stdout, stderr = out.communicate()
if 'ERROR' in str(stdout):
return False, []
outstr = str(stdout).split('\\n')
isOK = False
stream_line_started = False
formats = []
for line in outstr:
if line.startswith('format'):
isOK = True
stream_line_started = True
continue
if stream_line_started:
line_sections = line.split(' ')
if line_sections[0].isnumeric():
formats.append(strjoin(line_sections))
return isOK, formats
def download_video(url,frm,opt_dict=None):
if opt_dict is None:
opt_dict = {}
cmd_str = add_global_options(opt_dict)
cmd_str.append('-f')
cmd_str.append(frm)
cmd_str.append(url)
out = subprocess.Popen(cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
regex = re.compile('\d+(\.\d+)?%')
for stdout_line in iter(out.stdout.readline, ""):
prog = re.sub(' +', ' ', stdout_line).split(' ')
if len(prog) > 1:
if regex.search(prog[1]):
if not prog[1] == '100%':
yield prog[1], prog[3], prog[5], prog[7][:-2]
else:
yield prog[1], prog[3][:-2], '', '00:00'
out.stdout.close()
out.wait()
def get_thumbnail(url,opt_dict=None):
if opt_dict is None:
opt_dict = {}
cmd_str = add_global_options(opt_dict)
cmd_str.append('--write-thumbnail')
cmd_str.append('--skip-download')
cmd_str.append(url)
out = subprocess.Popen(cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
filepath = None
for stdout_line in iter(out.stdout.readline, ""):
prog = re.sub(' +', ' ', stdout_line).split(': ')
if len(prog) > 2:
if os.path.isfile(prog[2][:-1]):
filepath = prog[2][:-1]
out.stdout.close()
out.wait()
return filepath
def get_title(url,opt_dict=None):
if opt_dict is None:
opt_dict = {}
cmd_str = add_global_options(opt_dict)
cmd_str.append('--get-title')
cmd_str.append('--skip-download')
cmd_str.append('-f')
cmd_str.append('worst')
cmd_str.append(url)
out = subprocess.Popen(cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
title = ''
for stdout_line in iter(out.stdout.readline, ""):
title = stdout_line
out.stdout.close()
out.wait()
return title
def read_batchfile(filename):
urls = []
regex = re.compile('^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|v\/)?)([\w\-]+)(\S+)?$')
with open(filename,'r') as bfile:
lines = bfile.readlines()
for line in lines:
if regex.match(line):
urls.append(line[:-1])
return urls
def download_video_batch(url,opt_dict:dict):
cmd_str = ['./youtube-dl']
for key, value in opt_dict.items():
cmd_str.append(key)
cmd_str.append(value)
cmd_str.append(url)
print(cmd_str)
out = subprocess.Popen(cmd_str,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True)
regex = re.compile('\d+(\.\d+)?%')
for stdout_line in iter(out.stdout.readline, ""):
prog = re.sub(' +', ' ', stdout_line).split(' ')
if len(prog) > 1:
if regex.search(prog[1]):
if not prog[1] == '100%':
yield prog[1], prog[3], prog[5], prog[7][:-2]
else:
yield prog[1], prog[3][:-2], '', '00:00'
out.stdout.close()
out.wait()
def strjoin(list_str):
lstnew = []
for lstr in list_str:
if not ( (lstr == '') or (lstr == ',')):
lstnew.append(lstr)
return lstnew