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}")