Spaces:
Sleeping
Sleeping
prithivMLmods
commited on
Commit
•
7b5d347
1
Parent(s):
a44d7cb
Update app.py
Browse files
app.py
CHANGED
@@ -1,265 +1,272 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
from PIL import Image, ImageDraw, ImageFont
|
5 |
-
import textwrap
|
6 |
-
import moviepy.editor as mp
|
7 |
-
import moviepy.video.fx.all as vfx
|
8 |
-
|
9 |
-
css = """
|
10 |
-
#col-container {
|
11 |
-
margin: 0 auto;
|
12 |
-
max-width: 290px;
|
13 |
-
}
|
14 |
-
"""
|
15 |
-
|
16 |
-
def create_typing_video(code_text, format_choice, line_spacing, width_choice, height_choice, font_name="arial.ttf", font_size=18, frame_rate=10, sound_choice=None, custom_audio=None, background_color="black", text_color="white", enhance_quality=False, video_speed="1.0"):
|
17 |
-
font_path = f"font/{font_name}"
|
18 |
-
|
19 |
-
# Convert font_size to integer
|
20 |
-
font_size = int(font_size)
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
image_width
|
27 |
-
|
28 |
-
current_text = ""
|
29 |
-
|
30 |
-
# Create the background
|
31 |
-
background = Image.new("RGB", (image_width, image_height), color=background_color)
|
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 |
-
audio = mp.
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
video = video.
|
115 |
-
video.write_videofile("
|
116 |
-
video_filename = "
|
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 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
import textwrap
|
6 |
+
import moviepy.editor as mp
|
7 |
+
import moviepy.video.fx.all as vfx
|
8 |
+
|
9 |
+
css = """
|
10 |
+
#col-container {
|
11 |
+
margin: 0 auto;
|
12 |
+
max-width: 290px;
|
13 |
+
}
|
14 |
+
"""
|
15 |
+
|
16 |
+
def create_typing_video(code_text, format_choice, line_spacing, width_choice, height_choice, font_name="arial.ttf", font_size=18, frame_rate=10, sound_choice=None, custom_audio=None, background_color="black", text_color="white", enhance_quality=False, video_speed="1.0"):
|
17 |
+
font_path = f"font/{font_name}"
|
18 |
+
|
19 |
+
# Convert font_size to integer
|
20 |
+
font_size = int(font_size)
|
21 |
+
|
22 |
+
video_frames = []
|
23 |
+
|
24 |
+
# Setup initial parameters
|
25 |
+
image_width, image_height = int(width_choice), int(height_choice)
|
26 |
+
max_width = image_width - 40 # Margin of 20 pixels on each side
|
27 |
+
max_height = image_height - 40 # Margin of 20 pixels on top and bottom
|
28 |
+
current_text = ""
|
29 |
+
|
30 |
+
# Create the background
|
31 |
+
background = Image.new("RGB", (image_width, image_height), color=background_color)
|
32 |
+
|
33 |
+
# Load and adjust the font
|
34 |
+
font = ImageFont.truetype(font_path, font_size)
|
35 |
+
|
36 |
+
# Adjust font size if necessary to fit text within image height
|
37 |
+
def adjust_font_size(text, font, max_width, max_height):
|
38 |
+
while True:
|
39 |
+
wrapped_lines = textwrap.wrap(text, width=max_width // font.getsize(' ')[0])
|
40 |
+
text_height = sum(font.getsize(line)[1] for line in wrapped_lines)
|
41 |
+
|
42 |
+
if text_height <= max_height:
|
43 |
+
return font
|
44 |
+
|
45 |
+
# Reduce font size
|
46 |
+
font_size -= 1
|
47 |
+
font = ImageFont.truetype(font_path, font_size)
|
48 |
+
|
49 |
+
font = adjust_font_size(code_text, font, max_width, max_height)
|
50 |
+
|
51 |
+
# Generate frames for the typing effect
|
52 |
+
for char in code_text:
|
53 |
+
current_text += char
|
54 |
+
|
55 |
+
if format_choice == "Paragraph":
|
56 |
+
wrapped_lines = textwrap.wrap(current_text, width=max_width // font.getsize(' ')[0])
|
57 |
+
else: # Programming
|
58 |
+
wrapped_lines = current_text.splitlines()
|
59 |
+
|
60 |
+
# Copy the background image for each frame
|
61 |
+
image = background.copy()
|
62 |
+
draw = ImageDraw.Draw(image)
|
63 |
+
|
64 |
+
y_position = 20
|
65 |
+
for line in wrapped_lines:
|
66 |
+
draw.text((20, y_position), line, font=font, fill=text_color)
|
67 |
+
line_height = font.getsize(line)[1]
|
68 |
+
y_position += line_height * line_spacing
|
69 |
+
|
70 |
+
# Convert to numpy array for OpenCV
|
71 |
+
frame = np.array(image)
|
72 |
+
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
73 |
+
video_frames.append(frame)
|
74 |
+
|
75 |
+
# Create a video writer
|
76 |
+
video_filename = "typed_code_video.mp4"
|
77 |
+
out = cv2.VideoWriter(video_filename, cv2.VideoWriter_fourcc(*"mp4v"), frame_rate, (image_width, image_height))
|
78 |
+
|
79 |
+
for frame in video_frames:
|
80 |
+
out.write(frame)
|
81 |
+
out.release()
|
82 |
+
|
83 |
+
# Adjust video speed
|
84 |
+
speed_factor = {
|
85 |
+
"1x": 1.0,
|
86 |
+
"1.25x": 1.25,
|
87 |
+
"1.5x": 1.5,
|
88 |
+
"1.75x": 1.75,
|
89 |
+
"2x": 2.0
|
90 |
+
}.get(video_speed, 1.0) # Default to 1.0 if video_speed is not found in the dictionary
|
91 |
+
|
92 |
+
video = mp.VideoFileClip(video_filename).fx(vfx.speedx, factor=speed_factor)
|
93 |
+
video.write_videofile("speed_adjusted_video.mp4", codec="libx264")
|
94 |
+
video_filename = "speed_adjusted_video.mp4"
|
95 |
+
|
96 |
+
# Add sound if a sound choice is selected
|
97 |
+
if sound_choice and sound_choice != "No Sound":
|
98 |
+
video = mp.VideoFileClip(video_filename)
|
99 |
+
audio = mp.AudioFileClip(f"type-sounds/{sound_choice}")
|
100 |
+
|
101 |
+
# Loop the audio to match the duration of the video
|
102 |
+
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
|
103 |
+
video = video.set_audio(audio)
|
104 |
+
video.write_videofile("typed_code_video_with_sound.mp4", codec="libx264")
|
105 |
+
video_filename = "typed_code_video_with_sound.mp4"
|
106 |
+
|
107 |
+
# Add custom audio if provided
|
108 |
+
if custom_audio:
|
109 |
+
video = mp.VideoFileClip(video_filename)
|
110 |
+
audio = mp.AudioFileClip(custom_audio)
|
111 |
+
|
112 |
+
# Loop the custom audio to match the duration of the video
|
113 |
+
audio = audio.fx(mp.afx.audio_loop, duration=video.duration)
|
114 |
+
video = video.set_audio(audio)
|
115 |
+
video.write_videofile("typed_code_video_with_custom_audio.mp4", codec="libx264")
|
116 |
+
video_filename = "typed_code_video_with_custom_audio.mp4"
|
117 |
+
|
118 |
+
# Apply video quality enhancement if enabled
|
119 |
+
if enhance_quality:
|
120 |
+
video = mp.VideoFileClip(video_filename)
|
121 |
+
video = video.fx(vfx.resize, height=720) # Resize video to enhance quality
|
122 |
+
video = video.fx(vfx.colorx, 1.2) # Increase contrast
|
123 |
+
video.write_videofile("enhanced_" + video_filename, codec="libx264")
|
124 |
+
video_filename = "enhanced_" + video_filename
|
125 |
+
|
126 |
+
return video_filename
|
127 |
+
|
128 |
+
def generate_video(code_text, format_choice, line_spacing, width_choice, height_choice, font_choice, font_size, sound_choice, custom_audio, background_color, text_color, enhance_quality, video_speed):
|
129 |
+
return create_typing_video(code_text, format_choice, line_spacing, width_choice, height_choice, font_name=font_choice, font_size=font_size, sound_choice=sound_choice, custom_audio=custom_audio, background_color=background_color, text_color=text_color, enhance_quality=enhance_quality, video_speed=video_speed)
|
130 |
+
|
131 |
+
# Create Gradio interface
|
132 |
+
format_choice = gr.Dropdown(
|
133 |
+
choices=["Paragraph", "Programming"],
|
134 |
+
value="Paragraph",
|
135 |
+
label="Text Format"
|
136 |
+
)
|
137 |
+
|
138 |
+
line_spacing = gr.Dropdown(
|
139 |
+
choices=[1.0, 1.15, 1.5, 2.0, 2.5, 3.0],
|
140 |
+
value=1.15,
|
141 |
+
label="Line Spacing"
|
142 |
+
)
|
143 |
+
|
144 |
+
font_choice = gr.Dropdown(
|
145 |
+
choices=[
|
146 |
+
"DejaVuMathTeXGyre.ttf",
|
147 |
+
"FiraCode-Medium.ttf",
|
148 |
+
"InputMono-Light.ttf",
|
149 |
+
"JetBrainsMono-Thin.ttf",
|
150 |
+
"ProggyCrossed Regular Mac.ttf",
|
151 |
+
"SourceCodePro-Black.ttf",
|
152 |
+
"arial.ttf",
|
153 |
+
"calibri.ttf",
|
154 |
+
"mukta-malar-extralight.ttf",
|
155 |
+
"noto-sans-arabic-medium.ttf",
|
156 |
+
"times new roman.ttf",
|
157 |
+
"ANGSA.ttf",
|
158 |
+
"Book-Antiqua.ttf",
|
159 |
+
"CONSOLA.TTF",
|
160 |
+
"COOPBL.TTF",
|
161 |
+
"Rockwell-Bold.ttf",
|
162 |
+
"Candara Light.TTF",
|
163 |
+
"Carlito-Regular.ttf",
|
164 |
+
"Castellar.ttf",
|
165 |
+
"Courier New.ttf",
|
166 |
+
"LSANS.TTF",
|
167 |
+
"Lucida Bright Regular.ttf",
|
168 |
+
"TRTempusSansITC.ttf",
|
169 |
+
"Verdana.ttf",
|
170 |
+
"bell-mt.ttf",
|
171 |
+
"eras-itc-light.ttf",
|
172 |
+
"fonnts.com-aptos-light.ttf",
|
173 |
+
"georgia.ttf",
|
174 |
+
"segoeuithis.ttf",
|
175 |
+
"youyuan.TTF",
|
176 |
+
"TfPonetoneExpanded-7BJZA.ttf",
|
177 |
+
],
|
178 |
+
value="SourceCodePro-Black.ttf",
|
179 |
+
label="Select Font"
|
180 |
+
)
|
181 |
+
|
182 |
+
font_size = gr.Dropdown(
|
183 |
+
choices=["16", "18", "20", "22", "24"],
|
184 |
+
value="18",
|
185 |
+
label="Font Size"
|
186 |
+
)
|
187 |
+
|
188 |
+
width_choice = gr.Dropdown(
|
189 |
+
choices=["400","800", "1024", "1280", "1920"],
|
190 |
+
value="800",
|
191 |
+
label="Video Width"
|
192 |
+
)
|
193 |
+
|
194 |
+
height_choice = gr.Dropdown(
|
195 |
+
choices=["400", "720", "1080", "1440", "2160"],
|
196 |
+
value="400",
|
197 |
+
label="Video Height"
|
198 |
+
)
|
199 |
+
|
200 |
+
sound_choice = gr.Dropdown(
|
201 |
+
choices=["No Sound",
|
202 |
+
"Mediumspeed Typing.mp3",
|
203 |
+
"Speed Typing.mp3",
|
204 |
+
"Bass Typing.mp3",
|
205 |
+
"Bay Typing.mp3",
|
206 |
+
"Crack Typing.mp3",
|
207 |
+
"Deep Sence Typing.mp3",
|
208 |
+
"Flacking Typing.mp3",
|
209 |
+
"Flaw Typing.mp3",
|
210 |
+
"Focused Typing.mp3",
|
211 |
+
"K55 Typing.mp3",
|
212 |
+
"Laptop Typing.mp3",
|
213 |
+
"NDC Typing.mp3",
|
214 |
+
"RedMECH Typing.mp3",
|
215 |
+
"Smooth Typing.mp3",
|
216 |
+
"Stop Tpying.mp3",
|
217 |
+
],
|
218 |
+
value="No Sound",
|
219 |
+
label="Typing Sound"
|
220 |
+
)
|
221 |
+
custom_audio = gr.File(
|
222 |
+
label="Upload Custom Audio SFX🔊",
|
223 |
+
type="filepath"
|
224 |
+
)
|
225 |
+
|
226 |
+
background_color = gr.Dropdown(
|
227 |
+
choices=["black", "white", "darkblue", "orange", "green"],
|
228 |
+
value="black",
|
229 |
+
label="Background Color"
|
230 |
+
)
|
231 |
+
|
232 |
+
text_color = gr.Dropdown(
|
233 |
+
choices=["white", "black", "blue", "orange", "red"],
|
234 |
+
value="white",
|
235 |
+
label="Text Color"
|
236 |
+
)
|
237 |
+
|
238 |
+
enhance_quality = gr.Checkbox(
|
239 |
+
value=False,
|
240 |
+
label="Enhance Video Quality"
|
241 |
+
)
|
242 |
+
|
243 |
+
video_speed = gr.Dropdown(
|
244 |
+
choices=["1x", "1.25x", "1.5x", "1.75x", "2x"],
|
245 |
+
value="1x",
|
246 |
+
label="Video Speed"
|
247 |
+
)
|
248 |
+
|
249 |
+
interface = gr.Interface(
|
250 |
+
fn=generate_video,
|
251 |
+
inputs=[gr.Textbox(lines=10, placeholder="Enter code here...", label="Code Text"),
|
252 |
+
format_choice,
|
253 |
+
line_spacing,
|
254 |
+
width_choice,
|
255 |
+
height_choice,
|
256 |
+
font_choice,
|
257 |
+
font_size,
|
258 |
+
sound_choice,
|
259 |
+
custom_audio,
|
260 |
+
background_color,
|
261 |
+
text_color,
|
262 |
+
enhance_quality,
|
263 |
+
video_speed],
|
264 |
+
|
265 |
+
outputs=gr.Video(label="Typing Video"),
|
266 |
+
title="Type Bytes🐧",
|
267 |
+
css=css,
|
268 |
+
theme="bethecloud/storj_theme",
|
269 |
+
)
|
270 |
+
|
271 |
+
if __name__ == "__main__":
|
272 |
+
interface.launch(share=True)
|