Spaces:
Runtime error
Runtime error
zfff
Browse files- video_generator.py +14 -25
video_generator.py
CHANGED
|
@@ -9,37 +9,26 @@ def generate_lyric_frame(lyric, image_size):
|
|
| 9 |
image_size = (800, 600) # Example default size, you can change this
|
| 10 |
|
| 11 |
# Ensure image_size is a tuple
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
factor = 2
|
| 16 |
-
large_image_size = (image_size[0] * factor, image_size[1] * factor)
|
| 17 |
-
image = Image.new('RGB', large_image_size, color='black')
|
| 18 |
draw = ImageDraw.Draw(image)
|
| 19 |
-
font = ImageFont.load_default()
|
| 20 |
-
|
| 21 |
-
# Wrap text
|
| 22 |
-
max_width = large_image_size[0] - 40 * factor
|
| 23 |
-
wrapped_text = textwrap.fill(lyric, width=40 * factor)
|
| 24 |
-
lines = wrapped_text.split('\n')
|
| 25 |
|
| 26 |
-
#
|
| 27 |
-
|
| 28 |
-
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
for line in lines:
|
| 37 |
-
draw.text((text_x, y), line, font=font, fill='white')
|
| 38 |
-
y += font.getmask(line).getbbox()[3] if font.getmask(line).getbbox() else 0
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
return
|
| 43 |
|
| 44 |
|
| 45 |
def generate_video(lyrics_with_timing, image_size=(800, 600), frame_rate=30):
|
|
|
|
| 9 |
image_size = (800, 600) # Example default size, you can change this
|
| 10 |
|
| 11 |
# Ensure image_size is a tuple
|
| 12 |
+
if not isinstance(image_size, tuple):
|
| 13 |
+
image_size = tuple(image_size)
|
| 14 |
|
| 15 |
+
image = Image.new('RGB', image_size, color='black')
|
|
|
|
|
|
|
|
|
|
| 16 |
draw = ImageDraw.Draw(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Define font and text alignment
|
| 19 |
+
font = ImageFont.load_default()
|
| 20 |
+
text_width = draw.textlength(lyric, font=font) # Getting text width
|
| 21 |
|
| 22 |
+
# Getting text height, with a check for empty text
|
| 23 |
+
text_bbox = font.getmask(lyric).getbbox()
|
| 24 |
+
text_height = text_bbox[3] if text_bbox else 0
|
| 25 |
|
| 26 |
+
text_x = (image_size[0] - text_width) / 2
|
| 27 |
+
text_y = (image_size[1] - text_height) / 2
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Draw the text on the image
|
| 30 |
+
draw.text((text_x, text_y), lyric, font=font, fill='white')
|
| 31 |
+
return image
|
| 32 |
|
| 33 |
|
| 34 |
def generate_video(lyrics_with_timing, image_size=(800, 600), frame_rate=30):
|