Spaces:
Sleeping
Sleeping
Rename video_creater.py to video.py
Browse files- video.py +75 -0
- video_creater.py +0 -77
video.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
IMAGE_DIR = "/tmp/images"
|
| 5 |
+
os.makedirs(IMAGE_DIR, exist_ok=True)
|
| 6 |
+
|
| 7 |
+
def get_text_size(text, font):
|
| 8 |
+
bbox = font.getbbox(text)
|
| 9 |
+
width = bbox[2] - bbox[0]
|
| 10 |
+
height = bbox[3] - bbox[1]
|
| 11 |
+
return width, height
|
| 12 |
+
|
| 13 |
+
def create_text_image(text,id,image_olst, image_size=(1280, 720), bg_color="white", text_color="black"):
|
| 14 |
+
image = Image.new("RGB", image_size, color=bg_color)
|
| 15 |
+
draw = ImageDraw.Draw(image)
|
| 16 |
+
|
| 17 |
+
font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf"
|
| 18 |
+
bold_font_path = "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf"
|
| 19 |
+
max_font_size = 65
|
| 20 |
+
min_font_size = 10
|
| 21 |
+
|
| 22 |
+
best_font = None
|
| 23 |
+
best_lines = []
|
| 24 |
+
|
| 25 |
+
for font_size in range(max_font_size, min_font_size, -2):
|
| 26 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 27 |
+
words = text.split()
|
| 28 |
+
lines = []
|
| 29 |
+
current_line = ""
|
| 30 |
+
|
| 31 |
+
lines_raw = text.splitlines()
|
| 32 |
+
lines = []
|
| 33 |
+
|
| 34 |
+
for raw_line in lines_raw:
|
| 35 |
+
words = raw_line.split()
|
| 36 |
+
current_line = ""
|
| 37 |
+
|
| 38 |
+
left_padding = 80
|
| 39 |
+
right_padding = 80
|
| 40 |
+
max_text_width = image_size[0] - left_padding - right_padding
|
| 41 |
+
|
| 42 |
+
for word in words:
|
| 43 |
+
test_line = f"{current_line} {word}" if current_line else word
|
| 44 |
+
w, _ = get_text_size(test_line, font)
|
| 45 |
+
if w <= max_text_width:
|
| 46 |
+
current_line = test_line
|
| 47 |
+
else:
|
| 48 |
+
lines.append(current_line)
|
| 49 |
+
current_line = word
|
| 50 |
+
if current_line:
|
| 51 |
+
lines.append(current_line)
|
| 52 |
+
|
| 53 |
+
total_height = sum(get_text_size(line, font)[1] + 23 for line in lines)
|
| 54 |
+
if total_height <= image_size[1] * 0.95:
|
| 55 |
+
best_font = font
|
| 56 |
+
best_lines = lines
|
| 57 |
+
break
|
| 58 |
+
|
| 59 |
+
heading_color = "#0033cc" # Deep attractive blue
|
| 60 |
+
normal_color = text_color # Usually black
|
| 61 |
+
|
| 62 |
+
y = (image_size[1] - total_height) // 2
|
| 63 |
+
for line in best_lines:
|
| 64 |
+
is_heading = line.strip().endswith("?") or line.strip().endswith(":")
|
| 65 |
+
font_to_use = ImageFont.truetype(bold_font_path, best_font.size) if is_heading else best_font
|
| 66 |
+
color_to_use = heading_color if is_heading else normal_color
|
| 67 |
+
|
| 68 |
+
w, h = get_text_size(line, font_to_use)
|
| 69 |
+
x = 80 # Left padding
|
| 70 |
+
draw.text((x, y), line, font=font_to_use, fill=color_to_use)
|
| 71 |
+
y += h + 23
|
| 72 |
+
image_name="slide"+str(id)+".png"
|
| 73 |
+
image_olst.append(image_name)
|
| 74 |
+
image_path=os.path.join(IMAGE_DIR,image_name)
|
| 75 |
+
image.save(image_path)
|
video_creater.py
DELETED
|
@@ -1,77 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from gtts import gTTS
|
| 3 |
-
from mutagen.mp3 import MP3
|
| 4 |
-
from moviepy.editor import *
|
| 5 |
-
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
-
|
| 7 |
-
def video_gen(TEXT):
|
| 8 |
-
# === SETTINGS ===
|
| 9 |
-
WIDTH, HEIGHT = 1280, 720
|
| 10 |
-
FONT_SIZE = 100
|
| 11 |
-
FONT_PATH = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" # Change for Windows/Mac
|
| 12 |
-
FONT_COLOR = 'white'
|
| 13 |
-
IMAGE_PATH = "images"
|
| 14 |
-
TEMP_FRAME_PATH = "temp_frames"
|
| 15 |
-
AUDIO_FILE = "voice.mp3"
|
| 16 |
-
OUTPUT_VIDEO = "output_video.mp4"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# === STEP 1: Convert text to audio ===
|
| 21 |
-
def generate_audio(text, filename):
|
| 22 |
-
tts = gTTS(text, lang='ta')
|
| 23 |
-
tts.save(filename)
|
| 24 |
-
|
| 25 |
-
# === STEP 2: Get images ===
|
| 26 |
-
def get_images(folder):
|
| 27 |
-
return sorted([os.path.join(folder, f)
|
| 28 |
-
for f in os.listdir(folder)
|
| 29 |
-
if f.lower().endswith(('.jpg', '.jpeg', '.png'))])
|
| 30 |
-
|
| 31 |
-
# === STEP 3: Create text-overlay images using Pillow ===
|
| 32 |
-
def create_text_image(background_img, text, output_path):
|
| 33 |
-
img = Image.open(background_img).resize((WIDTH, HEIGHT))
|
| 34 |
-
draw = ImageDraw.Draw(img)
|
| 35 |
-
try:
|
| 36 |
-
font = ImageFont.truetype(FONT_PATH, FONT_SIZE)
|
| 37 |
-
except:
|
| 38 |
-
font = ImageFont.load_default() # Fallback if font fails
|
| 39 |
-
margin = 60
|
| 40 |
-
offset = 100
|
| 41 |
-
for line in text.split('\n'):
|
| 42 |
-
draw.text((margin, offset), line.strip(), font=font, fill=FONT_COLOR)
|
| 43 |
-
offset += FONT_SIZE + 10
|
| 44 |
-
img.save(output_path)
|
| 45 |
-
|
| 46 |
-
# === STEP 4: Create video slides ===
|
| 47 |
-
def create_video_slides(images, paragraphs, audio_duration):
|
| 48 |
-
os.makedirs(TEMP_FRAME_PATH, exist_ok=True)
|
| 49 |
-
clips = []
|
| 50 |
-
duration_per_slide = audio_duration / len(paragraphs)
|
| 51 |
-
|
| 52 |
-
for i, para in enumerate(paragraphs):
|
| 53 |
-
temp_image = os.path.join(TEMP_FRAME_PATH, f"frame_{i}.png")
|
| 54 |
-
create_text_image(images[i % len(images)], para, temp_image)
|
| 55 |
-
clip = ImageClip(temp_image).set_duration(duration_per_slide)
|
| 56 |
-
clips.append(clip)
|
| 57 |
-
return clips
|
| 58 |
-
|
| 59 |
-
# === MAIN ===
|
| 60 |
-
if __name__ == "__main__":
|
| 61 |
-
print("Generating audio...")
|
| 62 |
-
generate_audio(TEXT, AUDIO_FILE)
|
| 63 |
-
audio = AudioFileClip(AUDIO_FILE)
|
| 64 |
-
duration = MP3(AUDIO_FILE).info.length
|
| 65 |
-
|
| 66 |
-
print("Preparing images...")
|
| 67 |
-
images = get_images(IMAGE_PATH)
|
| 68 |
-
paragraphs = [p.strip() for p in TEXT.strip().split("\n\n") if p.strip()]
|
| 69 |
-
|
| 70 |
-
print("Creating video slides...")
|
| 71 |
-
video_clips = create_video_slides(images, paragraphs, duration)
|
| 72 |
-
|
| 73 |
-
print("Finalizing video...")
|
| 74 |
-
final_video = concatenate_videoclips(video_clips).set_audio(audio)
|
| 75 |
-
final_video.write_videofile(OUTPUT_VIDEO, fps=24)
|
| 76 |
-
|
| 77 |
-
print("Done! Output saved to:", OUTPUT_VIDEO)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|