Skip to content

Examples

SpyC0der77 edited this page Nov 23, 2024 · 2 revisions

VidKit Examples

This page contains practical examples of using VidKit for various video generation scenarios. Each example includes a complete configuration and explanation.

Basic Examples

1. Simple Image Sequence

Create a basic video from two images, each showing for 3 seconds:

{
    "name": "simple_sequence",
    "format": "mp4",
    "framerate": 30,
    "resolution": [1920, 1080],
    "frames": [
        {
            "image": "intro_slide.jpg",
            "duration": 3
        },
        {
            "image": "outro_slide.jpg",
            "duration": 3
        }
    ]
}

2. Presentation with Audio

Create a presentation-style video with background music:

{
    "name": "presentation",
    "format": "mp4",
    "framerate": 30,
    "resolution": [1920, 1080],
    "frames": [
        {
            "image": "title.jpg",
            "duration": 5
        },
        {
            "image": "content1.jpg",
            "duration": 10
        },
        {
            "image": "content2.jpg",
            "duration": 10
        },
        {
            "image": "summary.jpg",
            "duration": 5
        }
    ],
    "audio": "background_music.mp3"
}

Code Examples

Here's how to use these configurations in Python:

from vidkit import renderVideo, saveVideo
import json

# Load configuration from a file
with open("config.json", "r") as f:
    config = json.load(f)

# Generate the video
video_bytes = renderVideo(config)

# Save the video
saveVideo(video_bytes, "output.mp4")

Creating Test Images

Here's a helpful script to create test images for experimenting with VidKit:

from PIL import Image, ImageDraw, ImageFont
import os

def create_text_image(text, filename, size=(1920, 1080), bg_color="white", text_color="black"):
    # Create a new image with a white background
    image = Image.new('RGB', size, bg_color)
    draw = ImageDraw.Draw(image)
    
    # Use a default font
    try:
        font = ImageFont.truetype("arial.ttf", 60)
    except:
        font = ImageFont.load_default()
    
    # Get text size
    text_bbox = draw.textbbox((0, 0), text, font=font)
    text_width = text_bbox[2] - text_bbox[0]
    text_height = text_bbox[3] - text_bbox[1]
    
    # Calculate text position (center)
    x = (size[0] - text_width) // 2
    y = (size[1] - text_height) // 2
    
    # Draw the text
    draw.text((x, y), text, font=font, fill=text_color)
    
    # Save the image
    image.save(filename)

# Create test images
create_text_image("Welcome to My Presentation", "title.jpg")
create_text_image("First Slide", "content1.jpg")
create_text_image("Second Slide", "content2.jpg")
create_text_image("Thank You!", "summary.jpg")

# Create configuration
config = {
    "name": "test_presentation",
    "format": "mp4",
    "framerate": 30,
    "resolution": [1920, 1080],
    "frames": [
        {"image": "title.jpg", "duration": 3},
        {"image": "content1.jpg", "duration": 3},
        {"image": "content2.jpg", "duration": 3},
        {"image": "summary.jpg", "duration": 3}
    ]
}

# Generate and save video
video_bytes = renderVideo(config)
saveVideo(video_bytes, "test_presentation.mp4")

Tips and Best Practices

  1. File Organization: Keep your media files (images, audio) in separate directories for better organization.
  2. Performance: For better performance, optimize your media files before using them in VidKit:
    • Resize images to match your target video resolution
    • Convert audio to MP3 format
    • Use consistent image dimensions
  3. Testing: Start with shorter duration videos while testing your configurations.
  4. Image Formats: Use JPG or PNG formats for images.

Additional Resources