OptiJuegos commited on
Commit
ffd22a5
verified
1 Parent(s): 34af7a3

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import subprocess
4
+ import random
5
+ from multiprocessing import Process
6
+ import uvicorn
7
+ from fastapi import FastAPI, Response
8
+ import py7zr
9
+
10
+ app = FastAPI()
11
+
12
+ # Carpeta de videos y rutas de salida
13
+ video_folder = "Los Simpsons"
14
+ hls_output = "stream.m3u8"
15
+ hls_segment_prefix = "stream"
16
+ concat_file = "concat.txt"
17
+
18
+ # Funci贸n para descargar y descomprimir el archivo 7z
19
+ def download_and_extract_videos():
20
+ video_file = "Los Simpsons.7z"
21
+ download_path = os.path.join(video_folder, video_file)
22
+ extract_path = os.path.join(video_folder)
23
+ subprocess.run(["wget", "https://huggingface.co/spaces/lozanogamer/lozanogamers/resolve/main/Los%20Simpsons.7z?download=true", "-P", video_folder])
24
+ with py7zr.SevenZipFile(download_path, 'r') as archive:
25
+ archive.extractall(path=extract_path)
26
+ os.remove(download_path)
27
+
28
+ # Funci贸n para generar un archivo de concatenaci贸n de videos
29
+ def generate_concat_file():
30
+ video_files = os.listdir(video_folder)
31
+ with open(concat_file, "w") as f:
32
+ for video_file in video_files:
33
+ if video_file.endswith(".mp4"):
34
+ f.write(f"file '{os.path.join(video_folder, video_file)}'\n")
35
+
36
+ # Comando FFmpeg para transmitir video en HLS
37
+ def build_ffmpeg_command():
38
+ return [
39
+ 'ffmpeg',
40
+ '-re',
41
+ '-f', 'concat',
42
+ '-safe', '0',
43
+ '-i', concat_file,
44
+ '-c:v', 'libx264',
45
+ '-c:a', 'aac',
46
+ '-b:v', '2000k',
47
+ '-b:a', '128k',
48
+ '-f', 'hls',
49
+ '-hls_time', '10',
50
+ '-hls_list_size', '0',
51
+ '-hls_flags', 'delete_segments',
52
+ '-hls_segment_filename', f'{hls_segment_prefix}%d.ts',
53
+ hls_output
54
+ ]
55
+
56
+ def stream_video():
57
+ # Descarga y descomprime el archivo de video
58
+ download_and_extract_videos()
59
+
60
+ # Ejecuta el comando FFmpeg para iniciar la transmisi贸n
61
+ while True:
62
+ generate_concat_file()
63
+ ffmpeg_command = build_ffmpeg_command()
64
+ process = subprocess.Popen(ffmpeg_command)
65
+ process.wait()
66
+
67
+ def start_video_streaming():
68
+ video_process = Process(target=stream_video)
69
+ video_process.start()
70
+
71
+ @app.on_event("startup")
72
+ async def startup_event():
73
+ # Espera activa para verificar que el archivo M3U8 est茅 generado
74
+ while not os.path.exists(hls_output):
75
+ print("Esperando a que se genere el archivo M3U8...")
76
+ time.sleep(1)
77
+ print("Archivo M3U8 generado. Servidor listo para recibir solicitudes.")
78
+
79
+ @app.get("/")
80
+ async def read_root():
81
+ return {"message": "Servidor de Streaming de Videos M3U8 Activo"}
82
+
83
+ @app.get("/stream.m3u8")
84
+ def get_m3u8():
85
+ # Devuelve el archivo M3U8 generado
86
+ with open(hls_output, "rb") as f:
87
+ m3u8_content = f.read()
88
+ return Response(content=m3u8_content, media_type="application/vnd.apple.mpegurl")
89
+
90
+ @app.get("/stream{segment}.ts")
91
+ def get_segment(segment: int):
92
+ # Devuelve los segmentos TS generados
93
+ segment_file = f"{hls_segment_prefix}{segment}.ts"
94
+ if os.path.exists(segment_file):
95
+ with open(segment_file, "rb") as f:
96
+ segment_content = f.read()
97
+ return Response(content=segment_content, media_type="video/mp2t")
98
+ else:
99
+ return Response(status_code=404, content="Segment not found")
100
+
101
+ if __name__ == "__main__":
102
+ start_video_streaming()
103
+ uvicorn.run(app, host="0.0.0.0", port=7860)