Usage Examples#

Encode a Sequence of Image Files#

import video_encoding

video_encoding.encode_image_file_sequence(
    "frame_%03d.png",
    1,
    30,
    "specific_frames.mp4",
    True
)

Encode specific image files#

import video_encoding
import os

specific_files = [
    "frame_001.png",
    "frame_002.png",
    "frame_003.png"
]

encoder = video_encoding.get_video_encoding_interface()
encoder.start_encoding("specific_frames.mp4", 30.0, len(specific_files), True)

for image_file in specific_files:
    if os.path.exists(image_file):
        encoder.encode_next_frame_from_file(image_file)
        print(f"Encoded {image_file}")

encoder.finalize_encoding()

Encode Frames from Buffer Data#

import video_encoding
import numpy as np

# Get the video encoding interface
encoder = video_encoding.get_video_encoding_interface()

# Start encoding
video_filename = "output_from_buffer.mp4"
framerate = 30.0
num_frames = 60
width = 640
height = 480

# Initialize the encoder
encoder.start_encoding(video_filename, framerate, num_frames, True)

# Create and encode frames from numpy arrays
for i in range(num_frames):
    # Create a simple gradient image
    # Format: RGBA with values 0-255
    r = np.linspace(0, 255, width).reshape(1, width).astype(np.uint8)
    g = np.linspace(0, 255, height).reshape(height, 1).astype(np.uint8)
    # Create a blue value that changes with frame number
    b_value = int(i / num_frames * 255)
    
    # Create RGBA frame (R8G8B8A8 format)
    frame = np.zeros((height, width, 4), dtype=np.uint8)
    frame[:, :, 0] = r  # R channel
    frame[:, :, 1] = g  # G channel
    frame[:, :, 2] = b_value  # B channel
    frame[:, :, 3] = 255  # A channel (fully opaque)
    
    # Encode the frame
    success = encoder.encode_next_frame_from_buffer(
        buffer_rgba8=frame,
        width=width,
        height=height
    )
    
    if success:
        print(f"Encoded frame {i+1}/{num_frames}")
    else:
        print(f"Failed to encode frame {i+1}")

# Finalize the encoding process
encoder.finalize_encoding()
print(f"Video saved to {video_filename}")