-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathacestream_to_http.py
314 lines (291 loc) · 13.3 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
import SimpleHTTPServer, SocketServer, requests
import os, psutil, time, sys, base64
import hashlib, json
import ConfigParser
import acestream_to_http_tc
from datetime import datetime
Config = ConfigParser.ConfigParser()
Config.read("/home/acestream/.config/acestream-to-http/acestream_to_http.conf")
PORT = Config.get('main', 'port')
SERVER_IP = Config.get('main', 'domain')
USERNAME = Config.get('main', 'username')
PASSWORD = Config.get('main', 'password')
PROTOCOL = Config.get('main', 'protocol')
dir_path = os.path.dirname(os.path.realpath(__file__))+"/www" #change this to where you want to store files. Must have "listings" and "segments" subdirectories writeable by script
temp_stream_saved = False
file_save_status = []
key = ""
streamtimer = 0
saved_file_name = ""
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, saved_file_name
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:
acestream_to_http_tc.startengine()
elif path[2] == 'stop':
acestream_to_http_tc.stopengine()
elif path[2] == 'stopstream':
acestream_to_http_tc.stopenginestream()
time.sleep(5)
self.send_response(302)
self.send_header('Location',"/")
self.end_headers()
elif path[1] == 'transcoding' and acestream_to_http_tc.engine_status() is not None:
if len(path) == 3:
if path[2] == 'start':
if vlcrunning == False:#only start one instance
temp_stream_saved = False
saved_file_name = datetime.today().strftime("%Y%b%d-%H%M")
acestream_to_http_tc.startvlc(saved_file_name)
elif path[2] == 'stop':
acestream_to_http_tc.stopengine()
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":
matchname = f.split("=")[1]
acestream_to_http_tc.stopengine()
acestream_to_http_tc.ffmpeg_transcode(saved_file_name)
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:
acestream_to_http_tc.stopvlc()
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))
out = json.loads(r.text)
out['response']['stream_pid'] = 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(out))
#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
engine_status = acestream_to_http_tc.engine_status()
if enginerunning:
if engine_status is not None:
s = requests.get(engine_status['response']['stat_url'])
engine_response = json.loads(s.text)
disabledtext[0] = "disabled style='opacity: 0.4;'"
acestream_status_text = "Streaming<br />acestream://%s<br />" % engine_status['response']['stream_pid']
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: {0}://{1}/listings/LIVE.m3u8<br />".format(PROTOCOL, 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(\"%s://%s/player.html\", \"\", \"width=660,height=380\");'><i class='fa fa-play-circle'></i>Launch Player</button>" % (disabledtext[0], PROTOCOL, 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">
<button class='button buttonloading' %s onclick='document.getElementById("savefile").submit()'>
<i class='fa fa-save'></i>Save Recording</button>
</form>
</div>
''' % (disabledtext[0]))
# <input type=text name="savefilename" placeholder="Save filename" style="margin-top:20px; margin-bottom:10px;width:300px;" %s><br />
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)
acestream_to_http_tc.stopengine()#start with a clean slate
httpd.serve_forever()
main()