Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,86 @@
|
|
1 |
import gradio as gr
|
2 |
-
from tts_module import text_to_speech
|
3 |
-
from
|
4 |
-
from
|
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 |
# Interfaz Gradio
|
36 |
with gr.Blocks() as demo:
|
@@ -41,10 +91,13 @@ with gr.Blocks() as demo:
|
|
41 |
txt_file_input = gr.File(label="Or upload a .txt file", file_types=[".txt"])
|
42 |
mp3_file_input = gr.File(label="Upload background music (.mp3)", file_types=[".mp3"])
|
43 |
prompt_input = gr.Textbox(label="Or enter a prompt to generate text")
|
|
|
|
|
|
|
44 |
with gr.Column():
|
45 |
output_video = gr.Video(label="Generated Video")
|
46 |
|
47 |
btn = gr.Button("Generate Video")
|
48 |
-
btn.click(process_input, inputs=[text_input, txt_file_input, mp3_file_input, prompt_input], outputs=output_video)
|
49 |
|
50 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from tts_module import list_voices, text_to_speech
|
3 |
+
from pexels_api import search_pexels # Aseg煤rate de que este archivo est茅 correctamente configurado
|
4 |
+
from moviepy.editor import AudioFileClip, VideoFileClip, CompositeAudioClip
|
5 |
+
import asyncio
|
6 |
+
import os
|
7 |
+
import requests
|
8 |
+
import tempfile
|
9 |
+
|
10 |
+
# Obtener lista de voces disponibles
|
11 |
+
async def get_voices():
|
12 |
+
voices = await list_voices()
|
13 |
+
return list(voices.keys())
|
14 |
+
|
15 |
+
# Ajustar m煤sica de fondo
|
16 |
+
def adjust_background_music(video_duration, music_file):
|
17 |
+
from moviepy.editor import AudioFileClip, concatenate_audioclips
|
18 |
+
music = AudioFileClip(music_file)
|
19 |
+
if music.duration < video_duration:
|
20 |
+
repetitions = int(video_duration / music.duration) + 1
|
21 |
+
music_clips = [music] * repetitions
|
22 |
+
music = concatenate_audioclips(music_clips)
|
23 |
+
if music.duration > video_duration:
|
24 |
+
music = music.subclip(0, video_duration)
|
25 |
+
music = music.volumex(0.2) # Reducir volumen al 20%
|
26 |
+
return music
|
27 |
+
|
28 |
+
# Combinar audio, video y m煤sica
|
29 |
+
def combine_audio_video(audio_file, video_clip, music_clip=None):
|
30 |
+
audio_clip = AudioFileClip(audio_file)
|
31 |
+
final_clip = video_clip.set_audio(audio_clip)
|
32 |
+
|
33 |
+
if music_clip:
|
34 |
+
final_clip = final_clip.set_audio(CompositeAudioClip([audio_clip, music_clip]))
|
35 |
+
|
36 |
+
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
37 |
+
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac")
|
38 |
+
return output_path
|
39 |
+
|
40 |
+
# Funci贸n principal
|
41 |
+
def process_input(text, txt_file, mp3_file, prompt, selected_voice):
|
42 |
+
try:
|
43 |
+
# Paso 1: Obtener el texto final
|
44 |
+
if text.strip(): # Prioridad al texto escrito directamente
|
45 |
+
final_text = text
|
46 |
+
elif txt_file is not None: # Luego al archivo .txt
|
47 |
+
final_text = txt_file.decode("utf-8")
|
48 |
+
elif prompt.strip(): # Finalmente, usar el prompt para generar texto
|
49 |
+
final_text = "Generated text from prompt" # Placeholder
|
50 |
+
else:
|
51 |
+
return "No input provided", None
|
52 |
+
|
53 |
+
# Paso 2: Convertir texto a voz con edge_tts
|
54 |
+
audio_file = asyncio.run(text_to_speech(final_text, selected_voice))
|
55 |
+
|
56 |
+
# Paso 3: Buscar videos en Pexels
|
57 |
+
query = final_text.split(".")[0] # Usar la primera oraci贸n como consulta
|
58 |
+
try:
|
59 |
+
video_links = search_pexels(query, num_results=1)
|
60 |
+
if not video_links:
|
61 |
+
return "No se encontraron videos en Pexels", None
|
62 |
+
except Exception as e:
|
63 |
+
return f"Error al buscar videos en Pexels: {e}", None
|
64 |
+
|
65 |
+
# Descargar el primer video encontrado
|
66 |
+
video_response = requests.get(video_links[0])
|
67 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
|
68 |
+
tmp_video.write(video_response.content)
|
69 |
+
video_clip = VideoFileClip(tmp_video.name)
|
70 |
+
|
71 |
+
# Paso 4: Ajustar la m煤sica de fondo
|
72 |
+
if mp3_file is not None:
|
73 |
+
music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
|
74 |
+
else:
|
75 |
+
music_clip = None
|
76 |
+
|
77 |
+
# Paso 5: Combinar todos los elementos
|
78 |
+
final_video = combine_audio_video(audio_file, video_clip, music_clip)
|
79 |
+
|
80 |
+
return final_video
|
81 |
+
|
82 |
+
except Exception as e:
|
83 |
+
return f"Error durante el procesamiento: {e}", None
|
84 |
|
85 |
# Interfaz Gradio
|
86 |
with gr.Blocks() as demo:
|
|
|
91 |
txt_file_input = gr.File(label="Or upload a .txt file", file_types=[".txt"])
|
92 |
mp3_file_input = gr.File(label="Upload background music (.mp3)", file_types=[".mp3"])
|
93 |
prompt_input = gr.Textbox(label="Or enter a prompt to generate text")
|
94 |
+
voices = asyncio.run(get_voices())
|
95 |
+
voice_dropdown = gr.Dropdown(choices=voices, label="Select Voice")
|
96 |
+
|
97 |
with gr.Column():
|
98 |
output_video = gr.Video(label="Generated Video")
|
99 |
|
100 |
btn = gr.Button("Generate Video")
|
101 |
+
btn.click(process_input, inputs=[text_input, txt_file_input, mp3_file_input, prompt_input, voice_dropdown], outputs=output_video)
|
102 |
|
103 |
demo.launch()
|