Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,13 +14,12 @@ URL_ALVO = "https://ais-pre-czbrtxxjttcqeqhdn3kw3n-102718744012.us-east5.run.app
|
|
| 14 |
|
| 15 |
OUTPUT_DIR = "/tmp/hls"
|
| 16 |
|
| 17 |
-
WIDTH =
|
| 18 |
-
HEIGHT =
|
| 19 |
|
| 20 |
FPS = 10
|
| 21 |
|
| 22 |
-
VIDEO_BITRATE = "
|
| 23 |
-
AUDIO_BITRATE = "64k"
|
| 24 |
|
| 25 |
PORTA = 7860
|
| 26 |
|
|
@@ -32,22 +31,26 @@ app = Flask(__name__)
|
|
| 32 |
|
| 33 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 34 |
|
|
|
|
| 35 |
browser = None
|
| 36 |
page = None
|
| 37 |
ffmpeg_process = None
|
| 38 |
|
| 39 |
# =====================================================
|
| 40 |
-
#
|
| 41 |
# =====================================================
|
| 42 |
|
| 43 |
def iniciar_navegador():
|
| 44 |
|
|
|
|
| 45 |
global browser
|
| 46 |
global page
|
| 47 |
|
| 48 |
-
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
|
| 51 |
|
| 52 |
headless=True,
|
| 53 |
|
|
@@ -59,7 +62,9 @@ def iniciar_navegador():
|
|
| 59 |
|
| 60 |
"--disable-gpu",
|
| 61 |
|
| 62 |
-
"--autoplay-policy=no-user-gesture-required"
|
|
|
|
|
|
|
| 63 |
|
| 64 |
]
|
| 65 |
|
|
@@ -74,17 +79,35 @@ def iniciar_navegador():
|
|
| 74 |
|
| 75 |
)
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
time.sleep(10)
|
| 80 |
|
|
|
|
| 81 |
try:
|
|
|
|
| 82 |
page.mouse.click(WIDTH // 2, HEIGHT // 2)
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
# =====================================================
|
| 87 |
-
#
|
| 88 |
# =====================================================
|
| 89 |
|
| 90 |
def atualizar_frames():
|
|
@@ -94,16 +117,45 @@ def atualizar_frames():
|
|
| 94 |
try:
|
| 95 |
|
| 96 |
page.screenshot(
|
|
|
|
| 97 |
path="/tmp/frame.jpg",
|
|
|
|
| 98 |
type="jpeg",
|
| 99 |
-
|
|
|
|
|
|
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
time.sleep(1 / FPS)
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
# =====================================================
|
| 108 |
# FFMPEG
|
| 109 |
# =====================================================
|
|
@@ -112,6 +164,8 @@ def iniciar_ffmpeg():
|
|
| 112 |
|
| 113 |
global ffmpeg_process
|
| 114 |
|
|
|
|
|
|
|
| 115 |
ffmpeg_cmd = [
|
| 116 |
|
| 117 |
"ffmpeg",
|
|
@@ -122,17 +176,17 @@ def iniciar_ffmpeg():
|
|
| 122 |
|
| 123 |
"-loop", "1",
|
| 124 |
|
|
|
|
|
|
|
| 125 |
"-i", "/tmp/frame.jpg",
|
| 126 |
|
| 127 |
"-f", "lavfi",
|
| 128 |
|
| 129 |
-
"-i", "anullsrc
|
| 130 |
|
| 131 |
"-shortest",
|
| 132 |
|
| 133 |
-
|
| 134 |
-
f"fps={FPS}",
|
| 135 |
-
|
| 136 |
"-c:v", "libx264",
|
| 137 |
|
| 138 |
"-preset", "ultrafast",
|
|
@@ -143,15 +197,17 @@ def iniciar_ffmpeg():
|
|
| 143 |
|
| 144 |
"-b:v", VIDEO_BITRATE,
|
| 145 |
|
|
|
|
| 146 |
"-c:a", "aac",
|
| 147 |
|
| 148 |
-
"-b:a",
|
| 149 |
|
|
|
|
| 150 |
"-f", "hls",
|
| 151 |
|
| 152 |
"-hls_time", "4",
|
| 153 |
|
| 154 |
-
"-hls_list_size", "
|
| 155 |
|
| 156 |
"-hls_flags",
|
| 157 |
"delete_segments+append_list",
|
|
@@ -161,9 +217,17 @@ def iniciar_ffmpeg():
|
|
| 161 |
]
|
| 162 |
|
| 163 |
ffmpeg_process = subprocess.Popen(
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
|
|
|
|
|
|
|
| 167 |
# =====================================================
|
| 168 |
# MONITOR
|
| 169 |
# =====================================================
|
|
@@ -178,6 +242,8 @@ def monitor():
|
|
| 178 |
|
| 179 |
if ffmpeg_process.poll() is not None:
|
| 180 |
|
|
|
|
|
|
|
| 181 |
iniciar_ffmpeg()
|
| 182 |
|
| 183 |
except:
|
|
@@ -185,6 +251,33 @@ def monitor():
|
|
| 185 |
|
| 186 |
time.sleep(10)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
# =====================================================
|
| 189 |
# ROTAS
|
| 190 |
# =====================================================
|
|
@@ -204,8 +297,11 @@ def home():
|
|
| 204 |
def stream(filename):
|
| 205 |
|
| 206 |
return send_from_directory(
|
|
|
|
| 207 |
OUTPUT_DIR,
|
|
|
|
| 208 |
filename
|
|
|
|
| 209 |
)
|
| 210 |
|
| 211 |
# =====================================================
|
|
@@ -214,21 +310,42 @@ def stream(filename):
|
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
|
|
|
|
|
|
|
| 217 |
iniciar_navegador()
|
| 218 |
|
| 219 |
threading.Thread(
|
|
|
|
| 220 |
target=atualizar_frames,
|
|
|
|
| 221 |
daemon=True
|
|
|
|
| 222 |
).start()
|
| 223 |
|
|
|
|
|
|
|
| 224 |
iniciar_ffmpeg()
|
| 225 |
|
| 226 |
threading.Thread(
|
|
|
|
| 227 |
target=monitor,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
daemon=True
|
|
|
|
| 229 |
).start()
|
| 230 |
|
| 231 |
app.run(
|
|
|
|
| 232 |
host="0.0.0.0",
|
|
|
|
| 233 |
port=PORTA
|
|
|
|
| 234 |
)
|
|
|
|
| 14 |
|
| 15 |
OUTPUT_DIR = "/tmp/hls"
|
| 16 |
|
| 17 |
+
WIDTH = 640
|
| 18 |
+
HEIGHT = 360
|
| 19 |
|
| 20 |
FPS = 10
|
| 21 |
|
| 22 |
+
VIDEO_BITRATE = "350k"
|
|
|
|
| 23 |
|
| 24 |
PORTA = 7860
|
| 25 |
|
|
|
|
| 31 |
|
| 32 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 33 |
|
| 34 |
+
playwright_instance = None
|
| 35 |
browser = None
|
| 36 |
page = None
|
| 37 |
ffmpeg_process = None
|
| 38 |
|
| 39 |
# =====================================================
|
| 40 |
+
# INICIAR NAVEGADOR
|
| 41 |
# =====================================================
|
| 42 |
|
| 43 |
def iniciar_navegador():
|
| 44 |
|
| 45 |
+
global playwright_instance
|
| 46 |
global browser
|
| 47 |
global page
|
| 48 |
|
| 49 |
+
print("INICIANDO PLAYWRIGHT...")
|
| 50 |
|
| 51 |
+
playwright_instance = sync_playwright().start()
|
| 52 |
+
|
| 53 |
+
browser = playwright_instance.chromium.launch(
|
| 54 |
|
| 55 |
headless=True,
|
| 56 |
|
|
|
|
| 62 |
|
| 63 |
"--disable-gpu",
|
| 64 |
|
| 65 |
+
"--autoplay-policy=no-user-gesture-required",
|
| 66 |
+
|
| 67 |
+
"--disable-blink-features=AutomationControlled"
|
| 68 |
|
| 69 |
]
|
| 70 |
|
|
|
|
| 79 |
|
| 80 |
)
|
| 81 |
|
| 82 |
+
print("ABRINDO SITE...")
|
| 83 |
+
|
| 84 |
+
page.goto(
|
| 85 |
+
|
| 86 |
+
URL_ALVO,
|
| 87 |
+
|
| 88 |
+
timeout=60000,
|
| 89 |
+
|
| 90 |
+
wait_until="networkidle"
|
| 91 |
+
|
| 92 |
+
)
|
| 93 |
|
| 94 |
time.sleep(10)
|
| 95 |
|
| 96 |
+
# tenta iniciar vídeo
|
| 97 |
try:
|
| 98 |
+
|
| 99 |
page.mouse.click(WIDTH // 2, HEIGHT // 2)
|
| 100 |
+
|
| 101 |
+
print("CLICK ENVIADO")
|
| 102 |
+
|
| 103 |
+
except Exception as e:
|
| 104 |
+
|
| 105 |
+
print("ERRO CLICK:", e)
|
| 106 |
+
|
| 107 |
+
time.sleep(5)
|
| 108 |
|
| 109 |
# =====================================================
|
| 110 |
+
# CAPTURA FRAME
|
| 111 |
# =====================================================
|
| 112 |
|
| 113 |
def atualizar_frames():
|
|
|
|
| 117 |
try:
|
| 118 |
|
| 119 |
page.screenshot(
|
| 120 |
+
|
| 121 |
path="/tmp/frame.jpg",
|
| 122 |
+
|
| 123 |
type="jpeg",
|
| 124 |
+
|
| 125 |
+
quality=70
|
| 126 |
+
|
| 127 |
)
|
| 128 |
|
| 129 |
+
print("FRAME OK")
|
| 130 |
+
|
| 131 |
+
except Exception as e:
|
| 132 |
+
|
| 133 |
+
print("ERRO FRAME:", e)
|
| 134 |
|
| 135 |
time.sleep(1 / FPS)
|
| 136 |
|
| 137 |
+
# =====================================================
|
| 138 |
+
# ESPERAR PRIMEIRO FRAME
|
| 139 |
+
# =====================================================
|
| 140 |
+
|
| 141 |
+
def esperar_frame():
|
| 142 |
+
|
| 143 |
+
while True:
|
| 144 |
+
|
| 145 |
+
if os.path.exists("/tmp/frame.jpg"):
|
| 146 |
+
|
| 147 |
+
tamanho = os.path.getsize("/tmp/frame.jpg")
|
| 148 |
+
|
| 149 |
+
if tamanho > 1000:
|
| 150 |
+
|
| 151 |
+
print("FRAME DETECTADO")
|
| 152 |
+
|
| 153 |
+
return
|
| 154 |
+
|
| 155 |
+
print("AGUARDANDO FRAME...")
|
| 156 |
+
|
| 157 |
+
time.sleep(2)
|
| 158 |
+
|
| 159 |
# =====================================================
|
| 160 |
# FFMPEG
|
| 161 |
# =====================================================
|
|
|
|
| 164 |
|
| 165 |
global ffmpeg_process
|
| 166 |
|
| 167 |
+
print("INICIANDO FFMPEG...")
|
| 168 |
+
|
| 169 |
ffmpeg_cmd = [
|
| 170 |
|
| 171 |
"ffmpeg",
|
|
|
|
| 176 |
|
| 177 |
"-loop", "1",
|
| 178 |
|
| 179 |
+
"-framerate", str(FPS),
|
| 180 |
+
|
| 181 |
"-i", "/tmp/frame.jpg",
|
| 182 |
|
| 183 |
"-f", "lavfi",
|
| 184 |
|
| 185 |
+
"-i", "anullsrc",
|
| 186 |
|
| 187 |
"-shortest",
|
| 188 |
|
| 189 |
+
# vídeo
|
|
|
|
|
|
|
| 190 |
"-c:v", "libx264",
|
| 191 |
|
| 192 |
"-preset", "ultrafast",
|
|
|
|
| 197 |
|
| 198 |
"-b:v", VIDEO_BITRATE,
|
| 199 |
|
| 200 |
+
# áudio falso
|
| 201 |
"-c:a", "aac",
|
| 202 |
|
| 203 |
+
"-b:a", "64k",
|
| 204 |
|
| 205 |
+
# hls
|
| 206 |
"-f", "hls",
|
| 207 |
|
| 208 |
"-hls_time", "4",
|
| 209 |
|
| 210 |
+
"-hls_list_size", "5",
|
| 211 |
|
| 212 |
"-hls_flags",
|
| 213 |
"delete_segments+append_list",
|
|
|
|
| 217 |
]
|
| 218 |
|
| 219 |
ffmpeg_process = subprocess.Popen(
|
| 220 |
+
|
| 221 |
+
ffmpeg_cmd,
|
| 222 |
+
|
| 223 |
+
stdout=subprocess.PIPE,
|
| 224 |
+
|
| 225 |
+
stderr=subprocess.PIPE
|
| 226 |
+
|
| 227 |
)
|
| 228 |
|
| 229 |
+
print("FFMPEG INICIADO")
|
| 230 |
+
|
| 231 |
# =====================================================
|
| 232 |
# MONITOR
|
| 233 |
# =====================================================
|
|
|
|
| 242 |
|
| 243 |
if ffmpeg_process.poll() is not None:
|
| 244 |
|
| 245 |
+
print("FFMPEG CAIU")
|
| 246 |
+
|
| 247 |
iniciar_ffmpeg()
|
| 248 |
|
| 249 |
except:
|
|
|
|
| 251 |
|
| 252 |
time.sleep(10)
|
| 253 |
|
| 254 |
+
# =====================================================
|
| 255 |
+
# LIMPEZA HLS
|
| 256 |
+
# =====================================================
|
| 257 |
+
|
| 258 |
+
def limpeza():
|
| 259 |
+
|
| 260 |
+
while True:
|
| 261 |
+
|
| 262 |
+
try:
|
| 263 |
+
|
| 264 |
+
for arquivo in os.listdir(OUTPUT_DIR):
|
| 265 |
+
|
| 266 |
+
caminho = os.path.join(OUTPUT_DIR, arquivo)
|
| 267 |
+
|
| 268 |
+
if arquivo.endswith(".ts"):
|
| 269 |
+
|
| 270 |
+
idade = time.time() - os.path.getmtime(caminho)
|
| 271 |
+
|
| 272 |
+
if idade > 60:
|
| 273 |
+
|
| 274 |
+
os.remove(caminho)
|
| 275 |
+
|
| 276 |
+
except:
|
| 277 |
+
pass
|
| 278 |
+
|
| 279 |
+
time.sleep(10)
|
| 280 |
+
|
| 281 |
# =====================================================
|
| 282 |
# ROTAS
|
| 283 |
# =====================================================
|
|
|
|
| 297 |
def stream(filename):
|
| 298 |
|
| 299 |
return send_from_directory(
|
| 300 |
+
|
| 301 |
OUTPUT_DIR,
|
| 302 |
+
|
| 303 |
filename
|
| 304 |
+
|
| 305 |
)
|
| 306 |
|
| 307 |
# =====================================================
|
|
|
|
| 310 |
|
| 311 |
if __name__ == "__main__":
|
| 312 |
|
| 313 |
+
print("INICIANDO SISTEMA")
|
| 314 |
+
|
| 315 |
iniciar_navegador()
|
| 316 |
|
| 317 |
threading.Thread(
|
| 318 |
+
|
| 319 |
target=atualizar_frames,
|
| 320 |
+
|
| 321 |
daemon=True
|
| 322 |
+
|
| 323 |
).start()
|
| 324 |
|
| 325 |
+
esperar_frame()
|
| 326 |
+
|
| 327 |
iniciar_ffmpeg()
|
| 328 |
|
| 329 |
threading.Thread(
|
| 330 |
+
|
| 331 |
target=monitor,
|
| 332 |
+
|
| 333 |
+
daemon=True
|
| 334 |
+
|
| 335 |
+
).start()
|
| 336 |
+
|
| 337 |
+
threading.Thread(
|
| 338 |
+
|
| 339 |
+
target=limpeza,
|
| 340 |
+
|
| 341 |
daemon=True
|
| 342 |
+
|
| 343 |
).start()
|
| 344 |
|
| 345 |
app.run(
|
| 346 |
+
|
| 347 |
host="0.0.0.0",
|
| 348 |
+
|
| 349 |
port=PORTA
|
| 350 |
+
|
| 351 |
)
|