-
Notifications
You must be signed in to change notification settings - Fork 1
/
m3u8.py
332 lines (301 loc) · 11.7 KB
/
m3u8.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env python
# encoding: utf-8
import requests, sys, os, platform, time
import re
from Crypto.Cipher import AES
import multiprocessing
from retrying import retry
from retrying import RetryError
requests.packages.urllib3.disable_warnings()
class Color:
PURPLE = '\033[95m'
CYAN = '\033[96m'
DARKCYAN = '\033[36m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
END = '\033[0m'
MOVE_UP = '\033[F'
FLUSH_LINE = '\033[K'
@staticmethod
def failed(s):
return Color.colorize(s, Color.BOLD + Color.RED)
@staticmethod
def success(s):
return Color.colorize(s, Color.GREEN)
@staticmethod
def colorize(s, color):
"""Formats the given string with the given color"""
return color + s + Color.END if Color.tty() else s
@staticmethod
def tty():
"""Returns true if running in a real terminal (as opposed to being piped or redirected)"""
return sys.stdout.isatty()
@staticmethod
def moveup():
sys.stdout.write(Color.MOVE_UP)
@staticmethod
def flushline():
sys.stdout.write(Color.FLUSH_LINE)
class M3u8:
'''
This is a main Class, the file contains all documents.
One document contains paragraphs that have several sentences
It loads the original file and converts the original file to new content
Then the new content will be saved by this class
'''
def __init__(self):
'''
Initial the custom file by self
'''
self.encrypt = False
self.encryptKey = ""
self.saveSuffix = "ts"
self.parseSegment = "ts"
self.attributePattern = re.compile(r'''((?:[^,"']|"[^"]*"|'[^']*')+)''')
self.headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36",
"accept": "*/*"
}
def formatter(self):
print("*" * 50)
return self
def checkUrl(self, url):
'''
Determine if it is a available link of m3u8
:return: bool
'''
if len(url) <= 0 :
return False
elif not url.startswith('http'):
return False
else:
return True
def parse(self, url):
'''
Analyze a link of m3u8
:param url: string, the link need to analyze
:return: list
'''
container = list()
response = self.request(url, None).text.split('\n')
for ts in response:
if (".%s" % (self.parseSegment)) in ts:
if self.containsSegment(container, ts):
continue
container.append(ts)
if '#EXT-X-KEY:' in ts:
self.encrypt = True
container.append(ts)
return container
def containsSegment(self, lst: [], url) -> bool:
if lst == None or len(lst) == 0:
return False
return url in lst
def getEncryptKey(self, url, lastSlashUrl, line):
'''
Access to the secret key
:param url: string, Access to the secret key by the url
:return: string
'''
params = self.attributePattern.split(line.replace('#EXT-X-KEY:', ''))[1::2]
keyStr = ''
for param in params:
name, value = param.split('=', 1)
if name == "URI":
quotes = ('"', "'")
if value.startswith(quotes) and value.endswith(quotes):
keyStr = value[1:-1]
url = url if url.endswith("/") else url + '/'
finalUrl = keyStr if keyStr.startswith("http") else "{}{}".format(url, keyStr)
encryptKey = ""
try:
encryptKey = self.request(finalUrl, None).content
except Exception as e:
finalUrl = lastSlashUrl if lastSlashUrl.endswith("/") else lastSlashUrl + '/'
finalUrl = "{}{}".format(finalUrl, keyStr)
encryptKey = self.request(finalUrl, None).content
else:
pass
finally:
pass
return encryptKey
def aesDecode(self, data, key):
'''
Decode the data
:param data: stream, the data need to decode
:param key: secret key
:return: decode the data
'''
crypt = AES.new(key, AES.MODE_CBC, key)
plain_text = crypt.decrypt(data)
return plain_text.rstrip(b'\0')
def download(self, queue, sort, file, downPath, url, failed):
'''
Download the debris of video
:param queue: the queue
:param sort: which number debris
:param file: the link of debris
:param downPath: the path to save debris
:param url: the link of m3u8
:return: None
'''
queue.put(file)
domainUrl = '/'.join(url.split("/")[:3])
if file[:1] == '/':
# root path
baseUrl = domainUrl
baseUrl += file
baseUrl = '/'.join(baseUrl.split("/")[:-1])
else:
baseUrl = '/'.join(url.split("/")[:-1])
if not file.startswith("http"):
if file[:1] == '/':
file = domainUrl + file
else:
file = baseUrl + '/' +file
debrisName = "{}/{}.{}".format(downPath, sort, self.saveSuffix)
offset = 0
if not os.path.exists(debrisName):
try:
response = self.request(file, None)
data = response.content
if self.encrypt:
data = self.aesDecode(response.content, self.encryptKey)
finded, offset = self.findTSOffset(data)
if not finded:
offset = self.skipPNGLength(data)
if offset == 0:
offset = self.skipBMPLength(data)
except (RetryError, requests.exceptions.RequestException) as e:
failed.append(queue.get(file))
return
with open(debrisName, "wb") as f:
f.write(data[offset:])
f.flush()
queue.get(file)
def skipBMPLength(sekf, data: bytes) -> int:
bmpHeaderStart = b'\x42\x4d'
if data[:2] != bmpHeaderStart:
return 0
return int.from_bytes(data[10:14], byteorder='little', signed=False)
def findTSOffset(self, data: bytes) -> (bool, int):
tsHeaderPattern = b'\x47\x40\x11\x10\x00\x42\xF0\x25'
offset = 0
while offset < len(data) - len(tsHeaderPattern):
tmpSlice = data[offset:offset + len(tsHeaderPattern)]
if tmpSlice[0:3] == tsHeaderPattern[0:3] and tmpSlice[4:len(tsHeaderPattern)] == tsHeaderPattern[4:len(tsHeaderPattern)]:
return True, offset
offset += 1
return False, offset
def skipPNGLength(self, data: bytes) -> int:
pngHeaderPattern = b'\x89\x50\x4E\x47\x0D\x0A\x1A\x0A'
pngEndPattern = b'\x00\x00\x00\x00\x49\x45\x4E\x44\xAE\x42\x60\x82'
if data[:8] != pngHeaderPattern:
return 0
offset = 8
while offset < len(data) - len(pngEndPattern):
if data[offset:offset + len(pngEndPattern)] == pngEndPattern:
return offset + len(pngEndPattern)
offset += 1
return offset
def progressBar(self, targets, failed):
total = len(targets)
print('---一共{}个碎片...'.format(total))
finished = 0
while True:
for debrisName in targets:
if os.path.exists(debrisName):
finished += 1
targets.remove(debrisName)
print(Color.success("%d / %d" % (finished, total)) + " " * 5 + Color.failed("failed: %d" % (len(failed))))
if finished + len(failed) < total:
Color.moveup()
if finished + len(failed) == total:
break
def status_code_is_not_success(response):
return response.status_code != 200
@retry(stop_max_attempt_number=5, wait_fixed=2000, retry_on_result=status_code_is_not_success)
def request(self, url, params):
if len(self.needProxy) != 0:
response = requests.get(url, proxies=self.proxy, params=params, headers=self.headers, timeout=10, verify=False)
else:
response = requests.get(url, params=params, headers=self.headers, timeout=10, verify=False)
# assert response.status_code == 200
return response
def mergefiles(self, downPath, savePath, saveName, clearDebris):
sys = platform.system()
if sys == "Windows":
os.system("copy /b {}/*.ts {}/{}.{}".format(downPath, savePath, saveName, self.saveSuffix))
if clearDebris:
os.system("rmdir /s/q {}".format(downPath))
else:
os.system("cat {}/*.{}>{}/{}.{}".format(downPath, self.saveSuffix, savePath, saveName, self.saveSuffix))
if clearDebris:
os.system("rm -rf {}".format(downPath))
def run(self):
'''
program entry, Input basic information
'''
downPath = str(input("碎片的保存路径, 默认./Download:")) or "./Download"
savePath = str(input("视频的保存路径, 默认./Complete:")) or "./Complete"
clearDebris = bool(input("是否清除碎片, 默认False:")) or False
self.parseSegment = str(input("url解析关键字, 默认ts:")) or "ts"
self.saveSuffix = str(input("保存片段格式, 默认ts:")) or "ts"
self.needProxy = str(input("输入代理,若不需要请回车略过:")) or ""
self.proxy = {}
if len(self.needProxy) != 0:
self.proxy = {
'http': self.needProxy,
'https': self.needProxy
}
while True:
url = str(input("请输入合法的m3u8链接:"))
if self.checkUrl(url):
break
# create a not available folder
if not os.path.exists(downPath):
os.mkdir(downPath)
if not os.path.exists(savePath):
os.mkdir(savePath)
# start analyze a link of m3u8
print('---正在分析链接...')
container = self.parse(url)
print('---链接分析成功...')
# run processing to do something
print('---进程开始运行...')
po = multiprocessing.Pool(30)
queue = multiprocessing.Manager().Queue()
targets = multiprocessing.Manager().list() # 本地没有的资源集合
failed = multiprocessing.Manager().list() # 失败的资源集合
size = 0
if self.encrypt:
baseUrl = '/'.join(url.split("/")[:3])
lastSlashUrl = url.rsplit('/', 1)[0]
self.encryptKey = self.getEncryptKey(baseUrl, lastSlashUrl, container[0])
for file in container:
sort = str(size).zfill(5)
debrisName = "{}/{}.{}".format(downPath, sort, self.saveSuffix)
if not os.path.exists(debrisName):
po.apply_async(self.download, args=(queue, sort, file, downPath, url, failed,))
targets.append(debrisName)
size += 1
po.close()
self.progressBar(targets, failed)
print('---进程运行结束...')
if len(failed) > 0:
print('---失败资源...')
print(failed)
return
# handler debris
sys = platform.system()
saveName = time.strftime("%Y%m%d_%H%M%S", time.localtime())
print('---文件合并清除...')
self.mergefiles(downPath, savePath, saveName, clearDebris)
print('---合并清除完成...')
print('---任务下载完成...')
if __name__ == "__main__":
M3u8().formatter().run()