import json
import os
import random
from datetime import datetime
from PIL import Image, ImageDraw, ImageFont

# 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():
    data = {}
    
    # How many scenes
    while True:
        try:
            scenes = int(input("How many scenes? "))
            if scenes <= 0:
                print("Please enter a positive number.")
                continue
            break
        except ValueError:
            print("Invalid input. Please enter a number.")

    data["scenes"] = []

    for i in range(scenes):
        scene = {}
        
        # Question 1: What text would you like to add?
        scene["text"] = input(f"Scene {i+1}: What text would you like to add? ")

        # Question 2: Choose text color
        print(f"Choose text color for scene {i+1}:")
        for index, color in enumerate(COLORS.keys()):
            print(f"{index+1}. {color}")
        while True:
            try:
                color_choice = int(input("Enter the number for the text color: "))
                if color_choice < 1 or color_choice > len(COLORS):
                    print("Invalid choice. Please select a number from the list.")
                    continue
                scene["text_color"] = COLORS[list(COLORS.keys())[color_choice - 1]]
                break
            except ValueError:
                print("Invalid input. Please enter a number.")

        # Question 3: Duration of scene (in seconds)
        while True:
            try:
                scene["duration"] = int(input(f"Scene {i+1}: Enter duration of the scene (in seconds): "))
                if scene["duration"] <= 0:
                    print("Please enter a positive number.")
                    continue
                break
            except ValueError:
                print("Invalid input. Please enter a number.")

        # Question 4: Choose background color
        print(f"Choose background color for scene {i+1}:")
        for index, color in enumerate(COLORS.keys()):
            print(f"{index+1}. {color}")
        while True:
            try:
                bg_choice = int(input("Enter the number for the background color: "))
                if bg_choice < 1 or bg_choice > len(COLORS):
                    print("Invalid choice. Please select a number from the list.")
                    continue
                scene["background_color"] = COLORS[list(COLORS.keys())[bg_choice - 1]]
                break
            except ValueError:
                print("Invalid input. Please enter a number.")

        # Question 5: Choose text size
        text_sizes = [20, 40, 60, 80, 100]
        print(f"Choose text size for scene {i+1}:")
        for index, size in enumerate(text_sizes):
            print(f"{index+1}. {size}")
        while True:
            try:
                size_choice = int(input("Enter the number for the text size: "))
                if size_choice < 1 or size_choice > len(text_sizes):
                    print("Invalid choice. Please select a number from the list.")
                    continue
                scene["text_size"] = text_sizes[size_choice - 1]
                break
            except ValueError:
                print("Invalid input. Please enter a number.")

        # Add scene to data
        data["scenes"].append(scene)

    # Save the data to var.json
    with open("/root/mm/var/var.json", "w") as f:
        json.dump(data, f, indent=4)

    return data

# 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)

    # Load the variable font with rotation axes (XROT, YROT)
    font = ImageFont.truetype(f"{font_path}[XROT={xrot_value},YROT={yrot_value}]", text_size)

    # Calculate text position to center it
    w, h = draw.textsize(text, font=font)
    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
    images = []
    for scene in 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)
        images.append(img)

    # Save the images to an MP4 file
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_path = f"/root/mm/var/mp4/{timestamp}.mp4"

    # Save images as an MP4 file (requires ffmpeg or other external tools)
    img.save(output_path, save_all=True, append_images=images, duration=[scene["duration"] * 1000 for scene in data["scenes"]], loop=0)

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

if __name__ == "__main__":
    main()
What libraries would help. What's wrong with the script?