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