venkatl commited on
Commit
6b885dc
1 Parent(s): b0c3615

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image, ImageDraw, ImageFont
3
+ from gtts import gTTS
4
+ from moviepy.editor import ImageClip, AudioFileClip, concatenate_videoclips
5
+ import textwrap
6
+ import os
7
+
8
+ def create_video_from_text(text):
9
+ lines = text.split('\n')
10
+
11
+ clips = []
12
+ for line in lines:
13
+ # Create an image with text
14
+ img = Image.new('RGB', (1920, 1080), color = (73, 109, 137)) # Professional color background
15
+ d = ImageDraw.Draw(img)
16
+ fnt = ImageFont.truetype('/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf', 50)
17
+
18
+ wrapped_text = textwrap.wrap(line, width=20) # Adjust the width parameter as needed
19
+ y_text=50
20
+ for line1 in wrapped_text:
21
+ bbox = d.textbbox((0, 0), line1, font=fnt)
22
+ textwidth, textheight = bbox[2], bbox[3]
23
+ x= (img.width - textwidth) /2
24
+ d.text((x, y_text), line1, font=fnt, fill=(255, 255, 255)) # White text color
25
+ y_text += textheight
26
+ img.save('text.png')
27
+
28
+ # Convert text to speech
29
+ speech = gTTS(text=line, lang='en', slow=False)
30
+ speech.save("text.mp3")
31
+
32
+ # Create a video clip from the image
33
+ clip = ImageClip('text.png')
34
+
35
+ # Set the duration of the video clip to the duration of the audio file
36
+ audioclip = AudioFileClip('text.mp3')
37
+ videoclip = clip.set_duration(audioclip.duration)
38
+
39
+ # Add audio to the video clip
40
+ videoclip = videoclip.set_audio(audioclip)
41
+
42
+ clips.append(videoclip)
43
+
44
+ # Concatenate all video clips
45
+ final_clip = concatenate_videoclips(clips)
46
+
47
+ # Write the result to a file
48
+ final_clip.write_videofile("text.mp4", codec='libx264',fps=24)
49
+ os.remove("text.png")
50
+ os.remove("text.mp3")
51
+ return "text.mp4"
52
+
53
+ iface = gr.Interface(fn=create_video_from_text, inputs="text", outputs=gr.Video())
54
+ iface.launch()