-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_generator.py
More file actions
79 lines (69 loc) · 2.65 KB
/
video_generator.py
File metadata and controls
79 lines (69 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Generate a video from script text using free tools (gTTS + moviepy)
# In production, use TTS (gTTS) and moviepy, or a virtual influencer API
from gtts import gTTS
from moviepy.editor import ImageClip, AudioFileClip
import os
from config import VIDEO_TITLE
def generate_video(script_text: str) -> str:
if not script_text or not script_text.strip():
raise ValueError("Script text cannot be empty")
audio_path = "output_audio.mp3"
img_path = "background.png"
video_path = "output_video.mp4"
try:
# Generate TTS audio from script
tts = gTTS(text=script_text, lang='en')
tts.save(audio_path)
# Create a solid color background image (1280x720)
from PIL import Image, ImageDraw, ImageFont
img = Image.new('RGB', (1280, 720), color=(
30, 144, 255)) # Dodger blue
d = ImageDraw.Draw(img)
# Add title text (optional, short)
title = VIDEO_TITLE
try:
# Try different font paths for cross-platform compatibility
font_paths = [
"arial.ttf", # Windows
"/System/Library/Fonts/Arial.ttf", # macOS
"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf" # Linux
]
font = None
for font_path in font_paths:
try:
font = ImageFont.truetype(font_path, 60)
break
except (OSError, IOError):
continue
if font is None:
font = ImageFont.load_default()
except Exception:
font = ImageFont.load_default()
d.text((50, 50), title, fill=(255, 255, 255), font=font)
img.save(img_path)
# Create video clip from image
audio_clip = None
image_clip = None
try:
audio_clip = AudioFileClip(audio_path)
duration = audio_clip.duration
image_clip = ImageClip(img_path).set_duration(duration)
image_clip = image_clip.set_audio(audio_clip)
# Generate video
image_clip.write_videofile(
video_path, fps=24, codec="libx264", audio_codec="aac")
finally:
# Properly close clips to free memory
if audio_clip:
audio_clip.close()
if image_clip:
image_clip.close()
finally:
# Clean up temp files (always executed)
for temp_file in [audio_path, img_path]:
if os.path.exists(temp_file):
try:
os.remove(temp_file)
except OSError:
pass # Ignore cleanup errors
return video_path