CineMax commited on
Commit
8383140
·
verified ·
1 Parent(s): 24f3ca1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -25
app.py CHANGED
@@ -1,37 +1,53 @@
1
- from fastapi import FastAPI
2
  import subprocess
3
- import json
4
  import uvicorn
 
 
 
5
 
6
  app = FastAPI()
7
 
8
- @app.get("/extraer-audios")
9
- def extraer_audios(url: str):
10
- # Comando FFprobe para leer la URL del MP4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  comando = [
12
- "ffprobe", "-v", "quiet", "-print_format", "json",
13
- "-show_streams", url
 
 
 
 
 
 
 
 
14
  ]
15
 
16
- resultado = subprocess.run(comando, capture_output=True, text=True)
17
-
18
- # Manejo de errores por si ffprobe falla al leer la URL
19
- try:
20
- datos = json.loads(resultado.stdout)
21
- except json.JSONDecodeError:
22
- return {"error": "No se pudo leer el video. Verifica que la URL sea pública y directa al MP4."}
23
 
24
- audios = []
25
- if "streams" in datos:
26
- for stream in datos["streams"]:
27
- if stream.get("codec_type") == "audio":
28
- audios.append({
29
- "id": stream.get("index"),
30
- "idioma": stream.get("tags", {}).get("language", "Desconocido")
31
- })
32
-
33
- return {"audios": audios}
34
 
35
- # FALTABA ESTA PARTE: Mantener el servidor encendido en el puerto correcto
36
  if __name__ == "__main__":
37
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
1
  import subprocess
2
+ import os
3
  import uvicorn
4
+ from fastapi import FastAPI
5
+ from fastapi.staticfiles import StaticFiles
6
+ from fastapi.middleware.cors import CORSMiddleware
7
 
8
  app = FastAPI()
9
 
10
+ # Permitir conexiones desde tu hosting
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"],
14
+ allow_methods=["*"],
15
+ )
16
+
17
+ # Creamos una carpeta temporal en Hugging Face para guardar los pedazos de video
18
+ DIRECTORIO_HLS = "/tmp/hls_en_vivo"
19
+ os.makedirs(DIRECTORIO_HLS, exist_ok=True)
20
+
21
+ # Servimos esa carpeta públicamente para que JW Player pueda leerla
22
+ app.mount("/hls", StaticFiles(directory=DIRECTORIO_HLS), name="hls")
23
+
24
+ @app.get("/iniciar-transmision")
25
+ def iniciar_transmision(url: str):
26
+ # Limpiamos conversiones anteriores
27
+ os.system(f"rm -rf {DIRECTORIO_HLS}/*")
28
+
29
+ # Comando brutal de FFmpeg: Copia video, separa 2 pistas de audio y crea HLS en vivo
30
  comando = [
31
+ "ffmpeg", "-y", "-i", url,
32
+ "-map", "0:v:0", "-map", "0:a:0", "-map", "0:a:1", # Asumimos que tiene 2 audios
33
+ "-c", "copy", # VITAL: Copiamos sin recodificar para que la CPU de HF no explote
34
+ "-f", "hls",
35
+ "-hls_time", "10", # Pedazos de 10 segundos
36
+ "-hls_list_size", "0",
37
+ "-hls_segment_filename", f"{DIRECTORIO_HLS}/pedazo_%v_%03d.ts",
38
+ "-master_pl_name", "master.m3u8",
39
+ "-var_stream_map", "v:0,a:0 a:1", # Asocia el video con los audios separados
40
+ f"{DIRECTORIO_HLS}/playlist_%v.m3u8"
41
  ]
42
 
43
+ # Lanzamos el proceso de FFmpeg en segundo plano
44
+ subprocess.Popen(comando)
 
 
 
 
 
45
 
46
+ # Le devolvemos a tu PHP la URL del archivo maestro recién creado
47
+ return {
48
+ "estado": "Procesando en vivo",
49
+ "url_reproductor": "https://cinemax-mp4.hf.space/hls/master.m3u8"
50
+ }
 
 
 
 
 
51
 
 
52
  if __name__ == "__main__":
53
  uvicorn.run(app, host="0.0.0.0", port=7860)