Spaces:
Building
Building
File size: 10,740 Bytes
c94aadc 0a28283 c94aadc 9270419 bdb3cfa c94aadc faac2b8 bdb3cfa c94aadc 0a28283 f93cca7 c94aadc 24d97f6 0a28283 bdb3cfa 0a28283 bdb3cfa 0a28283 24d97f6 0a28283 24d97f6 c94aadc 0a28283 c94aadc 9ea2009 0a28283 c94aadc 9ea2009 c94aadc bdb3cfa c94aadc 9ea2009 0a28283 c94aadc 9ea2009 0a28283 c94aadc 9ea2009 0a28283 c94aadc bdb3cfa c94aadc bdb3cfa c94aadc bdb3cfa c94aadc bdb3cfa c94aadc bdb3cfa c94aadc 941a8dd bdb3cfa 941a8dd bdb3cfa 941a8dd bdb3cfa c94aadc 0a28283 c94aadc bdb3cfa c94aadc 1bac256 c94aadc bdb3cfa c94aadc f27d42e |
1 2 3 4 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
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) |