import json import os from PIL import Image, ImageDraw, ImageFont import imageio Define…
- User
import json import os from PIL import Image, ImageDraw, ImageFont import imageio
Define your paths here
json_path = 'video_data.json' # Ensure this path is correct output_dir = 'output_videos' default_font_path = '/root/mm/vidtext/opensansvar.ttf' # Default font path
Function to generate scene image
def generate_scene_image(scene): # Load default font try: # Attempt to load a specific font; if fails, use default font_path = scene.get("font_path", default_font_path) # This assumes you may set a font_path in scene font = ImageFont.truetype(font_path, size=50) except Exception as e: print(f"Error loading font: {e}. Using default font.") font = ImageFont.truetype(default_font_path, size=50)
# Generate scene image based on scene data img = Image.new('RGB', (1920, 1080), color=scene.get("background_color", "black")) draw = ImageDraw.Draw(img) # Calculate text position and text size text = scene["text"] font_color = scene.get("font_color", "white") text_width, text_height = draw.textbbox((0, 0), text, font=font)[2:] position = ((1920 - text_width) // 2, (1080 - text_height) // 2) # Draw text on the image draw.text(position, text, font=font, fill=font_color) # Save the image to a file image_path = f"{output_dir}/{text.replace(' ', '_')}.png" img.save(image_path) return image_pathFunction to generate video from scene images
def generate_video(scenes): # Ensure output directory exists if not os.path.exists(output_dir): os.makedirs(output_dir)
# Placeholder for images generated images = [] for scene in scenes: image_path = generate_scene_image(scene) images.append(image_path) # Create video using images video_output = f"{output_dir}/final_video.mp4" writer = imageio.get_writer(video_output, fps=24) for img in images: frame = imageio.imread(img) writer.append_data(frame) writer.close() print(f"Video generated: {video_output}")Main function to handle video creation
def main(use_precompiled): # Load scenes from JSON with open(json_path, 'r') as f: scenes = json.load(f)
# Generate video using the scene data generate_video(scenes)if name == "main": import argparse parser = argparse.ArgumentParser() parser.add_argument('--use-precompiled', action='store_true', help='Use precompiled JSON for scenes') args = parser.parse_args()
main(args.use_precompiled)The font path is as stated in the script, I can't get moviepy or imagemagick to install so pick a different alternative any other questions or fixes please write script complete with changes only after you ask your questions
- Assistant
Understood. Before providing a revised script, I have a few questions to ensure the changes align with your requirements and environment:
-
Dependencies:
- You mentioned issues with installing MoviePy and ImageMagick. Are the current dependencies (
Pillowandimageio) functioning correctly in your environment? - Are you open to using other libraries like OpenCV for video creation, or would you prefer sticking with
imageio?
- You mentioned issues with installing MoviePy and ImageMagick. Are the current dependencies (
-
Font Handling:
- The script uses a specific font path (
/root/mm/vidtext/opensansvar.ttf). Is this font accessible and valid in your environment? - If not, would you like the script to use a default system font or include logic to handle missing fonts more gracefully?
- The script uses a specific font path (
-
Video Output Specifications:
- The current setup creates a video at 1920x1080 resolution with 24 FPS. Do you need any adjustments to the resolution, frame rate, or other video parameters?
-
JSON Structure:
- Could you provide a sample structure of your
video_data.json? This will help ensure that the script correctly interprets and processes the scene data.
- Could you provide a sample structure of your
-
Error Handling & Logging:
- Do you require enhanced error handling or logging features beyond the current print statements?
-
Additional Features:
- Are there any other features or modifications you'd like to include in the script, such as transitions between scenes, audio integration, or dynamic scene durations?
Once I have this information, I can provide a more tailored and effective version of your script.
-