Spaces:
Build error
Build error
Upload 3 files
Browse files- app.py +68 -41
- requirements.txt +3 -2
app.py
CHANGED
|
@@ -1,44 +1,71 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
if __name__ == "__main__":
|
| 44 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
+
import subprocess
|
| 4 |
+
from multiprocessing import Process
|
| 5 |
+
import uvicorn
|
| 6 |
+
from fastapi import FastAPI, Response
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# Archivo de audio y rutas de salida
|
| 11 |
+
audio_file = "/app/test.mp3"
|
| 12 |
+
hls_output = "/app/stream.m3u8"
|
| 13 |
+
hls_segment_prefix = "/app/stream"
|
| 14 |
+
|
| 15 |
+
# Comando FFmpeg para transmitir audio en HLS
|
| 16 |
+
ffmpeg_command = [
|
| 17 |
+
'ffmpeg',
|
| 18 |
+
'-re',
|
| 19 |
+
'-i', audio_file,
|
| 20 |
+
'-c:a', 'aac',
|
| 21 |
+
'-b:a', '128k',
|
| 22 |
+
'-f', 'hls',
|
| 23 |
+
'-hls_time', '10',
|
| 24 |
+
'-hls_list_size', '0',
|
| 25 |
+
'-hls_flags', 'delete_segments',
|
| 26 |
+
'-hls_segment_filename', f'{hls_segment_prefix}%d.ts', # Nombre para los segmentos
|
| 27 |
+
hls_output
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
def stream_audio():
|
| 31 |
+
# Ejecuta el comando FFmpeg para iniciar la transmisi贸n
|
| 32 |
+
process = subprocess.Popen(ffmpeg_command)
|
| 33 |
+
process.wait()
|
| 34 |
+
|
| 35 |
+
def start_audio_streaming():
|
| 36 |
+
audio_process = Process(target=stream_audio)
|
| 37 |
+
audio_process.start()
|
| 38 |
+
|
| 39 |
+
@app.on_event("startup")
|
| 40 |
+
async def startup_event():
|
| 41 |
+
# Espera activa para verificar que el archivo M3U8 est茅 generado
|
| 42 |
+
while not os.path.exists(hls_output):
|
| 43 |
+
print("Esperando a que se genere el archivo M3U8...")
|
| 44 |
+
time.sleep(1)
|
| 45 |
+
print("Archivo M3U8 generado. Servidor listo para recibir solicitudes.")
|
| 46 |
+
|
| 47 |
+
@app.get("/")
|
| 48 |
+
async def read_root():
|
| 49 |
+
return {"message": "Servidor de Radio M3U8 Activo"}
|
| 50 |
+
|
| 51 |
+
@app.get("/stream.m3u8")
|
| 52 |
+
def get_m3u8():
|
| 53 |
+
# Devuelve el archivo M3U8 generado
|
| 54 |
+
with open(hls_output, "rb") as f:
|
| 55 |
+
m3u8_content = f.read()
|
| 56 |
+
return Response(content=m3u8_content, media_type="application/vnd.apple.mpegurl")
|
| 57 |
+
|
| 58 |
+
@app.get("/stream{segment}.ts")
|
| 59 |
+
def get_segment(segment: int):
|
| 60 |
+
# Devuelve los segmentos TS generados
|
| 61 |
+
segment_file = f"{hls_segment_prefix}{segment}.ts"
|
| 62 |
+
if os.path.exists(segment_file):
|
| 63 |
+
with open(segment_file, "rb") as f:
|
| 64 |
+
segment_content = f.read()
|
| 65 |
+
return Response(content=segment_content, media_type="video/mp2t")
|
| 66 |
+
else:
|
| 67 |
+
return Response(status_code=404, content="Segment not found")
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
start_audio_streaming()
|
| 71 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 3 |
ffmpeg-python==0.2.0
|
|
|
|
| 1 |
+
fastapi==0.79.0
|
| 2 |
+
uvicorn==0.18.3
|
| 3 |
+
requests==2.28.1
|
| 4 |
ffmpeg-python==0.2.0
|