-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
executable file
·129 lines (106 loc) · 2.84 KB
/
main.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
import socket
import sys
import datetime
import time
import os
import SimpleHTTPServer
import subprocess
import SocketServer
from threading import Thread
import time
import signal
import sys
import shutil
os.chdir("tcode")
timer=0
done=False
p=False
def scanTuners():
p=subprocess.Popen(["hdhomerun_config","discover"],stdout=subprocess.PIPE)
x=p.communicate()[0]
return x.split("at")[1].rstrip().replace(" ","")
def ffmpeg_codecs():
x=subprocess.Popen(["ffmpeg","-codecs"],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
y=x.communicate()[0]
return y.find("libfdk_aac")
channelComp=""
host=""
def start_ffmpeg():
global channelComp
global p
global host
acodecs=['libfdk_aac', '-ac:a:0', '2', '-vbr', '5']
if ffmpeg_codecs() == -1:
print "Hey. You. Get FFmpeg with libfdk_aac! Your ears will thank you!"
acodecs=["aac","-ac","2","-b:a:0","128k","-strict","-2"]
logfile = open('../ffmpeg_log.txt', 'w')
p=subprocess.Popen(["ffmpeg","-i","http://"+host+":5004/auto/v"+channelComp,"-vcodec","libx264","-preset","veryfast","-acodec"]+acodecs+["-vf","yadif=0:0:0","out.m3u8"],stdout=logfile,stderr=logfile)
def letsgo(chan):
global done
global timer
global p
global channelComp
ch=chan.split("_")
# http://192.168.0.149:5004/auto/v41.1
channelComp=ch[0]+"."+ch[1]
thread2 = Thread(target = start_ffmpeg)
thread2.start()
while done==False: # get 10MB of the file...
elapsed_time = time.time() - timer
print "here "+str(elapsed_time)+" "+str(timer)
if int(elapsed_time) > 20 and timer != 0:
done=True
happening=False
p.kill()
time.sleep(5)
print "over"
import glob
files = glob.glob('./*.ts')
for f in files:
os.remove(f)
os.remove("./out.m3u8")
happening=False
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def end_headers(self):
self.send_header("Access-Control-Allow-Origin", "*")
SimpleHTTPServer.SimpleHTTPRequestHandler.end_headers(self)
def do_GET(self):
global done
global happening
global timer
elapsed_time = time.time() - timer
print elapsed_time
if timer==0 or elapsed_time < 20:
timer=time.time()
else:
print "Stream finished! Cleaning up!"
done=True
happening=False
if self.path.find("?chan="):
if happening==False:
ch=self.path.split("?chan=")
if len(ch)<2:
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
return
print ch
ch=ch[1]
ch=ch.replace("/","")
print ch
try:
os.remove("./out.m3u8")
except:
pass
th = Thread(target = letsgo, args=(ch,))
th.start()
timer=0
done=False
happening=True
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
host=scanTuners()
o=open("ip.txt","w")
o.write(host)
o.close()
#Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
SocketServer.TCPServer.allow_reuse_address=True
httpd = SocketServer.TCPServer(("", 7090), CustomHandler)
httpd.serve_forever()