File size: 8,057 Bytes
f93cca7 e2aa189 aef08e4 4d60f7e 0b2cd2e 865ab31 4d60f7e f93cca7 fb7a495 ec0a35b e2aa189 0b2cd2e 865ab31 f93cca7 ec0a35b e2aa189 ec0a35b 0b2cd2e 865ab31 4d60f7e de0afc9 0b2cd2e de0afc9 aef08e4 de0afc9 0b2cd2e aef08e4 de0afc9 865ab31 0b2cd2e 4d60f7e aef08e4 865ab31 de0afc9 865ab31 0b2cd2e 865ab31 d4dab54 aef08e4 0b2cd2e de0afc9 aef08e4 de0afc9 0b2cd2e 865ab31 de0afc9 865ab31 d4dab54 865ab31 d4dab54 865ab31 0b2cd2e de0afc9 865ab31 de0afc9 0b2cd2e de0afc9 0b2cd2e de0afc9 d4dab54 f93cca7 0b2cd2e 865ab31 0b2cd2e 865ab31 0b2cd2e 865ab31 0b2cd2e 6b4aac3 865ab31 f93cca7 123b465 865ab31 f93cca7 123b465 865ab31 4d60f7e 123b465 865ab31 4d60f7e aef08e4 4d60f7e 123b465 865ab31 f93cca7 865ab31 ec0a35b 6b4aac3 de0afc9 865ab31 f93cca7 123b465 f93cca7 0b2cd2e 865ab31 0b2cd2e f93cca7 865ab31 e2aa189 f93cca7 e2aa189 865ab31 f93cca7 865ab31 f93cca7 e2aa189 9c62553 |
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 |
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
)
import asyncio
import os
import time
import requests
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
import tempfile
import re
import random
output_folder = "outputs"
os.makedirs(output_folder, exist_ok=True)
def clean_text_for_search(text):
text = re.sub(r'[^\w\s]', '', text).strip()
return text
def resize_and_blur_video(clip, target_aspect_ratio=16/9):
try:
w, h = clip.size
current_aspect_ratio = w / h
if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
return clip
if current_aspect_ratio < target_aspect_ratio:
target_w = int(h * target_aspect_ratio)
target_h = h
background = clip.resize(width=target_w)
try:
background = background.fx(vfx.blur, sigma=50)
except Exception as e:
print(f"Error al aplicar blur: {e}")
foreground = clip.resize(height=target_h)
x_center = (target_w - foreground.w) / 2
return CompositeVideoClip(
[background, foreground.set_position((x_center, 0))],
size=(target_w, target_h)
)
else:
return clip.resize(width=int(h * target_aspect_ratio), height=h)
except Exception as e:
print(f"Error en resize_and_blur_video: {e}")
return clip
def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
if not keyword_list:
raise Exception("No se proporcionaron palabras clave válidas.")
video_clips = []
for keyword in keyword_list:
try:
links = search_pixabay(keyword, num_results=num_videos_per_keyword)
if not links:
print(f"No se encontraron videos para la palabra clave '{keyword}'.")
continue
link = links[0]
video_response = requests.get(link)
if video_response.status_code != 200:
print(f"Error al descargar video desde {link}: Código de estado {video_response.status_code}")
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_blur_video(clip)
video_clips.append(processed_clip)
except Exception as e:
print(f"Error procesando palabra clave '{keyword}': {e}")
continue
if not video_clips:
raise Exception("No se pudieron obtener videos válidos.")
random.shuffle(video_clips)
return concatenate_videoclips(video_clips, method="compose")
def adjust_background_music(video_duration, music_file):
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)
if music.duration > video_duration:
music = music.subclip(0, video_duration)
music = music.volumex(0.2)
return music
except Exception as e:
print(f"Error ajustando música: {e}")
return None
def combine_audio_video(audio_file, video_clip, music_clip=None):
try:
audio_clip = AudioFileClip(audio_file)
total_duration = audio_clip.duration + 5
if video_clip.duration < total_duration:
video_clip = video_clip.loop(duration=total_duration)
video_clip = video_clip.set_duration(total_duration).fadeout(5)
final_clip = video_clip.set_audio(audio_clip)
if music_clip:
if music_clip.duration < total_duration:
repetitions = int(total_duration / music_clip.duration) + 1
music_clips = [music_clip] * repetitions
music_clip = concatenate_audioclips(music_clips)
if music_clip.duration > total_duration:
music_clip = music_clip.subclip(0, total_duration)
music_clip = music_clip.audio_fadeout(5)
final_clip = final_clip.set_audio(CompositeAudioClip([audio_clip, music_clip]))
output_filename = f"final_video_{int(time.time())}.mp4"
output_path = os.path.join(output_folder, output_filename)
final_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=24)
return output_path
except Exception as e:
print(f"Error combinando audio y video: {e}")
return None
def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keywords):
try:
if text.strip():
final_text = text
elif txt_file is not None:
final_text = txt_file.decode("utf-8")
else:
return None
voices = asyncio.run(get_voices())
if selected_voice not in voices:
return None
try:
audio_file = asyncio.run(text_to_speech(final_text, selected_voice, rate, pitch))
except Exception as e:
return None
try:
video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
except Exception as e:
return None
if mp3_file is not None:
music_clip = adjust_background_music(video_clip.duration, mp3_file.name)
else:
music_clip = None
final_video_path = combine_audio_video(audio_file, video_clip, music_clip)
upload_to_google_drive(final_video_path)
return final_video_path
except Exception as e:
return None
def upload_to_google_drive(file_path):
try:
api_key = os.getenv("GOOGLE_API_KEY")
if not api_key:
print("Error: GOOGLE_API_KEY no está definida en las variables de entorno.")
return None
service = build("drive", "v3", developerKey=api_key)
file_metadata = {"name": os.path.basename(file_path)}
media = MediaFileUpload(file_path, resumable=True)
file = service.files().create(body=file_metadata, media_body=media, fields="id").execute()
print(f"Archivo subido exitosamente con ID: {file.get('id')}")
return file.get("id")
except Exception as e:
print(f"Error subiendo a Google Drive: {e}")
return None
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)")
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.File(label="Download Generated Video")
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
)
port = int(os.getenv("PORT", 7860))
demo.launch(server_name="0.0.0.0", server_port=port, share=True) |