|
|
|
|
|
import gradio as gr |
|
import sounddevice as sd |
|
from scipy.io.wavfile import write |
|
import tempfile |
|
import shutil |
|
import os |
|
|
|
|
|
AUDIO_COPY_PATH = os.path.abspath(os.path.join("..", "miwav2lipv6","assets", "audio", "grabacion_gradio.wav")) |
|
|
|
VIDEO_PATH = os.path.abspath("../miwav2lipv6/assets/video/data_video_sun.mp4") |
|
|
|
|
|
if not os.path.exists(VIDEO_PATH): |
|
print(f"Advertencia: El archivo de video no se encontró en la ruta {VIDEO_PATH}") |
|
|
|
|
|
def grabar_audio(duration=8, sample_rate=44100): |
|
print("Grabando...") |
|
audio_data = sd.rec(int(duration * sample_rate), samplerate=sample_rate, channels=1) |
|
sd.wait() |
|
|
|
|
|
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".wav") |
|
write(temp_audio.name, sample_rate, audio_data) |
|
print("Grabación completada. Archivo temporal guardado en:", temp_audio.name) |
|
|
|
|
|
os.makedirs(os.path.dirname(AUDIO_COPY_PATH), exist_ok=True) |
|
|
|
|
|
shutil.copy(temp_audio.name, AUDIO_COPY_PATH) |
|
print(f"Copia de la grabación guardada en: {AUDIO_COPY_PATH}") |
|
|
|
return AUDIO_COPY_PATH |
|
|
|
|
|
def interfaz(): |
|
with gr.Blocks() as demo: |
|
gr.Video(VIDEO_PATH, loop=True, autoplay=True, height=300, width=500) |
|
|
|
|
|
with gr.Row(): |
|
grabar_button = gr.Button("Iniciar Grabación") |
|
|
|
|
|
output_audio = gr.Audio(label="Grabación de Audio", type="filepath") |
|
|
|
|
|
grabar_button.click(grabar_audio, outputs=output_audio) |
|
|
|
return demo |
|
|
|
|
|
if __name__ == "__main__": |
|
demo = interfaz() |
|
demo.launch(allowed_paths=[os.path.dirname(AUDIO_COPY_PATH)]) |
|
|
|
|