Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -14,44 +14,39 @@ def install_imagemagick():
|
|
14 |
|
15 |
install_imagemagick()
|
16 |
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
draw = ImageDraw.Draw(img)
|
20 |
-
font_path = "arial.ttf" # Make sure this file is in the root directory of your space
|
21 |
-
font = ImageFont.truetype(font_path, fontsize)
|
22 |
w, h = draw.textbbox((0, 0), text, font=font)[2:]
|
23 |
draw.text(((size[0] - w) / 2, (size[1] - h) / 2), text, font=font, fill=color)
|
24 |
-
return
|
25 |
-
|
26 |
-
def process_video(text):
|
27 |
-
video_folder = "videos"
|
28 |
-
video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))]
|
29 |
-
if not video_files:
|
30 |
-
raise FileNotFoundError("No video files found in the specified directory.")
|
31 |
-
|
32 |
-
selected_video = random.choice(video_files)
|
33 |
-
video = VideoFileClip(selected_video)
|
34 |
-
start_time = random.uniform(0, max(0, video.duration - 60))
|
35 |
-
video = video.subclip(start_time, min(start_time + 60, video.duration))
|
36 |
-
|
37 |
-
# Resize the video to have a width of 1080 while keeping the aspect ratio
|
38 |
-
new_width = 1080
|
39 |
-
new_height = int(video.h * (new_width / video.w))
|
40 |
-
video = video.resize(width=new_width, resample=Image.LANCZOS)
|
41 |
-
|
42 |
-
# Crop the video to a 9:16 aspect ratio
|
43 |
-
target_height = 1920
|
44 |
-
video = video.crop(y_center=video.h / 2, height=target_height)
|
45 |
-
|
46 |
-
text_lines = text.split()
|
47 |
-
text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)])
|
48 |
-
|
49 |
-
text_img = create_text_clip(text, fontsize=70, color='white', size=video.size)
|
50 |
-
text_clip = ImageClip(text_img).set_duration(video.duration).set_position(('center', 'center')).set_opacity(1)
|
51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
final_clip = CompositeVideoClip([video, text_clip])
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
55 |
final_clip.write_videofile(output_path, codec="libx264")
|
56 |
|
57 |
return output_path
|
|
|
14 |
|
15 |
install_imagemagick()
|
16 |
|
17 |
+
from moviepy.editor import VideoFileClip, CompositeVideoClip, ImageClip
|
18 |
+
from PIL import Image, ImageDraw, ImageFont
|
19 |
+
|
20 |
+
def create_text_clip(text, fontsize=70, color='white', size=(1080, 1920)):
|
21 |
+
font = ImageFont.truetype("arial.ttf", fontsize)
|
22 |
+
img = Image.new('RGBA', size, (0, 0, 0, 0))
|
23 |
draw = ImageDraw.Draw(img)
|
|
|
|
|
24 |
w, h = draw.textbbox((0, 0), text, font=font)[2:]
|
25 |
draw.text(((size[0] - w) / 2, (size[1] - h) / 2), text, font=font, fill=color)
|
26 |
+
return img
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def process_video(video_path, text):
|
29 |
+
video = VideoFileClip(video_path)
|
30 |
+
|
31 |
+
# Calculate new width to maintain the 9:16 aspect ratio
|
32 |
+
new_width = int(video.h * 9 / 16)
|
33 |
+
|
34 |
+
# Resize video to maintain 9:16 aspect ratio
|
35 |
+
video = video.resize(width=new_width)
|
36 |
+
|
37 |
+
# Create a text image
|
38 |
+
text_img = create_text_clip(text, fontsize=70, color='white', size=(new_width, video.h))
|
39 |
+
|
40 |
+
# Convert the text image to an ImageClip
|
41 |
+
text_clip = ImageClip(text_img).set_duration(video.duration)
|
42 |
+
|
43 |
+
# Composite the video with the text clip
|
44 |
final_clip = CompositeVideoClip([video, text_clip])
|
45 |
+
|
46 |
+
# Save the final video
|
47 |
+
final_clip.write_videofile("output_video.mp4", codec="libx264", fps=24)
|
48 |
+
|
49 |
+
return "output_video.mp4"
|
50 |
final_clip.write_videofile(output_path, codec="libx264")
|
51 |
|
52 |
return output_path
|