|
import gradio as gr |
|
from PIL import Image, ImageDraw, ImageFont |
|
from gtts import gTTS |
|
from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips |
|
import textwrap |
|
import os |
|
|
|
def create_video_from_text(text): |
|
lines = text.split('\n') |
|
|
|
clips = [] |
|
for line in lines: |
|
|
|
img = Image.new('RGB', (1920, 1080), color = (73, 109, 137)) |
|
d = ImageDraw.Draw(img) |
|
fnt = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 50) |
|
|
|
wrapped_text = textwrap.wrap(line, width=20) |
|
y_text=50 |
|
for line1 in wrapped_text: |
|
bbox = d.textbbox((0, 0), line1, font=fnt) |
|
textwidth, textheight = bbox[2], bbox[3] |
|
x= (img.width - textwidth) /2 |
|
d.text((x, y_text), line1, font=fnt, fill=(255, 255, 255)) |
|
y_text += textheight |
|
img.save('text.png') |
|
|
|
|
|
speech = gTTS(text=line, lang='en', slow=False) |
|
speech.save("text.mp3") |
|
|
|
|
|
clip = ImageClip('text.png') |
|
|
|
|
|
audioclip = AudioFileClip('text.mp3') |
|
videoclip = clip.set_duration(audioclip.duration) |
|
|
|
|
|
videoclip = videoclip.set_audio(audioclip) |
|
|
|
clips.append(videoclip) |
|
|
|
|
|
final_clip = concatenate_videoclips(clips) |
|
|
|
|
|
final_clip.write_videofile("text.mp4", codec='libx264',fps=24) |
|
os.remove("text.png") |
|
os.remove("text.mp3") |
|
return "text.mp4" |
|
|
|
iface = gr.Interface(fn=create_video_from_text, inputs="text", outputs=gr.Video()) |
|
if __name__ == "__main__": |
|
iface.launch() |
|
|