OptiJuegos commited on
Commit
caf6b3f
verified
1 Parent(s): 8af7697

Upload 3 files

Browse files
Files changed (2) hide show
  1. app.py +68 -41
  2. requirements.txt +3 -2
app.py CHANGED
@@ -1,44 +1,71 @@
1
  import os
2
- import ffmpeg
3
- import gradio as gr
4
-
5
- def generate_m3u8(audio_file):
6
- """
7
- Genera un archivo M3U8 a partir de un archivo de audio.
8
-
9
- Args:
10
- audio_file (str): La ruta del archivo de audio.
11
-
12
- Returns:
13
- str: La URL del archivo M3U8 generado.
14
- """
15
- # Obtener el nombre del archivo sin la extensi贸n
16
- file_name = os.path.splitext(os.path.basename(audio_file))[0]
17
-
18
- # Crear el archivo M3U8 y los segmentos TS
19
- input_stream = ffmpeg.input(audio_file)
20
- output_stream = ffmpeg.output(input_stream, f"{file_name}_%03d.ts", format='segment', segment_time=10)
21
- output_stream = ffmpeg.output(output_stream, f"{file_name}.m3u8", f='hls', hls_time=10, hls_list_size=0, hls_wrap=0)
22
- ffmpeg.run(output_stream)
23
-
24
- # Devolver la URL del archivo M3U8
25
- return f"http://localhost:7860/{file_name}.m3u8"
26
-
27
- def main():
28
- # Crear la interfaz de Gradio
29
- audio_file = gr.File(label="Selecciona un archivo de audio")
30
- output_url = gr.Textbox(label="URL del archivo M3U8")
31
-
32
- demo = gr.Interface(
33
- fn=generate_m3u8,
34
- inputs=audio_file,
35
- outputs=output_url,
36
- title="Generador de M3U8 a partir de audio",
37
- description="Selecciona un archivo de audio y genera un archivo M3U8 en tiempo real."
38
- )
39
-
40
- # Ejecutar la aplicaci贸n de Gradio con un enlace p煤blico
41
- demo.launch(server_port=7860, share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
  if __name__ == "__main__":
44
- main()
 
 
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
- flask==2.3.2
2
- opencv-python==4.8.0.74
 
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