Update app.py
Browse files
app.py
CHANGED
|
@@ -16,25 +16,21 @@ app.add_middleware(
|
|
| 16 |
allow_methods=["*"],
|
| 17 |
)
|
| 18 |
|
| 19 |
-
# Carpeta base para todos los streams
|
| 20 |
BASE_HLS = "/tmp/streams"
|
| 21 |
os.makedirs(BASE_HLS, exist_ok=True)
|
| 22 |
app.mount("/hls", StaticFiles(directory=BASE_HLS), name="hls")
|
| 23 |
|
| 24 |
@app.get("/iniciar-transmision")
|
| 25 |
def iniciar_transmision(url: str):
|
| 26 |
-
# Crear un identificador único para este video (así no chocan si hay varios usuarios)
|
| 27 |
hash_video = hashlib.md5(url.encode()).hexdigest()
|
| 28 |
dir_video = f"{BASE_HLS}/{hash_video}"
|
| 29 |
-
|
| 30 |
-
# Si el archivo maestro ya existe, no volvemos a procesar, servimos directo al instante
|
| 31 |
master_file = f"{dir_video}/master.m3u8"
|
|
|
|
| 32 |
if os.path.exists(master_file):
|
| 33 |
return {"url_reproductor": f"https://cinemax-mp4.hf.space/hls/{hash_video}/master.m3u8"}
|
| 34 |
|
| 35 |
os.makedirs(dir_video, exist_ok=True)
|
| 36 |
|
| 37 |
-
# Analizar el MP4
|
| 38 |
probe_cmd = ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", url]
|
| 39 |
probe_res = subprocess.run(probe_cmd, capture_output=True, text=True)
|
| 40 |
|
|
@@ -51,7 +47,6 @@ def iniciar_transmision(url: str):
|
|
| 51 |
"lang": stream.get("tags", {}).get("language", "und")
|
| 52 |
})
|
| 53 |
|
| 54 |
-
# Construir FFmpeg
|
| 55 |
cmd = ["ffmpeg", "-y", "-i", url, "-map", "0:v:0"]
|
| 56 |
var_stream_map = "v:0,agroup:audios"
|
| 57 |
|
|
@@ -63,7 +58,7 @@ def iniciar_transmision(url: str):
|
|
| 63 |
cmd.extend([
|
| 64 |
"-c", "copy",
|
| 65 |
"-f", "hls",
|
| 66 |
-
"-hls_time", "2",
|
| 67 |
"-hls_list_size", "0",
|
| 68 |
"-hls_playlist_type", "event",
|
| 69 |
"-hls_segment_filename", f"{dir_video}/pedazo_%v_%03d.ts",
|
|
@@ -72,20 +67,22 @@ def iniciar_transmision(url: str):
|
|
| 72 |
f"{dir_video}/playlist_%v.m3u8"
|
| 73 |
])
|
| 74 |
|
| 75 |
-
# Lanzar FFmpeg
|
| 76 |
subprocess.Popen(cmd)
|
| 77 |
|
| 78 |
-
#
|
|
|
|
|
|
|
| 79 |
tiempo_espera = 0
|
| 80 |
-
|
|
|
|
| 81 |
time.sleep(0.5)
|
| 82 |
tiempo_espera += 0.5
|
| 83 |
|
| 84 |
-
if not os.path.exists(
|
| 85 |
-
return {"error": "FFmpeg
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
time.sleep(
|
| 89 |
|
| 90 |
return {
|
| 91 |
"url_reproductor": f"https://cinemax-mp4.hf.space/hls/{hash_video}/master.m3u8"
|
|
|
|
| 16 |
allow_methods=["*"],
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
BASE_HLS = "/tmp/streams"
|
| 20 |
os.makedirs(BASE_HLS, exist_ok=True)
|
| 21 |
app.mount("/hls", StaticFiles(directory=BASE_HLS), name="hls")
|
| 22 |
|
| 23 |
@app.get("/iniciar-transmision")
|
| 24 |
def iniciar_transmision(url: str):
|
|
|
|
| 25 |
hash_video = hashlib.md5(url.encode()).hexdigest()
|
| 26 |
dir_video = f"{BASE_HLS}/{hash_video}"
|
|
|
|
|
|
|
| 27 |
master_file = f"{dir_video}/master.m3u8"
|
| 28 |
+
|
| 29 |
if os.path.exists(master_file):
|
| 30 |
return {"url_reproductor": f"https://cinemax-mp4.hf.space/hls/{hash_video}/master.m3u8"}
|
| 31 |
|
| 32 |
os.makedirs(dir_video, exist_ok=True)
|
| 33 |
|
|
|
|
| 34 |
probe_cmd = ["ffprobe", "-v", "quiet", "-print_format", "json", "-show_streams", url]
|
| 35 |
probe_res = subprocess.run(probe_cmd, capture_output=True, text=True)
|
| 36 |
|
|
|
|
| 47 |
"lang": stream.get("tags", {}).get("language", "und")
|
| 48 |
})
|
| 49 |
|
|
|
|
| 50 |
cmd = ["ffmpeg", "-y", "-i", url, "-map", "0:v:0"]
|
| 51 |
var_stream_map = "v:0,agroup:audios"
|
| 52 |
|
|
|
|
| 58 |
cmd.extend([
|
| 59 |
"-c", "copy",
|
| 60 |
"-f", "hls",
|
| 61 |
+
"-hls_time", "2",
|
| 62 |
"-hls_list_size", "0",
|
| 63 |
"-hls_playlist_type", "event",
|
| 64 |
"-hls_segment_filename", f"{dir_video}/pedazo_%v_%03d.ts",
|
|
|
|
| 67 |
f"{dir_video}/playlist_%v.m3u8"
|
| 68 |
])
|
| 69 |
|
|
|
|
| 70 |
subprocess.Popen(cmd)
|
| 71 |
|
| 72 |
+
# LA CLAVE: Esperamos SOLO a que exista el PRIMER fragmento de video.
|
| 73 |
+
# En cuanto este archivo nace, significa que ya hay video para mostrar.
|
| 74 |
+
primer_fragmento = f"{dir_video}/pedazo_0_000.ts"
|
| 75 |
tiempo_espera = 0
|
| 76 |
+
|
| 77 |
+
while not os.path.exists(primer_fragmento) and tiempo_espera < 15:
|
| 78 |
time.sleep(0.5)
|
| 79 |
tiempo_espera += 0.5
|
| 80 |
|
| 81 |
+
if not os.path.exists(primer_fragmento):
|
| 82 |
+
return {"error": "FFmpeg está atascado leyendo el inicio del archivo."}
|
| 83 |
|
| 84 |
+
# Un respiro de medio segundo para que la lista m3u8 se termine de guardar
|
| 85 |
+
time.sleep(0.5)
|
| 86 |
|
| 87 |
return {
|
| 88 |
"url_reproductor": f"https://cinemax-mp4.hf.space/hls/{hash_video}/master.m3u8"
|