Update app.py
Browse files
app.py
CHANGED
|
@@ -1,105 +1,80 @@
|
|
| 1 |
import subprocess
|
| 2 |
-
|
| 3 |
import os
|
| 4 |
-
|
| 5 |
import uvicorn
|
| 6 |
-
|
| 7 |
-
from fastapi import FastAPI
|
| 8 |
-
|
| 9 |
from fastapi.staticfiles import StaticFiles
|
| 10 |
-
|
| 11 |
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
# Permitir conexiones desde tu hosting
|
| 20 |
-
|
| 21 |
-
app.add_middleware(
|
| 22 |
-
|
| 23 |
-
CORSMiddleware,
|
| 24 |
-
|
| 25 |
-
allow_origins=["*"],
|
| 26 |
-
|
| 27 |
-
allow_methods=["*"],
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# Creamos una carpeta temporal en Hugging Face para guardar los pedazos de video
|
| 34 |
-
|
| 35 |
-
DIRECTORIO_HLS = "/tmp/hls_en_vivo"
|
| 36 |
-
|
| 37 |
os.makedirs(DIRECTORIO_HLS, exist_ok=True)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# Servimos esa carpeta públicamente para que JW Player pueda leerla
|
| 42 |
-
|
| 43 |
app.mount("/hls", StaticFiles(directory=DIRECTORIO_HLS), name="hls")
|
| 44 |
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
def iniciar_transmision(url: str):
|
| 50 |
-
|
| 51 |
-
# Limpiamos conversiones anteriores
|
| 52 |
-
|
| 53 |
os.system(f"rm -rf {DIRECTORIO_HLS}/*")
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# Comando brutal de FFmpeg: Copia video, separa 2 pistas de audio y crea HLS en vivo
|
| 58 |
-
|
| 59 |
comando = [
|
| 60 |
-
|
| 61 |
"ffmpeg", "-y", "-i", url,
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"-
|
|
|
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
"-
|
|
|
|
|
|
|
| 74 |
|
|
|
|
| 75 |
"-master_pl_name", "master.m3u8",
|
| 76 |
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
|
| 79 |
f"{DIRECTORIO_HLS}/playlist_%v.m3u8"
|
| 80 |
-
|
| 81 |
]
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
# Le devolvemos a tu PHP la URL del archivo maestro recién creado
|
| 92 |
|
|
|
|
|
|
|
|
|
|
| 93 |
return {
|
| 94 |
-
|
| 95 |
-
"estado": "Procesando en vivo",
|
| 96 |
-
|
| 97 |
"url_reproductor": "https://cinemax-mp4.hf.space/hls/master.m3u8"
|
| 98 |
-
|
| 99 |
}
|
| 100 |
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|
| 104 |
-
|
| 105 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
import subprocess
|
|
|
|
| 2 |
import os
|
|
|
|
| 3 |
import uvicorn
|
| 4 |
+
from fastapi import FastAPI, BackgroundTasks
|
|
|
|
|
|
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 6 |
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
|
|
|
|
|
|
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"])
|
| 11 |
|
| 12 |
+
DIRECTORIO_HLS = "/tmp/hls_salida"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
os.makedirs(DIRECTORIO_HLS, exist_ok=True)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
app.mount("/hls", StaticFiles(directory=DIRECTORIO_HLS), name="hls")
|
| 16 |
|
| 17 |
+
proceso_ffmpeg = None
|
| 18 |
|
| 19 |
+
def correr_ffmpeg(url: str):
|
| 20 |
+
global proceso_ffmpeg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
os.system(f"rm -rf {DIRECTORIO_HLS}/*")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
comando = [
|
|
|
|
| 24 |
"ffmpeg", "-y", "-i", url,
|
| 25 |
|
| 26 |
+
# Mapear video y las 2 pistas de audio
|
| 27 |
+
"-map", "0:v:0",
|
| 28 |
+
"-map", "0:a:0",
|
| 29 |
+
"-map", "0:a:1",
|
| 30 |
|
| 31 |
+
# Metadata de idioma para que el player muestre los nombres
|
| 32 |
+
"-metadata:s:a:0", "language=spa",
|
| 33 |
+
"-metadata:s:a:0", "title=Español",
|
| 34 |
+
"-metadata:s:a:1", "language=eng",
|
| 35 |
+
"-metadata:s:a:1", "title=English",
|
| 36 |
|
| 37 |
+
# Sin recodificar
|
| 38 |
+
"-c", "copy",
|
| 39 |
|
| 40 |
+
# HLS VOD
|
| 41 |
+
"-f", "hls",
|
| 42 |
+
"-hls_time", "6",
|
| 43 |
+
"-hls_list_size", "0", # 0 = guarda TODOS los segmentos (VOD)
|
| 44 |
+
"-hls_flags", "independent_segments", # necesario para VOD correcto
|
| 45 |
|
| 46 |
+
"-hls_segment_filename", f"{DIRECTORIO_HLS}/seg_%v_%03d.ts",
|
| 47 |
"-master_pl_name", "master.m3u8",
|
| 48 |
|
| 49 |
+
# ✅ CLAVE: agroup:audios vincula ambas pistas al mismo grupo de audio
|
| 50 |
+
# stream 0 = video + audio ESP, stream 1 = solo audio ENG (alternativo)
|
| 51 |
+
"-var_stream_map", "v:0,a:0,agroup:audios a:1,agroup:audios",
|
| 52 |
|
| 53 |
f"{DIRECTORIO_HLS}/playlist_%v.m3u8"
|
|
|
|
| 54 |
]
|
| 55 |
|
| 56 |
+
proceso_ffmpeg = subprocess.Popen(
|
| 57 |
+
comando,
|
| 58 |
+
stdout=subprocess.DEVNULL,
|
| 59 |
+
stderr=subprocess.PIPE # captura errores para debug
|
| 60 |
+
)
|
| 61 |
+
proceso_ffmpeg.wait()
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
+
@app.get("/iniciar-transmision")
|
| 64 |
+
def iniciar_transmision(url: str, background_tasks: BackgroundTasks):
|
| 65 |
+
background_tasks.add_task(correr_ffmpeg, url)
|
| 66 |
return {
|
| 67 |
+
"estado": "Procesando...",
|
|
|
|
|
|
|
| 68 |
"url_reproductor": "https://cinemax-mp4.hf.space/hls/master.m3u8"
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
+
@app.get("/estado")
|
| 72 |
+
def estado():
|
| 73 |
+
if proceso_ffmpeg is None:
|
| 74 |
+
return {"estado": "sin_proceso"}
|
| 75 |
+
if proceso_ffmpeg.poll() is None:
|
| 76 |
+
return {"estado": "procesando"}
|
| 77 |
+
return {"estado": "listo", "codigo": proceso_ffmpeg.returncode}
|
| 78 |
|
| 79 |
if __name__ == "__main__":
|
|
|
|
| 80 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|