teszenofficial commited on
Commit
217cbcf
·
verified ·
1 Parent(s): b78b20e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -32
app.py CHANGED
@@ -1,61 +1,80 @@
1
  import gradio as gr
2
- from pydub import AudioSegment
3
- import uuid
4
- import os
5
 
6
  UPLOAD_DIR = "uploads"
7
  OUTPUT_DIR = "outputs"
8
- CROSSFADE_MS = 20_000 # 20 segundos reales
9
 
10
  os.makedirs(UPLOAD_DIR, exist_ok=True)
11
  os.makedirs(OUTPUT_DIR, exist_ok=True)
12
 
13
  # -------------------------
14
- # MOTOR DE MEZCLA
15
  # -------------------------
16
 
17
- def create_mix(files, progress=gr.Progress()):
 
 
 
 
 
18
  if not files or len(files) < 2 or len(files) > 4:
19
  raise gr.Error("Sube entre 2 y 4 canciones")
20
 
21
- progress(0, desc="Validando archivos…")
 
22
 
23
- paths = []
24
- for f in files:
 
 
 
 
 
 
25
  ext = f.name.split(".")[-1].lower()
26
  if ext not in ["mp3", "wav", "flac", "m4a"]:
27
  raise gr.Error("Formato no soportado")
28
 
29
- name = f"{uuid.uuid4().hex}.{ext}"
30
- path = os.path.join(UPLOAD_DIR, name)
31
-
32
- # Copia segura (HF-friendly)
33
  with open(f.name, "rb") as src, open(path, "wb") as dst:
34
  dst.write(src.read())
35
 
36
- paths.append(path)
 
 
 
 
 
 
 
 
 
37
 
38
- progress(10, desc="Cargando audio…")
39
 
40
- current = AudioSegment.from_file(paths[0])
41
- step = 80 // (len(paths) - 1)
42
 
43
- for i, p in enumerate(paths[1:], start=1):
44
- progress(10 + step * (i - 1), desc=f"Mezclando canción {i + 1}")
45
- next_track = AudioSegment.from_file(p)
46
- current = current.append(next_track, crossfade=CROSSFADE_MS)
47
 
48
- progress(95, desc="Exportando en M4A (ligero)…")
 
 
 
 
 
 
 
49
 
50
  out_name = f"monx_ai_mix_{uuid.uuid4().hex}.m4a"
51
  out_path = os.path.join(OUTPUT_DIR, out_name)
52
 
53
- # 🔑 Exportación AAC (M4A)
54
- current.export(
55
  out_path,
56
- format="ipod", # m4a
57
  codec="aac",
58
- bitrate="192k" # buena calidad / poco peso
59
  )
60
 
61
  progress(100, desc="Mix listo 🎶")
@@ -66,22 +85,35 @@ def create_mix(files, progress=gr.Progress()):
66
  # INTERFAZ GRADIO
67
  # -------------------------
68
 
69
- with gr.Blocks(title="MONX AI") as demo:
70
  gr.Markdown(
71
  """
72
  <div style="text-align:center">
73
- <h1>🎵 MONX AI</h1>
74
- <p>Fusión de audio inteligente con crossfade (exporta en M4A)</p>
75
  </div>
76
  """
77
  )
78
 
79
  files = gr.File(
80
- label="Sube 2 a 4 canciones (MP3, WAV, FLAC, M4A)",
81
  file_count="multiple",
82
  file_types=[".mp3", ".wav", ".flac", ".m4a"]
83
  )
84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85
  btn = gr.Button("✨ Crear Mix", variant="primary")
86
 
87
  output = gr.Audio(
@@ -89,9 +121,13 @@ with gr.Blocks(title="MONX AI") as demo:
89
  type="filepath"
90
  )
91
 
 
 
 
 
92
  btn.click(
93
- fn=create_mix,
94
- inputs=files,
95
  outputs=output
96
  )
97
 
 
1
  import gradio as gr
2
+ from pydub import AudioSegment, effects
3
+ import uuid, os
 
4
 
5
  UPLOAD_DIR = "uploads"
6
  OUTPUT_DIR = "outputs"
 
7
 
8
  os.makedirs(UPLOAD_DIR, exist_ok=True)
9
  os.makedirs(OUTPUT_DIR, exist_ok=True)
10
 
11
  # -------------------------
12
+ # MOTOR DE MEZCLA PRO
13
  # -------------------------
14
 
15
+ def create_mix(
16
+ files,
17
+ start_times,
18
+ crossfade_sec,
19
+ progress=gr.Progress()
20
+ ):
21
  if not files or len(files) < 2 or len(files) > 4:
22
  raise gr.Error("Sube entre 2 y 4 canciones")
23
 
24
+ if len(start_times) != len(files):
25
+ raise gr.Error("Los tiempos deben coincidir con el número de canciones")
26
 
27
+ crossfade_ms = int(crossfade_sec * 1000)
28
+
29
+ progress(0, desc="Cargando canciones…")
30
+
31
+ segments = []
32
+
33
+ # Preparar canciones
34
+ for i, f in enumerate(files):
35
  ext = f.name.split(".")[-1].lower()
36
  if ext not in ["mp3", "wav", "flac", "m4a"]:
37
  raise gr.Error("Formato no soportado")
38
 
39
+ path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4().hex}.{ext}")
 
 
 
40
  with open(f.name, "rb") as src, open(path, "wb") as dst:
41
  dst.write(src.read())
42
 
43
+ audio = AudioSegment.from_file(path)
44
+
45
+ start_ms = int(start_times[i] * 1000)
46
+ if start_ms >= len(audio):
47
+ raise gr.Error(f"El inicio de la canción {i+1} es mayor que su duración")
48
+
49
+ audio = audio[start_ms:]
50
+ audio = effects.normalize(audio) # volumen estable
51
+
52
+ segments.append(audio)
53
 
54
+ progress(20, desc="Mezclando…")
55
 
56
+ # Mezcla secuencial con control
57
+ mix = segments[0]
58
 
59
+ step = 60 // (len(segments) - 1)
 
 
 
60
 
61
+ for i in range(1, len(segments)):
62
+ progress(20 + step * (i - 1), desc=f"Crossfade canción {i+1}")
63
+ mix = mix.append(
64
+ segments[i],
65
+ crossfade=crossfade_ms
66
+ )
67
+
68
+ progress(90, desc="Exportando mix…")
69
 
70
  out_name = f"monx_ai_mix_{uuid.uuid4().hex}.m4a"
71
  out_path = os.path.join(OUTPUT_DIR, out_name)
72
 
73
+ mix.export(
 
74
  out_path,
75
+ format="ipod",
76
  codec="aac",
77
+ bitrate="192k"
78
  )
79
 
80
  progress(100, desc="Mix listo 🎶")
 
85
  # INTERFAZ GRADIO
86
  # -------------------------
87
 
88
+ with gr.Blocks(title="MONX AI Pro Mix") as demo:
89
  gr.Markdown(
90
  """
91
  <div style="text-align:center">
92
+ <h1>🎛️ MONX AI – Pro Mix</h1>
93
+ <p>Control total de entradas, salidas y crossfade</p>
94
  </div>
95
  """
96
  )
97
 
98
  files = gr.File(
99
+ label="Sube 2 a 4 canciones",
100
  file_count="multiple",
101
  file_types=[".mp3", ".wav", ".flac", ".m4a"]
102
  )
103
 
104
+ start_times = gr.Textbox(
105
+ label="Segundos de inicio (ej: 0, 30, 45, 60)",
106
+ placeholder="Uno por canción, separados por coma"
107
+ )
108
+
109
+ crossfade = gr.Slider(
110
+ minimum=5,
111
+ maximum=30,
112
+ value=15,
113
+ step=1,
114
+ label="Duración del crossfade (segundos)"
115
+ )
116
+
117
  btn = gr.Button("✨ Crear Mix", variant="primary")
118
 
119
  output = gr.Audio(
 
121
  type="filepath"
122
  )
123
 
124
+ def wrapper(files, start_times, crossfade, progress=gr.Progress()):
125
+ times = [float(t.strip()) for t in start_times.split(",")]
126
+ return create_mix(files, times, crossfade, progress)
127
+
128
  btn.click(
129
+ fn=wrapper,
130
+ inputs=[files, start_times, crossfade],
131
  outputs=output
132
  )
133