gnosticdev's picture
Update app.py
bdb3cfa verified
raw
history blame
10.7 kB
import gradio as gr
from tts_module import get_voices, text_to_speech
from pixabay_api import search_pixabay
from moviepy.editor import (
AudioFileClip, VideoFileClip, CompositeAudioClip,
concatenate_audioclips, concatenate_videoclips, vfx, CompositeVideoClip,
ColorClip, ImageClip
)
import asyncio
import os
import json
import time
import requests
import tempfile
import re
import random
from google_drive_upload import authenticate_google_drive, upload_to_google_drive
from io import BytesIO
from PIL import Image
import numpy as np
# Crear el archivo de credenciales de servicio desde los secretos
service_account_info = json.loads(os.getenv('GOOGLE_SERVICE_ACCOUNT', '{}'))
if service_account_info:
with open('service-account.json', 'w') as f:
json.dump(service_account_info, f)
# Define la carpeta de salida
output_folder = "outputs"
os.makedirs(output_folder, exist_ok=True)
# ID de la carpeta de destino en Google Drive
FOLDER_ID = "12S6adpanAXjf71pKKGRRPqpzbJa5XEh3" # Reemplaza con tu ID real
def resize_and_add_background(clip, target_width=1920, target_height=1080, background_url="https://wallpaperaccess.com/full/231401.jpg"):
"""
Redimensiona el video al tamaño 1080p (16:9) y añade una imagen de fondo descargada desde una URL.
"""
try:
w, h = clip.size
current_aspect_ratio = w / h
target_aspect_ratio = target_width / target_height
# Descargar la imagen de fondo desde la URL
response = requests.get(background_url)
if response.status_code != 200:
print("Error al descargar la imagen de fondo, usando un fondo negro.")
background = (
ImageClip(
np.zeros((target_height, target_width, 3), dtype=np.uint8),
duration=clip.duration,
)
.set_duration(clip.duration)
.resize((target_width, target_height))
)
else:
image_data = Image.open(BytesIO(response.content))
image_array = np.array(image_data)
background = (
ImageClip(image_array)
.set_duration(clip.duration)
.resize((target_width, target_height))
)
# Si el video ya tiene una relación de aspecto cercana a 16:9, solo redimensionarlo
if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
resized_clip = clip.resize((target_width, target_height))
else:
# Redimensionar el video manteniendo su proporción y centrarlo sobre el fondo
if current_aspect_ratio < target_aspect_ratio: # Video vertical
new_height = target_height
new_width = int(new_height * current_aspect_ratio)
x_center = (target_width - new_width) / 2
resized_clip = clip.resize(width=new_width).set_position((x_center, 0))
else: # Video horizontal
new_width = target_width
new_height = int(new_width / current_aspect_ratio)
y_center = (target_height - new_height) / 2
resized_clip = clip.resize(height=new_height).set_position((0, y_center))
# Combinar el fondo con el video redimensionado
return CompositeVideoClip([background, resized_clip], size=(target_width, target_height))
except Exception as e:
print(f"Error en resize_and_add_background: {e}")
return clip
def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
"""Concatena videos de Pixabay basados en palabras clave."""
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
if not keyword_list:
keyword_list = ["nature"] # Palabra clave por defecto
video_clips = []
for keyword in keyword_list:
try:
print(f"Buscando videos para la palabra clave '{keyword}'...")
links = search_pixabay(keyword, num_results=num_videos_per_keyword)
if not links:
print(f"No se encontraron videos para '{keyword}', probando con 'nature'")
links = search_pixabay("nature", num_results=num_videos_per_keyword)
if not links:
continue
link = links[0]
video_response = requests.get(link)
if video_response.status_code != 200:
print(f"Error al descargar video desde {link}")
continue
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as tmp_video:
tmp_video.write(video_response.content)
clip = VideoFileClip(tmp_video.name)
processed_clip = resize_and_add_background(clip)
video_clips.append(processed_clip)
os.unlink(tmp_video.name) # Limpiamos el archivo temporal
except Exception as e:
print(f"Error procesando palabra clave '{keyword}': {e}")
continue
if not video_clips:
# Si no hay videos, creamos un clip negro de 5 segundos
return ColorClip(size=(1920, 1080), color=[0, 0, 0], duration=5)
random.shuffle(video_clips)
return concatenate_videoclips(video_clips, method="compose")
def adjust_background_music(video_duration, music_file):
"""Ajusta la música de fondo para que coincida con la duración del video."""
try:
music = AudioFileClip(music_file)
if music.duration < video_duration:
repetitions = int(video_duration / music.duration) + 1
music_clips = [music] * repetitions
music = concatenate_audioclips(music_clips)
music = music.subclip(0, video_duration)
return music.volumex(0.2)
except Exception as e:
print(f"Error ajustando música: {e}")
return None
def combine_audio_video(audio_file, video_clip, music_clip=None):
"""Combina el audio y el video en un archivo final."""
try:
audio_clip = AudioFileClip(audio_file)
total_duration = audio_clip.duration + 2 # Añadimos 2 segundos extra
# Aseguramos que el video tenga la duración correcta
video_clip = video_clip.loop(duration=total_duration)
video_clip = video_clip.set_duration(total_duration).fadeout(2)
# Combinamos el audio principal
final_clip = video_clip.set_audio(audio_clip)
# Añadimos la música de fondo si existe
if music_clip:
music_clip = music_clip.set_duration(total_duration).audio_fadeout(2)
final_clip = final_clip.set_audio(CompositeAudioClip([audio_clip, music_clip]))
# Generamos el nombre del archivo y la ruta
output_filename = f"final_video_{int(time.time())}.mp4"
output_path = os.path.join(output_folder, output_filename)
# Guardamos el video
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=24)
# Limpiamos los clips
final_clip.close()
video_clip.close()
audio_clip.close()
if music_clip:
music_clip.close()
return output_path
except Exception as e:
print(f"Error combinando audio y video: {e}")
if 'final_clip' in locals():
final_clip.close()
return None
def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
"""Procesa la entrada del usuario y genera el video final."""
try:
# Determinamos el texto a usar
if text.strip():
final_text = text
elif txt_file is not None:
final_text = txt_file.decode("utf-8")
else:
raise ValueError("No text input provided")
# Generamos el audio
audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
if not audio_file:
raise ValueError("Failed to generate audio")
# Generamos el video
video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
if not video_clip:
raise ValueError("Failed to generate video")
# Procesamos la música de fondo si existe
music_clip = None
if mp3_file is not None:
music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
# Combinamos todo
final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
if not final_video_path:
raise ValueError("Failed to combine audio and video")
# Subimos a Google Drive
video_id = upload_to_google_drive(final_video_path, folder_id=FOLDER_ID)
if video_id:
print(f"Video subido a Google Drive con ID: {video_id}")
download_link = f"https://drive.google.com/file/d/{video_id}/view?usp=sharing"
return download_link # Devuelve el enlace compartido
else:
print("Error subiendo el video a Google Drive")
return "Error al subir el video"
except Exception as e:
print(f"Error durante el procesamiento: {e}")
return f"Error durante el procesamiento: {e}"
# Interfaz Gradio
with gr.Blocks() as demo:
gr.Markdown("# Text-to-Video Generator")
with gr.Row():
with gr.Column():
text_input = gr.Textbox(label="Write your text here", lines=5)
txt_file_input = gr.File(label="Or upload a .txt file", file_types=[".txt"])
mp3_file_input = gr.File(label="Upload background music (.mp3)", file_types=[".mp3"])
keyword_input = gr.Textbox(
label="Enter keywords separated by commas (e.g., universe, galaxy, forest, cat)",
value="space, espacio,galaxy,moon,fear,astral,god,evil,mistery,cosmos,stars"
)
voices = asyncio.run(get_voices())
voice_dropdown = gr.Dropdown(choices=list(voices.keys()), label="Select Voice")
rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1)
pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
with gr.Column():
output_video = gr.Textbox(label="Download Link") # Mostramos el enlace de descarga
btn = gr.Button("Generate Video")
btn.click(
process_input,
inputs=[text_input, txt_file_input, mp3_file_input, voice_dropdown, rate_slider, pitch_slider, keyword_input],
outputs=output_video
)
# Leer el puerto asignado por Hugging Face
port = int(os.getenv("PORT", 7860))
# Lanzar la aplicación
demo.launch(server_name="0.0.0.0", server_port=port, share=True, show_error=True)