-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmultimpdtool.py
More file actions
418 lines (339 loc) · 13.4 KB
/
multimpdtool.py
File metadata and controls
418 lines (339 loc) · 13.4 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import datetime
import glob
import os
import uuid
from os.path import exists
from subprocess import Popen, PIPE
import ffmpeg
import json
import requests
import xmltodict
import yt_dlp
import yt_dlp.options
from pywidevine import Cdm, PSSH, Device
class color:
PURPLE = '\033[95m'
BLUE = '\033[94m'
CYAN = '\033[96m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
END = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class style:
INFO = '[' + color.GREEN + 'INFO' + color.END + '] '
WARN = '[' + color.YELLOW + 'WARN' + color.END + '] '
ERROR = '[' + color.RED + 'EROR' + color.END + '] '
def format_seconds(
seconds: int
) -> str:
m, seconds = divmod(seconds, 60)
h, m = divmod(m, 60)
return f'{int(h)}h {int(m)}m {int(seconds)}s'
def getPSSH(
mpd_url: str
) -> str | None:
pssh = None
try:
r = requests.get(url=mpd_url)
r.raise_for_status()
xml = xmltodict.parse(r.text)
mpd = json.loads(json.dumps(xml))
periods = mpd['MPD']['Period']
except Exception:
return
try:
if isinstance(periods, list):
for idx, period in enumerate(periods):
if isinstance(period['AdaptationSet'], list):
for ad_set in period['AdaptationSet']:
if ad_set['@mimeType'] == 'video/mp4':
try:
for t in ad_set['ContentProtection']:
if t['@schemeIdUri'].lower() == "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed":
pssh = t["cenc:pssh"]
except Exception:
pass
else:
if period['AdaptationSet']['@mimeType'] == 'video/mp4':
try:
for t in period['AdaptationSet']['ContentProtection']:
if t['@schemeIdUri'].lower() == "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed":
pssh = t["cenc:pssh"]
except Exception:
pass
else:
for ad_set in periods['AdaptationSet']:
if ad_set['@mimeType'] == 'video/mp4':
try:
for t in ad_set['ContentProtection']:
if t['@schemeIdUri'].lower() == "urn:uuid:edef8ba9-79d6-4ace-a3c8-27dcd51d21ed":
pssh = t["cenc:pssh"]
except Exception:
pass
except Exception:
return
return pssh
def queryForMedia():
print(style.INFO + 'Input Encrypted Video/Audio Files:')
video, audio = input(style.INFO + '+ Encrypted Video File: '), input(style.INFO + '+ Encrypted Audio File: ')
if not video and not audio:
print(style.ERROR + "No input files provided.")
exit(-1)
return video, audio
def queryForHeaders():
headers = open("headers.py", "w")
print(style.INFO + 'Input headers/params in Python requests format (type ";;" to END):')
cont = False
for line in iter(input, ';;'):
if 'headers' in line.lower():
cont = True
headers.write(str(line) + "\n")
if not cont:
headers.write("headers = {}" + "\n")
headers.close()
def chooseHeaders():
s = input(style.INFO + 'Input headers (y/n)? ~: ')
if s.lower() == 'y':
return True
else:
return False
def queryForKeys():
keys = []
print(style.INFO + 'Input Decryption Key(s):')
while True:
i = input('')
if not i:
break
keys.append(i)
return keys
def queryForMPD():
mpd = input(style.INFO + 'Input .mpd URL ~: ')
if not mpd:
print(style.ERROR + 'No .mpd URL provided.')
exit(-1)
return mpd
def clearHeaders():
with open("headers.py", "w") as f:
f.write("headers = {}")
def printLogo():
print()
print(color.CYAN + " ___ ___ ____ " + color.PURPLE + "___ __ _____ ______" + color.BLUE + " _______ __")
print(color.CYAN + " /__//__/| /_/_/| " + color.PURPLE + "/__/ /_//____//_____/" + color.BLUE + "/______/| /_/|")
print(color.CYAN + " | \\/ ||_ _| | ||(_)" + color.PURPLE + "| \\/ | __ \\| __ \\" + color.BLUE + "__ __|/_ ____| ||")
print(color.CYAN + " | \\ / |L/ /_| | |L/_/" + color.PURPLE + "| \\ / | |/_) | || | ||" + color.BLUE + "| ||___/\\/___/| ||")
print(color.CYAN + " | |\\/| | |_| | | __| |" + color.PURPLE + "| |\\/| | ___/| ||_| ||" + color.BLUE + "| |/ _ \\// _ \\| ||")
print(color.CYAN + " | || | | |_| | | |_| |" + color.PURPLE + "| || | | || | |/_| ||" + color.BLUE + "| | (_) | (_) | ||")
print(color.CYAN + " |_|/ |_|\\__,_|_|\\__|_|" + color.PURPLE + "|_|/ |_|_|/ |_____// " + color.BLUE + "|_|\\___/ \\___/|_|/" + color.END)
print(" (c) https://github.com/DevLARLEY")
print()
def getKeys(
pssh,
lic_url
) -> list | None:
files = glob.glob('CDM/*.wvd')
device = Device.load(files[0])
cdm = Cdm.from_device(device)
session_id = cdm.open()
challenge = cdm.get_license_challenge(session_id, PSSH(pssh))
response = requests.post(
url=lic_url,
data=challenge
)
if 200 > response.status_code > 299:
print(f"Unable to obtain decryption keys, got error code {response.status_code}: \n{response.text}")
print("License wrapping may be in use.")
return
try:
cdm.parse_license(session_id, response.content)
except Exception:
print(f"Unable to obtain decryption keys: \n{response.text}")
print("License wrapping may be in use.")
return
keys = list(
map(
lambda key: f"{key.kid.hex}:{key.key.hex()}",
filter(
lambda key: key.type == 'CONTENT',
cdm.get_keys(session_id)
)
)
)
cdm.close(session_id)
return keys if keys else None
class Main:
def __init__(self):
self.video_file = None
self.audio_file = None
self.current_media_type = 'Video'
def log(
self,
data: dict
):
done = data.get('status') == 'finished'
name = data.get('filename')
size, progress = 0, 0
if (
(size_estimate := data.get('total_bytes_estimate')) and
(size_downloaded := data.get('downloaded_bytes'))
):
if size_estimate:
size = int(size_estimate / 1000000)
if size_downloaded:
progress = int(size_downloaded / size_estimate * 100)
eta = 'N/A'
if eta_data := data.get('eta'):
eta = format_seconds(eta_data)
frags = f"{data.get('fragment_index', '?')}/{data.get('fragment_count', '?')}"
if speed := data.get('speed', 0):
speed = int(speed / 1000)
if progress <= 25:
percent = f'{color.RED}{round(progress) + 1}%{color.END}'
elif 25 < progress <= 75:
percent = f'{color.YELLOW}{round(progress) + 1}%{color.END}'
elif progress > 75:
percent = f'{color.GREEN}{round(progress) + 1}%{color.END}'
else:
percent = color.RED + '??.?%' + color.END
progress_message = (
f'{style.INFO}Progress ({self.current_media_type}): {percent} (ETA: {eta}, Frags: {frags}, {speed} KB/s, {size} MB)'
)
if name:
if self.current_media_type == 'Video':
self.video_file = name
elif self.current_media_type == 'Audio':
self.audio_file = name
if done:
print()
if progress == 0:
progress_message = ''
self.current_media_type = 'Audio'
print('\r' + progress_message, end="")
# TODO
# + Add support for local .mpd files
# + If decrypting local files, give option to decrypt using PSSH and License URL
def run(self):
printLogo()
reqs = ['mp4decrypt.exe']
for r in reqs:
if not exists(r):
print(style.ERROR + 'Requirement not found in current directory: ' + r)
exit(-1)
mpd = None
pssh = None
source_option = int(input(style.INFO + 'Select Input Type => (1) .mpd File, (2) Video/Audio Files ~: '))
if source_option == 1:
mpd = queryForMPD()
pssh = getPSSH(mpd)
if pssh:
print(style.INFO + "PSSH => " + str(pssh))
else:
pssh = input(style.ERROR + "No PSSH found. Enter manually ~: ")
if pssh == '':
print(style.ERROR + "No PSSH was entered.")
exit(-1)
elif source_option == 2:
self.video_file, self.audio_file = queryForMedia()
if source_option == 1:
decryption_option = int(input(style.INFO + 'Select Decryption Type => (1) License URL, (2) Key(s) ~: '))
if decryption_option == 1:
lic = input(style.INFO + 'Input License URL: ')
if chooseHeaders():
queryForHeaders()
else:
clearHeaders()
keys = getKeys(pssh, lic)
if not keys:
print(style.ERROR + "Unable to extract key(s).")
exit(-1)
elif decryption_option == 2:
keys = queryForKeys()
if not keys:
print(style.ERROR + 'No Keys provided.')
exit(-1)
else:
print(style.ERROR + 'No option was chosen.')
exit(-1)
elif source_option == 2:
keys = queryForKeys()
if not keys:
print(style.ERROR + 'No Keys provided.')
exit(-1)
else:
print(style.ERROR + 'No option was chosen.')
exit(-1)
process_id = str(uuid.uuid4())
output = input(style.INFO + "Enter the output name without the file extension ~: ")
if source_option == 1:
print(style.INFO + 'Downloading .mpd file ...')
ydl_opts = {
'allow_unplayable_formats': True,
'noprogress': True,
'quiet': True,
'fixup': 'never',
'format': 'bv,ba',
'no_warnings': True,
'outtmpl': {'default': process_id + '.f%(format_id)s.%(ext)s'},
'progress_hooks': [self.log]
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download(mpd)
except Exception:
print(style.ERROR + "Unable to download .mpd file.")
exit(-1)
print(style.INFO + "Download successful.")
print(style.INFO + "Decrypting ...")
for media in (self.video_file, self.audio_file):
if not media:
continue
media_type = 'video' if media == self.video_file else 'audio'
extension = os.path.splitext(media)[-1]
output_file = f'{process_id}.{media_type}.{extension}'
command = (
'mp4decrypt.exe',
*sum([['--key', i] for i in keys], []),
media,
output_file
)
process = Popen(command, stdout=PIPE, stderr=PIPE)
_, stderr = process.communicate()
if errors := stderr.decode('utf-8'):
print(style.ERROR + "Failed decrypting " + media + ": " + errors)
exit(-1)
print(style.INFO + "Successfully decrypted " + media + '.')
if media_type == 'video':
self.video_file = output_file
else:
self.audio_file = output_file
if os.path.exists(media):
os.remove(media)
t = datetime.datetime.now()
if not output:
output = (
process_id +
'.{}-{}-{}_{}-{}-{}'.format(t.day, t.month, t.year, t.hour, t.minute, t.second) +
'.mkv'
)
else:
output += '.mkv'
if self.video_file and self.audio_file:
print(style.INFO + "Muxing ...")
video = ffmpeg.input(self.video_file)
audio = ffmpeg.input(self.audio_file)
stream = ffmpeg.output(video, audio, output, vcodec='copy', acodec='copy')
stream = ffmpeg.overwrite_output(stream)
ffmpeg.run(stream, quiet=True)
if os.path.exists(self.video_file):
os.remove(self.video_file)
if os.path.exists(self.audio_file):
os.remove(self.audio_file)
clearHeaders()
print(style.INFO + "Output file => " + output)
if __name__ == '__main__':
try:
main = Main()
main.run()
except KeyboardInterrupt:
exit(-1)