Cristobal commited on
Commit
3bdc3f4
·
1 Parent(s): 063608f

musica de fondo del cliente mezclada con voz

Browse files
Files changed (1) hide show
  1. app.py +45 -1
app.py CHANGED
@@ -138,7 +138,22 @@ def montar_video(guion, archivos, subs=None, efecto="ninguno"):
138
  else:
139
  txts = []
140
 
141
- final = CompositeVideoClip([clip]+txts).set_audio(voz)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  final.write_videofile("preview.mp4", fps=30, codec="libx264", bitrate="1500k", preset="medium", logger=None, threads=2, audio_codec="aac")
143
  return "preview.mp4"
144
 
@@ -216,6 +231,22 @@ async def ep_voz_grabada(file: UploadFile = File(...)):
216
  except Exception as e:
217
  return JSONResponse({"error": str(e)[:120]}, status_code=500)
218
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
219
  @app.get("/audio")
220
  def ep_audio():
221
  if os.path.exists("voz.mp3"):
@@ -376,6 +407,11 @@ audio,video{width:100%;margin-top:10px;border-radius:8px}
376
  </div>
377
  <div class="msg" id="msgVoz"></div>
378
  <audio id="player" controls style="display:none"></audio>
 
 
 
 
 
379
  </div>
380
 
381
  <div class="fase">
@@ -472,6 +508,14 @@ async function genVideo(){
472
  }catch(e){}
473
  },4000);
474
  }
 
 
 
 
 
 
 
 
475
  function cargarVideo(){
476
  var u="/video?t="+Date.now();
477
  var v=document.getElementById("vid");v.src=u;v.style.display="block";v.load();
 
138
  else:
139
  txts = []
140
 
141
+ # Mezclar musica de fondo si el cliente la subio
142
+ import os as _os
143
+ audio_final = voz
144
+ if _os.path.exists("musica.mp3"):
145
+ try:
146
+ from moviepy.editor import CompositeAudioClip, AudioFileClip as _AC
147
+ musica = _AC("musica.mp3").volumex(0.18) # musica bajita para no tapar la voz
148
+ if musica.duration > voz.duration:
149
+ musica = musica.subclip(0, voz.duration)
150
+ else:
151
+ musica = musica.audio_loop(duration=voz.duration)
152
+ audio_final = CompositeAudioClip([voz.volumex(1.0), musica])
153
+ except Exception as _em:
154
+ log(f"Error mezcla musica: {_em}")
155
+ audio_final = voz
156
+ final = CompositeVideoClip([clip]+txts).set_audio(audio_final)
157
  final.write_videofile("preview.mp4", fps=30, codec="libx264", bitrate="1500k", preset="medium", logger=None, threads=2, audio_codec="aac")
158
  return "preview.mp4"
159
 
 
231
  except Exception as e:
232
  return JSONResponse({"error": str(e)[:120]}, status_code=500)
233
 
234
+
235
+ @app.post("/subir-musica")
236
+ async def ep_musica(file: UploadFile = File(...)):
237
+ try:
238
+ import subprocess, os as _os
239
+ contenido = await file.read()
240
+ with open("musica_raw","wb") as f:
241
+ f.write(contenido)
242
+ subprocess.run(["ffmpeg","-y","-i","musica_raw","-acodec","libmp3lame","musica.mp3"], capture_output=True)
243
+ if _os.path.exists("musica.mp3") and _os.path.getsize("musica.mp3") > 1000:
244
+ ESTADO["musica"] = True
245
+ return {"ok": True}
246
+ return JSONResponse({"error":"No se pudo procesar la musica"}, status_code=500)
247
+ except Exception as e:
248
+ return JSONResponse({"error": str(e)[:120]}, status_code=500)
249
+
250
  @app.get("/audio")
251
  def ep_audio():
252
  if os.path.exists("voz.mp3"):
 
407
  </div>
408
  <div class="msg" id="msgVoz"></div>
409
  <audio id="player" controls style="display:none"></audio>
410
+ <div style="margin-top:14px;padding-top:14px;border-top:1px solid #222">
411
+ <span class="lbl">Musica de fondo (opcional, sonara bajita bajo la voz):</span>
412
+ <input type="file" id="musica" accept="audio/*" onchange="subirMusica()">
413
+ <div class="msg" id="msgMusica"></div>
414
+ </div>
415
  </div>
416
 
417
  <div class="fase">
 
508
  }catch(e){}
509
  },4000);
510
  }
511
+ async function subirMusica(){
512
+ var f=document.getElementById("musica").files[0]; if(!f)return;
513
+ var m=document.getElementById("msgMusica");m.textContent="Subiendo musica...";m.style.color="#ffb700";
514
+ var fd=new FormData(); fd.append("file",f);
515
+ var r=await fetch("/subir-musica",{method:"POST",body:fd}); var d=await r.json();
516
+ if(d.ok){m.textContent="✅ Musica anadida, sonara de fondo";m.style.color="#00c853";}
517
+ else{m.textContent="❌ "+(d.error||"Error");m.style.color="#ff5252";}
518
+ }
519
  function cargarVideo(){
520
  var u="/video?t="+Date.now();
521
  var v=document.getElementById("vid");v.src=u;v.style.display="block";v.load();