Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| from moviepy.editor import TextClip, CompositeVideoClip, AudioFileClip, ColorClip | |
| from gtts import gTTS | |
| import os | |
| # Function to convert script to video | |
| def script_to_video(script, background_color="black", text_color="white", font_size=50): | |
| # Step 1: Convert script to audio using gTTS | |
| tts = gTTS(script) | |
| audio_file = "output_audio.mp3" | |
| tts.save(audio_file) | |
| # Step 2: Load the audio file | |
| audio_clip = AudioFileClip(audio_file) | |
| audio_duration = audio_clip.duration | |
| # Step 3: Create a text clip with the script | |
| text_clip = TextClip( | |
| script, | |
| fontsize=font_size, | |
| color=text_color, | |
| size=(1280, 720), # HD resolution | |
| method="caption", # Automatically wrap text | |
| align="center", | |
| bg_color=background_color | |
| ).set_duration(audio_duration) | |
| # Step 4: Create a background clip | |
| background_clip = ColorClip( | |
| size=(1280, 720), | |
| color=[int(background_color.lstrip("#")[i:i+2], 16) for i in (0, 2, 4)] # Convert hex to RGB | |
| ).set_duration(audio_duration) | |
| # Step 5: Combine text and background | |
| video_clip = CompositeVideoClip([background_clip, text_clip.set_position("center")]) | |
| # Step 6: Add audio to the video | |
| video_clip = video_clip.set_audio(audio_clip) | |
| # Step 7: Save the video | |
| video_file = "output_video.mp4" | |
| video_clip.write_videofile(video_file, fps=24) | |
| # Clean up temporary audio file | |
| os.remove(audio_file) | |
| return video_file | |
| # Gradio Interface | |
| iface = gr.Interface( | |
| fn=script_to_video, | |
| inputs=[ | |
| gr.Textbox(label="Enter your script", lines=10, placeholder="Type your script here..."), | |
| gr.ColorPicker(label="Background Color", value="#000000"), | |
| gr.ColorPicker(label="Text Color", value="#FFFFFF"), | |
| gr.Slider(label="Font Size", minimum=20, maximum=100, value=50) | |
| ], | |
| outputs=gr.Video(label="Generated Video"), | |
| title="Script to Video Generator", | |
| description="Convert your script into a video with text and audio." | |
| ) | |
| # Launch the application | |
| iface.launch() |