sreepathi-ravikumar commited on
Commit
17901fc
·
verified ·
1 Parent(s): c4fc486

Rename sample.py to video_creater.py

Browse files
Files changed (2) hide show
  1. sample.py +0 -1
  2. video_creater.py +77 -0
sample.py DELETED
@@ -1 +0,0 @@
1
- f
 
 
video_creater.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)