-
Notifications
You must be signed in to change notification settings - Fork 0
/
rwth-opencast-dwnldr.py
189 lines (175 loc) · 6.15 KB
/
rwth-opencast-dwnldr.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
# (c) 2020 - 2021 Moehre2
import sys
import http.client
import os
import shutil
import math
import subprocess
playlist_buffer = []
qualities = []
def get_argvar(argnum, argname, text):
if len(sys.argv) > argnum:
argvar = sys.argv[argnum]
print(argname, ":", argvar)
else:
print(text)
argvar = input(argname + " : ")
return argvar
def get_playlist(guid):
global playlist_buffer
conn = http.client.HTTPSConnection("streaming.rwth-aachen.de", timeout=16)
conn.request("GET", "/rwth/smil:engage-player_" + guid + "_presentation.smil/playlist.m3u8")
req = conn.getresponse()
success = (req.status == 200)
print("Status [" + str(req.status) + "] " + req.reason)
if success:
data = req.read()
playlist_buffer = data.split(b"\n")
conn.close()
return success
def check_folder(folder_name):
if os.path.exists(folder_name):
if os.path.isfile(folder_name):
os.remove(folder_name)
elif os.path.isdir(folder_name):
shutil.rmtree(folder_name)
os.makedirs(folder_name)
else:
os.makedirs(folder_name)
def list_qualities():
global playlist_buffer, qualities
temp = []
for elem in playlist_buffer:
if elem == b"":
pass
elif elem[:17] == b"#EXT-X-STREAM-INF":
temp = elem[18:].lower().split(b",")
elif elem[:1] != b"#":
qualities.append({"terms": temp, "link": elem})
resolutions = []
for q in qualities:
res = 0
for t in q["terms"]:
if t[:11] == b"resolution=":
temp = t[11:].split(b"x")
if len(temp) == 2:
res = int(temp[0]) * int(temp[1])
resolutions.append(res)
minres = min(resolutions)
maxres = max(resolutions)
mini = maxi = -1
for i in range(0, len(resolutions)):
if resolutions[i] == 0:
pass
elif resolutions[i] == minres:
mini = i
elif resolutions[i] == maxres:
maxi = i
qualities[mini]["terms"].append(b"min")
qualities[maxi]["terms"].append(b"max")
for i in range(0, len(qualities)):
line = str(i) + ")"
for t in qualities[i]["terms"]:
line = line + " " + t.decode("utf-8")
print(line)
def get_quality_index(quality):
global qualities
for i in range(0, len(qualities)):
if quality == str(i):
return i
else:
for t in qualities[i]["terms"]:
if quality == t.decode("utf-8"):
return i
return -1
def download_part(guid, name, part):
conn = http.client.HTTPSConnection("streaming.rwth-aachen.de")
conn.request("GET", "/rwth/smil:engage-player_" + guid + "_presentation.smil/" + part)
req = conn.getresponse()
success = (req.status == 200)
if success:
data = req.read()
with open(name + "/" + part, "wb") as filewriter:
filewriter.write(data)
filewriter.close()
conn.close()
return success
def download_elements(guid, name, elements):
parts = []
for elem in elements:
if elem != b"" and elem[:1] != b"#":
parts.append(elem)
progress = 0
parts_len = len(parts)
print("[", end="", flush=True)
for i in range(0, parts_len):
download_part(guid, name, parts[i].decode("utf-8"))
if math.floor(i / parts_len * 33) - progress > 0:
print("=", end="", flush=True)
progress = math.floor(i / parts_len * 33)
print("]")
def download_m3u8(guid, name, element):
conn = http.client.HTTPSConnection("streaming.rwth-aachen.de")
conn.request("GET", "/rwth/smil:engage-player_" + guid + "_presentation.smil/" + element)
req = conn.getresponse()
success = (req.status == 200)
print("Status [" + str(req.status) + "] " + req.reason)
if success:
data = req.read()
with open(name + "/" + element, "wb") as filewriter:
filewriter.write(data)
filewriter.close()
download_elements(guid, name, data.split(b"\n"))
conn.close()
return success
def vlc_convert(vlcpath, name, element):
vlccmd = [vlcpath, "-I", "dummy", "-vv", os.path.join(os.getcwd(), name, element), "--sout=#transcode{vcodec=h264,vb=1024,acodec=mp4a,ab=192,channels=2,deinterlace}:standard{access=file,mux=ts,dst=" + os.path.join(os.getcwd(), name + ".mp4") + "}", "vlc://quit"]
print("Converting the video. This may take some time...")
p = subprocess.Popen(vlccmd)
p.wait()
return p.returncode
def delete_raw_files(name):
if os.path.exists(name) and os.path.isdir(name):
shutil.rmtree(name)
def main():
global qualities
print("rwth-opencast-dwnldr 0.2")
guid = get_argvar(1, "guid", "Please enter the guid of the video.")
if not get_playlist(guid):
exit(2)
name = get_argvar(2, "name", "Please enter a name for the video (without file endings)")
check_folder(name)
list_qualities()
quality = get_argvar(3, "quality", "Please enter one of the qualities")
qindex = get_quality_index(quality)
if qindex < 0:
print("Cannot find the specified quality")
exit(3)
print("Starting download...")
element = qualities[qindex]["link"].decode("utf-8")
if not download_m3u8(guid, name, element):
exit(4)
vlc = get_argvar(4, "vlc", "Do you want to convert the downloaded files with VLC Media Player (Y/n)")
if vlc == "n":
exit(0)
elif vlc != "Y":
print("Unknown option. Aborting.")
exit(5)
vlcpath = get_argvar(5, "vlcpath", "Please enter the VLC path (leave empty to load from path envvar)")
if vlcpath == "":
vlcpath = "vlc"
if vlc_convert(vlcpath, name, element) != 0:
print("Error while converting!")
exit(6)
deleterawfiles = get_argvar(6, "deleterawfiles", "Do you want to delete the raw files and just keep the converted video? (Y/n)")
if deleterawfiles == "n":
exit(0)
elif deleterawfiles != "Y":
print("Unknown option. Aborting.")
exit(6)
delete_raw_files(name)
try:
main()
except KeyboardInterrupt:
print("Error: The execution was interrupted")
exit(1)