forked from spiderrabbit/acestream-to-http
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacestream_to_http.py
350 lines (327 loc) · 16.2 KB
/
acestream_to_http.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import SimpleHTTPServer, SocketServer, requests, subprocess
import os, psutil, time, sys, base64
import hashlib, json
from MediaInfo import MediaInfo
PORT = "4523"
SERVER_IP = "127.0.0.1"
USERNAME = "user"
PASSWORD = "acestream"
dir_path = os.path.dirname(os.path.realpath(__file__))+"/www" #change this to where you want to store files. Must have "listings" and "segements" subdirectories writeable by script
temp_stream_saved = False
file_save_status = []
key = ""
streamtimer = 0
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_AUTHHEAD(self):
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm=\"acestream_to_http\"')
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
global temp_stream_saved, file_save_status, key, streamtimer, dir_path
if not os.path.isfile('/tmp/pid_stat_url'):
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(None))
with open('/tmp/pid_stat_url', 'r') as f: pid_stat_url = json.loads(f.read())
path = self.path.split("/")
if path[1] != 'segments':#allow non-password access to live stream segments
if self.headers.getheader('Authorization') == None:
self.do_AUTHHEAD()
pass
elif self.headers.getheader('Authorization') != "Basic %s" % key:
self.do_AUTHHEAD()
pass
if self.headers.getheader('Authorization') != "Basic %s" % key:
return
#check if vlc running
vlcrunning = False
for process in psutil.process_iter():
if '/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline():
vlcrunning = True
vlcrunning_line = process.cmdline()
enginerunning = False
if "acestreamengine" in (p.name() for p in psutil.process_iter()):
enginerunning=True
if path[1] == 'engine':
if len(path) == 3:
if path[2] == 'start' and enginerunning == False:
proc1 = subprocess.Popen(["/snap/bin/acestreamplayer.engine", "--client-console", "--bind-all", "--live-cache-type", "disk", "--live-disk-cache-size", "1000000000"])
elif path[2] == 'stop':
for process in psutil.process_iter():
if process.name() == "acestreamengine" or ('/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline()):
process.kill()
pid_stat_url = None
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
if os.path.isfile(dir_path+"/listings/LIVE.strm"):
os.remove(dir_path+"/listings/LIVE.strm")
elif path[2] == 'stopstream':
for process in psutil.process_iter():
if'/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline():
process.kill()
if pid_stat_url is not None:
#command_url ?method=stop
sr = requests.get(json.loads(pid_stat_url[3])['response']['command_url']+"?method=stop")
#print sr.text
pid_stat_url = None
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
if os.path.isfile(dir_path+"/listings/LIVE.strm"):
os.remove(dir_path+"/listings/LIVE.strm")
time.sleep(5)
self.send_response(302)
self.send_header('Location',"/")
self.end_headers()
elif path[1] == 'transcoding' and pid_stat_url is not None:
if len(path) == 3:
if path[2] == 'start' and len(pid_stat_url)>0:
if vlcrunning == False:#only start one instance
temp_stream_saved = False
f = open(dir_path+"/listings/LIVE.strm", "w")
f.write("http://%s/segments/acestream.m3u8" % (SERVER_IP))
f.close()
subprocess.Popen(["cvlc", "--live-caching", "30000", pid_stat_url[2], "--sout", "#duplicate{dst=std{access=livehttp{seglen=5,delsegs=true,numsegs=20,index="+dir_path+"/segments/acestream.m3u8,index-url=http://"+SERVER_IP+"/segments/stream-########.ts},mux=ts{use-key-frames},dst="+dir_path+"/segments/stream-########.ts},dst=std{access=file,mux=ts,dst='/tmp/acestream.mkv'}}"])
elif path[2] == 'stop' and len(pid_stat_url)>0:
for process in psutil.process_iter(): #kill acestream engine and vlc
if process.name() == "acestreamengine" or ('/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline()):
process.kill()
pid_stat_url = None
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
if os.path.isfile(dir_path+"/listings/LIVE.strm"):
os.remove(dir_path+"/listings/LIVE.strm")
temp_stream_saved = True
time.sleep(5)
self.send_response(302)
self.send_header('Location',"/")
self.end_headers()
elif path[1] == 'savefile' and len(path)==3:
for f in path[2].split("?")[1].split("&"):
if f.split("=")[0] == "savefilename":
savefilename = f.split("=")[1]
for process in psutil.process_iter():
if process.name() == "acestreamengine" or ('/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline()):
process.kill()
#ffmpeg has needed -bsf:a aac_adtstoasc option to fix PES packet size mismatch, Error parsing ADTS frame header errors only for AAC audio
try:
if info.getInfo()['audioCodec']=="AAC":
file_save_status = ["ffmpeg", "-y", "-i", "/tmp/acestream.mkv", "-c:v", "copy", "-c:a", "copy", "-movflags", "faststart", "-bsf:a", "aac_adtstoasc", dir_path+"/listings/"+matchname+".mp4"]
else:
file_save_status = ["ffmpeg", "-y", "-i", "/tmp/acestream.mkv", "-c:v", "copy", "-c:a", "copy", "-movflags", "faststart", dir_path+"/listings/"+matchname+".mp4"]
except:
file_save_status = ["ffmpeg", "-y", "-i", "/tmp/acestream.mkv", "-c:v", "copy", "-c:a", "copy", "-movflags", "faststart", dir_path+"/listings/"+matchname+".mp4"]
subprocess.Popen(file_save_status)
pid_stat_url = None
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
temp_stream_saved = False
self.send_response(302)
self.send_header('Location',"/")
self.end_headers()
elif path[1] == 'openpid' and len(path)==3 and enginerunning:
for process in psutil.process_iter(): #kill any vlc session
if '/usr/bin/vlc' in process.cmdline() and '--live-caching' in process.cmdline():
process.kill()
pid_stat_url = None
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
stream_pid = False
for f in path[2].split("?")[1].split("&"):
if f.split("=")[0] == "pid":
stream_pid = f.split("=")[1]
stream_uid = hashlib.sha1(stream_pid).hexdigest()
if stream_pid:
r = requests.get('http://127.0.0.1:6878/ace/getstream?format=json&sid={0}&id={1}'.format(stream_uid, stream_pid))
pid_stat_url = [stream_pid, json.loads(r.text)['response']['stat_url'], json.loads(r.text)['response']['playback_url'], r.text, stream_pid]
with open('/tmp/pid_stat_url', 'w') as f: f.write(json.dumps(pid_stat_url))
#start stream timer
streamtimer = time.time()
time.sleep(5)
self.send_response(302)
self.send_header('Location',"/")
self.end_headers()
else:
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write("""
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{
font-family: Arial;
font-size:12px;
text-align:center;
}
.button {
background-color: #4CAF50;
border: none;
color: white;
padding: 12px 24px;
font-size: 12px;
cursor: pointer;
width:150px;
text-align:left;
}
.buttonloading {
background-color: #4CAF50;
}
.buttonstopped{
background-color: #AF504C
}
.fa {
margin-left: -12px;
margin-right: 8px;
}
.status{
font-family:Courier;
font-size:80%;
}
.statusdiv{
margin:auto;
text-align:center;
width:400px;
padding-top:10px;
padding-bottom:20px;
font-family: Courier;
font-weight: bold;
}
</style>
</head>
<body>
""")
self.wfile.write("<div>")
disabledtext = ["", ""]
if enginerunning:
disabledtext[0] = "disabled style='opacity: 0.4;'"
engine_status_text = "Running"
else:
disabledtext[1] = "disabled style='opacity: 0.4;'"
engine_status_text = "Not running"
self.wfile.write("""
<button class='button buttonloading' %s onclick='this.style.display=\"none\";document.getElementById(\"button_starting\").style.display=\"\";location.href="/engine/start\"'>
<i class='fa fa-stop-circle'></i>Start Engine</button>
<button id='button_starting' class='button buttonloading' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Starting Engine</button>
""" % disabledtext[0])
self.wfile.write("""
<button class='button buttonstopped' %s onclick='this.style.display=\"none\";document.getElementById(\"button_stopping\").style.display=\"\";location.href="/engine/stop\"'>
<i class='fa fa-play-circle'></i>Stop Engine</button>
<button id='button_stopping' class='button buttonloading' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Stopping Engine</button>
"""% disabledtext[1])
self.wfile.write("</div><div class='statusdiv'>Engine Status: %s</div>" % engine_status_text)
disabledtext = ["", ""]
stream_OK_to_transcode = False
if enginerunning:
if pid_stat_url is not None:
s = requests.get(pid_stat_url[1])
engine_response = json.loads(s.text)
disabledtext[0] = "disabled style='opacity: 0.4;'"
acestream_status_text = "Streaming<br />acestream://%s<br />" % pid_stat_url[0]
if (time.time() - streamtimer) < 30:
acestream_status_text+="Wait 30s for stream to settle<br />"
acestream_status_text+="""
<script>
var timeleft = 30;
var downloadTimer = setInterval(function(){
document.getElementById("progressBar").value = 30 - --timeleft;
if(timeleft <= 0){
clearInterval(downloadTimer);
location.href="/";
}
},1000);
</script>
<progress value="0" max="30" id="progressBar"></progress>
<br />
"""
elif 'response' in engine_response:
if 'speed_down' in engine_response['response']:
if engine_response['response']['status'] == "prebuf":
acestream_status_text+="Buffering"
elif int(engine_response['response']['speed_down']) < 100:
acestream_status_text+="Stream health poor"
elif int(engine_response['response']['speed_down']) < 300:
acestream_status_text+="Stream health average"
stream_OK_to_transcode = True
else:
acestream_status_text+="Stream health OK"
stream_OK_to_transcode = True
if stream_OK_to_transcode:
acestream_status_text+="<br />DL Speed: %s kbps<br />Peers %s" % (engine_response['response']['speed_down'], engine_response['response']['peers'])
else:#reload every 5 seconds if buffering or poor stream health
self.wfile.write("""<script>
setTimeout(function(){
window.location.reload(1);
}, 5000);
</script>""")
else:
disabledtext[1] = "disabled style='opacity: 0.4;'"
acestream_status_text = "No stream"
else:
disabledtext[0] = "disabled style='opacity: 0.4;'"
disabledtext[1] = "disabled style='opacity: 0.4;'"
acestream_status_text = ""
self.wfile.write('<div><form method="GET" action="/openpid/" id="openpid" style="display:inline"><input type=text name="pid" placeholder="Acestream ID" style="margin-bottom:10px;width:300px;" %s><br>' % disabledtext[0])
self.wfile.write("""
<button class='button buttonloading' %s onclick='this.style.display="none"; document.getElementById("button_ace_starting").style.display=""; document.getElementById("openpid").submit()'>
<i class='fa fa-stop-circle'></i>Start Acestream</button>
<button id='button_ace_starting' class='button buttonloading' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Starting Acestream</button></form>
""" % disabledtext[0])
self.wfile.write("""
<button class='button buttonstopped' %s onclick='this.style.display="none";document.getElementById("button_ace_stopping").style.display="";location.href="/engine/stopstream"'>
<i class='fa fa-play-circle'></i>Stop Acestream</button>
<button id="button_ace_stopping" class='button buttonstopped' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Stopping Acestream</button>
""" % disabledtext[1])
self.wfile.write("</div><div class='statusdiv'>Acestream Status: %s</div>" % acestream_status_text)
self.wfile.write("<div>")
disabledtext = ["", ""]
if stream_OK_to_transcode:
if vlcrunning:
disabledtext[0] = "disabled style='opacity: 0.4;'"
transcode_status_text = """
Transcoding<br />Stream: http://%s/segments/acestream.m3u8<br />
Kodi: http://%s/listings/LIVE.strm<br />
""" % (SERVER_IP, SERVER_IP)
else:
disabledtext[1] = "disabled style='opacity: 0.4;'"
transcode_status_text = "Not Transcoding"
else:
disabledtext[0] = "disabled style='opacity: 0.4;'"
disabledtext[1] = "disabled style='opacity: 0.4;'"
transcode_status_text = ""
self.wfile.write("""
<button class='button buttonloading' %s onclick='this.style.display=\"none\";document.getElementById(\"transcode_starting\").style.display=\"\";location.href="/transcoding/start\"'>
<i class='fa fa-stop-circle'></i>Start Transcode</button>
<button id='transcode_starting' class='button buttonloading' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Starting Transcode</button>
""" % disabledtext[0])
self.wfile.write("""
<button class='button buttonstopped' %s onclick='this.style.display=\"none\";document.getElementById(\"transcode_stopping\").style.display=\"\";location.href="/transcoding/stop\"'>
<i class='fa fa-play-circle'></i>Stop Transcode</button>
<button id='transcode_stopping' class='button buttonloading' style='display:none'>
<i class='fa fa-spinner fa-spin'></i>Stopping Trancode</button>
"""% disabledtext[1])
self.wfile.write("</div><div class='statusdiv'>Transcode Status: %s</div>" % transcode_status_text)
if vlcrunning: disabledtext[0] = ""
else: disabledtext[0] = "disabled style='opacity: 0.4;'"
self.wfile.write("<button class='button buttonloading' %s onclick='window.open(\"http://%s/player.html\", \"\", \"width=660,height=380\");'><i class='fa fa-play-circle'></i>Launch Player</button>" % (disabledtext[0], SERVER_IP))
if not temp_stream_saved: disabledtext[0] = "disabled style='opacity: 0.4;'"
else: disabledtext[0] = ""
self.wfile.write('''
<div>
<form method="GET" action="/savefile/" id="savefile">
<input type=text name="savefilename" placeholder="Save filename" style="margin-top:20px; margin-bottom:10px;width:300px;" %s><br />
<button class='button buttonloading' %s onclick='document.getElementById("savefile").submit()'>
<i class='fa fa-save'></i>Save Recording</button>
</form>
</div>
''' % (disabledtext[0], disabledtext[0]))
def main():
global key, USERNAME, PASSWORD
key = base64.b64encode("%s:%s" % (USERNAME, PASSWORD))
SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(("", int(PORT)), Handler)
print "Running on http://%s:%s" % (SERVER_IP, PORT)
httpd.serve_forever()
main()