import json
import os
import random
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont
from variations import VariationFont
import io
from moviepy.editor import ImageClip, concatenate_videoclips

# Predefined color names and their corresponding hex values
COLORS = {
    "Red": "#FF0000",
    "Green": "#00FF00",
    "Blue": "#0000FF",
    "Yellow": "#FFFF00",
    "Cyan": "#00FFFF",
    "Magenta": "#FF00FF",
    "Black": "#000000",
    "White": "#FFFFFF",
    "Orange": "#FFA500",
    "Purple": "#800080",
}

# Function to ask user for input with validation
def ask_questions():
    scenes = []
    num_scenes = int(input("How many scenes do you want to create? "))

    for _ in range(num_scenes):
        text = input("Enter the text for this scene: ")
        text_color = input(f"Choose a text color from {list(COLORS.keys())}: ")
        background_color = input("Enter the background color (e.g., White): ")
        text_size = int(input("Enter the text size (e.g., 40): "))
        duration = float(input("Enter the duration for this scene (in seconds): "))

        scenes.append({
            "text": text,
            "text_color": COLORS.get(text_color, "#000000"),  # Default to black if not found
            "background_color": COLORS.get(background_color, "#FFFFFF"),  # Default to white
            "text_size": text_size,
            "duration": duration
        })
    return {"scenes": scenes}

# Function to load variable font instance
def get_variable_font(font_path, text_size, axes):
    # Create a variable font instance with specified axes
    var_font = VariationFont(font_path, axes)
    
    # Save the instantiated font to a BytesIO object
    font_bytes_io = io.BytesIO()
    var_font.save(font_bytes_io)
    font_bytes_io.seek(0)

    # Load the font into PIL
    font = ImageFont.truetype(font_bytes_io, text_size)
    return font

# Function to create image for each scene
def create_scene_image(text, text_color, background_color, text_size, font_path, xrot_value, yrot_value):
    # Create an image with 1024x1024 dimensions
    img = Image.new('RGB', (1024, 1024), color=background_color)
    draw = ImageDraw.Draw(img)

    # Define the axes for the variable font
    axes = {'XROT': xrot_value, 'YROT': yrot_value}

    # Load the variable font instance
    font = get_variable_font(font_path, text_size, axes)

    # Calculate text position to center it
    bbox = draw.textbbox((0, 0), text, font=font)
    w, h = bbox[2] - bbox[0], bbox[3] - bbox[1]
    position = ((1024 - w) // 2, (1024 - h) // 2)

    # Draw the text onto the image
    draw.text(position, text, fill=text_color, font=font)

    return img

# Main script logic
def main():
    font_path = "/root/mm/var/TiltWarp-Regular-VariableFont_XROT,YROT.ttf"
    
    # Ask the user questions and generate scenes
    data = ask_questions()

    # Generate images for each scene and save them
    clips = []
    for idx, scene in enumerate(data["scenes"]):
        # Random values for XROT and YROT to create variation
        xrot_value = random.randint(-30, 30)
        yrot_value = random.randint(-30, 30)

        # Generate the scene image
        img = create_scene_image(
            scene["text"],
            scene["text_color"],
            scene["background_color"],
            scene["text_size"],
            font_path,
            xrot_value,
            yrot_value
        )

        # Save image to a temporary file
        img_path = f"/tmp/scene_{idx}.png"
        img.save(img_path)

        # Create an ImageClip
        clip = ImageClip(img_path).set_duration(scene["duration"])
        clips.append(clip)

    # Concatenate clips
    final_clip = concatenate_videoclips(clips, method="compose")

    # Write the video file
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_path = f"/root/mm/var/mp4/{timestamp}.mp4"
    final_clip.write_videofile(output_path, fps=24)

    print(f"Video saved to {output_path}")

if __name__ == "__main__":
    main()
Whats wrong with my script