-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini_stream.py
223 lines (128 loc) · 4.62 KB
/
mini_stream.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
from flask import Flask, render_template
import threading
import requests
import os
import subprocess
import time
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
url = "https://pull-f5-tt03.tiktokcdn.com/game/stream-3287029420113265541_sd.flv?expire=1737322977&sign=6d05569d81460382b3bcc34b3da6c7d9&_webnoredir=1"
twitch_rtmp_url = "rtmp://live-lax.twitch.tv/app/live_1072101235_ztWGwxq7oMHGHkVmsrbqDIGvTV5DW2"
p_thread = None
stream_t1 = None
is_running = False
def miki():
try:
global p_thread
global stream_t1
global is_running
if is_running == False:
# p_thread = subprocess.Popen(["python", "test_thread2.py"])
p_thread = subprocess.Popen(["ffmpeg",
"-i", url,
"-c", "copy",
"-f", "flv",
"-fflags", "nobuffer",
"-flags", "low_delay",
twitch_rtmp_url])
is_running = True
print("streaming started")
except subprocess.CalledProcessError as e:
print(f"Error streaming live stream: {e}")
except KeyboardInterrupt:
print("Stream interrupted by user")
# subprocess.Popen(["sudo", "apt" , "update"])
# subprocess.Popen(["sudo", "apt" , "install", "ffmpeg", "-y"])
# sudo apt update
# sudo apt install ffmpeg -y
def miki_tester():
while True:
time.sleep(300)
if stream_t1 != None:
if stream_t1.is_alive() == True:
try:
response = requests.get('https://flask-zddy.onrender.com')
print(f"Response status code from miki_test: {response.status_code}")
except requests.exceptions.RequestException as e:
print(f"Error: {e}")
stream_t1 = threading.Thread(target=miki)
stream_t1.start()
t2 = threading.Thread(target=miki_tester)
t2.start()
def stream_restarter():
while True:
time.sleep(5)
try:
# p_thread.terminate()
# p_thread.wait()
# print("stoped thread")
# global is_running
# is_running = False
global stream_t1
global is_running
if stream_t1 == None:
try:
p_thread.terminate()
p_thread.wait()
is_running = False
except:
pass
stream_t1 = threading.Thread(target=miki)
stream_t1.start()
# return f"True"
else:
if stream_t1.is_alive() == False:
try:
p_thread.terminate()
p_thread.wait()
# global is_running
is_running = False
except:
pass
print("Stream stopped on its own ")
stream_t1 = threading.Thread(target=miki)
print("Now restarting...")
stream_t1.start()
except:
pass
# restarter_t1 = threading.Thread(target=stream_restarter)
# restarter_t1.start()
# https://flask-ap18.onrender.com/
@app.route('/')
def index():
if stream_t1 == None:
return render_template('app.html', is_streaming="False")
else:
return render_template('app.html', is_streaming=stream_t1.is_alive())
# return f"miki streaming app is {stream_t1.is_alive()}"
@app.route('/start')
def start_stream():
global stream_t1
if stream_t1 == None:
stream_t1 = threading.Thread(target=miki)
stream_t1.start()
return f"True"
else:
if stream_t1.is_alive() == False:
# stream_t1 = threading.Thread(target=miki)
stream_t1.start()
return f"{stream_t1.is_alive()}"
@app.route('/stop')
def stop_stream():
try:
p_thread.terminate()
p_thread.wait()
print("stoped thread")
global is_running
is_running = False
except:
pass
if stream_t1 != None:
return f"{stream_t1.is_alive()}"
else:
return f"False"
if __name__ == '__main__':
# server = Server(app.wsgi_app)
# server.serve(port=5000)
print("server starting...")
app.run(host="0.0.0.0", port=10000)