Spaces:
Runtime error
Runtime error
Create stream_videos.py
Browse files- stream_videos.py +47 -0
stream_videos.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Response
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import glob
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
@app.get("/")
|
| 9 |
+
def read_root():
|
| 10 |
+
return {"message": "Servidor de Radio M3U8 Activo"}
|
| 11 |
+
|
| 12 |
+
@app.get("/stream.m3u8")
|
| 13 |
+
def stream():
|
| 14 |
+
# Buscar todos los archivos .mp3 en el directorio /app
|
| 15 |
+
mp3_files = glob.glob("/app/*.mp3")
|
| 16 |
+
if not mp3_files:
|
| 17 |
+
return {"error": "No se encontraron archivos MP3 en el directorio /app"}
|
| 18 |
+
|
| 19 |
+
# Crear una lista de archivos para la entrada concatenada
|
| 20 |
+
with open("file_list.txt", "w") as f:
|
| 21 |
+
for mp3_file in mp3_files:
|
| 22 |
+
f.write(f"file '{mp3_file}'\n")
|
| 23 |
+
|
| 24 |
+
# Comando ffmpeg para generar la transmisión M3U8
|
| 25 |
+
cmd = [
|
| 26 |
+
"ffmpeg",
|
| 27 |
+
"-re",
|
| 28 |
+
"-f", "concat",
|
| 29 |
+
"-safe", "0",
|
| 30 |
+
"-i", "file_list.txt",
|
| 31 |
+
"-c:a", "aac",
|
| 32 |
+
"-b:a", "128k",
|
| 33 |
+
"-f", "hls",
|
| 34 |
+
"-hls_time", "10",
|
| 35 |
+
"-hls_list_size", "0",
|
| 36 |
+
"-hls_flags", "delete_segments",
|
| 37 |
+
"stream.m3u8"
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
# Ejecuta el comando ffmpeg
|
| 41 |
+
subprocess.Popen(cmd)
|
| 42 |
+
|
| 43 |
+
# Devuelve el archivo M3U8 generado
|
| 44 |
+
with open("stream.m3u8", "rb") as f:
|
| 45 |
+
m3u8_content = f.read()
|
| 46 |
+
|
| 47 |
+
return Response(content=m3u8_content, media_type="application/vnd.apple.mpegurl")
|