import os import json from PIL import Image, ImageDraw, ImageFont, ImageColor import cv2…
- User
import os import json from PIL import Image, ImageDraw, ImageFont, ImageColor import cv2 import numpy as np
Video settings
FPS = 30 # Frames per second FRAME_WIDTH = 1024 FRAME_HEIGHT = 1024 DEFAULT_FONT_PATH = "/root/mm/vidfinal/opensansvar.ttf" # Update with your font path VIDEO_CODEC = "mp4v" # Codec for MP4 files OUTPUT_DIR = "output" # Directory to save the video VIDEO_OUTPUT = os.path.join(OUTPUT_DIR, "output_video.mp4") JSON_PATH = "video_data.json" # Path to your JSON file with scenes
Define durations in seconds
FADE_IN_DURATION = 1 DISPLAY_DURATION = 5 TEXT_ANIMATION_DURATION = 3 FADE_OUT_DURATION = 1
Calculate number of frames for each segment
FADE_IN_FRAMES = FADE_IN_DURATION * FPS DISPLAY_FRAMES = DISPLAY_DURATION * FPS TEXT_ANIMATION_FRAMES = TEXT_ANIMATION_DURATION * FPS FADE_OUT_FRAMES = FADE_OUT_DURATION * FPS
Total frames per scene
TOTAL_SCENE_FRAMES = FADE_IN_FRAMES + DISPLAY_FRAMES + TEXT_ANIMATION_FRAMES + FADE_OUT_FRAMES
def load_scenes(json_path): """ Load scenes from a JSON file. """ try: with open(json_path, 'r') as f: scenes = json.load(f) if not isinstance(scenes, list): raise ValueError("JSON data must be a list of scenes.") return scenes except Exception as e: print(f"Error loading JSON: {e}") return []
def create_base_image(scene): """ Create the base image for a scene with background color. """ background_color = scene.get("background_color", "black") img = Image.new('RGB', (FRAME_WIDTH, FRAME_HEIGHT), color=background_color) return img
def load_font(scene): """ Load the specified font or default font. """ try: font_path = scene.get("font_path", DEFAULT_FONT_PATH) font_size = scene.get("font_size", 50) # Adjusted default for square frame font = ImageFont.truetype(font_path, size=font_size) except Exception as e: print(f"Error loading font '{scene.get('font_path', DEFAULT_FONT_PATH)}': {e}. Using default font.") try: font = ImageFont.truetype(DEFAULT_FONT_PATH, size=50) except Exception as e: print(f"Failed to load default font. {e}") font = ImageFont.load_default() return font
def add_text(image, text, font, font_color, visible_chars=None): """ Add text to an image. If visible_chars is specified, only that many characters are visible. """ draw = ImageDraw.Draw(image) text_width, text_height = draw.textsize(text, font=font) position = ((FRAME_WIDTH - text_width) // 2, (FRAME_HEIGHT - text_height) // 2)
codeif visible_chars is not None: text_to_draw = text[:visible_chars] else: text_to_draw = text draw.text(position, text_to_draw, font=font, fill=font_color) return imagedef blend_images(img1, img2, alpha): """ Blend two PIL images with the given alpha for img2. """ return Image.blend(img1, img2, alpha)
- Assistant