Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from moviepy.editor import TextClip, CompositeVideoClip, AudioFileClip, ColorClip
|
3 |
+
from gtts import gTTS
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Function to convert script to video
|
7 |
+
def script_to_video(script, background_color="black", text_color="white", font_size=50):
|
8 |
+
# Step 1: Convert script to audio using gTTS
|
9 |
+
tts = gTTS(script)
|
10 |
+
audio_file = "output_audio.mp3"
|
11 |
+
tts.save(audio_file)
|
12 |
+
|
13 |
+
# Step 2: Load the audio file
|
14 |
+
audio_clip = AudioFileClip(audio_file)
|
15 |
+
audio_duration = audio_clip.duration
|
16 |
+
|
17 |
+
# Step 3: Create a text clip with the script
|
18 |
+
text_clip = TextClip(
|
19 |
+
script,
|
20 |
+
fontsize=font_size,
|
21 |
+
color=text_color,
|
22 |
+
size=(1280, 720), # HD resolution
|
23 |
+
method="caption", # Automatically wrap text
|
24 |
+
align="center",
|
25 |
+
bg_color=background_color
|
26 |
+
).set_duration(audio_duration)
|
27 |
+
|
28 |
+
# Step 4: Create a background clip
|
29 |
+
background_clip = ColorClip(
|
30 |
+
size=(1280, 720),
|
31 |
+
color=[int(background_color.lstrip("#")[i:i+2], 16) for i in (0, 2, 4)] # Convert hex to RGB
|
32 |
+
).set_duration(audio_duration)
|
33 |
+
|
34 |
+
# Step 5: Combine text and background
|
35 |
+
video_clip = CompositeVideoClip([background_clip, text_clip.set_position("center")])
|
36 |
+
|
37 |
+
# Step 6: Add audio to the video
|
38 |
+
video_clip = video_clip.set_audio(audio_clip)
|
39 |
+
|
40 |
+
# Step 7: Save the video
|
41 |
+
video_file = "output_video.mp4"
|
42 |
+
video_clip.write_videofile(video_file, fps=24)
|
43 |
+
|
44 |
+
# Clean up temporary audio file
|
45 |
+
os.remove(audio_file)
|
46 |
+
|
47 |
+
return video_file
|
48 |
+
|
49 |
+
# Gradio Interface
|
50 |
+
iface = gr.Interface(
|
51 |
+
fn=script_to_video,
|
52 |
+
inputs=[
|
53 |
+
gr.Textbox(label="Enter your script", lines=10, placeholder="Type your script here..."),
|
54 |
+
gr.ColorPicker(label="Background Color", value="#000000"),
|
55 |
+
gr.ColorPicker(label="Text Color", value="#FFFFFF"),
|
56 |
+
gr.Slider(label="Font Size", minimum=20, maximum=100, value=50)
|
57 |
+
],
|
58 |
+
outputs=gr.Video(label="Generated Video"),
|
59 |
+
title="Script to Video Generator",
|
60 |
+
description="Convert your script into a video with text and audio."
|
61 |
+
)
|
62 |
+
|
63 |
+
# Launch the application
|
64 |
+
iface.launch()
|