SorovotPelo commited on
Commit
55552d2
·
verified ·
1 Parent(s): d6a0179

Update stream_audio.py

Browse files
Files changed (1) hide show
  1. stream_audio.py +15 -4
stream_audio.py CHANGED
@@ -7,11 +7,10 @@ from fastapi import FastAPI, Response
7
 
8
  app = FastAPI()
9
 
10
- # Directorio del archivo de audio
11
  audio_file = "/app/theforgottenradio.mp3"
12
-
13
- # Ruta de salida para la transmisión HLS
14
  hls_output = "/app/stream.m3u8"
 
15
 
16
  # Comando FFmpeg para transmitir audio en HLS
17
  ffmpeg_command = [
@@ -24,6 +23,7 @@ ffmpeg_command = [
24
  '-hls_time', '10',
25
  '-hls_list_size', '0',
26
  '-hls_flags', 'delete_segments',
 
27
  hls_output
28
  ]
29
 
@@ -49,12 +49,23 @@ async def read_root():
49
  return {"message": "Servidor de Radio M3U8 Activo"}
50
 
51
  @app.get("/stream.m3u8")
52
- def get_stream():
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
  if __name__ == "__main__":
59
  start_audio_streaming()
60
  uvicorn.run(app, host="0.0.0.0", port=7860)
 
7
 
8
  app = FastAPI()
9
 
10
+ # Archivo de audio y rutas de salida
11
  audio_file = "/app/theforgottenradio.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 = [
 
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
 
 
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)