Created
August 9, 2023 15:04
-
-
Save jacks0n9/db93c016e72b1f80a22a4496e8a6a9a0 to your computer and use it in GitHub Desktop.
A script to generate a random video. Credit to https://www.youtube.com/@tiepup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import random | |
import string | |
import os | |
import sys | |
# Number of videos to generate | |
num_videos = 1 | |
# Set video properties | |
width = 1920 | |
height = 1080 | |
fps = 24 | |
duration = 5 | |
def generate_video(): | |
for video_idx in range(num_videos): | |
# Generate random display duration for characters | |
min_display_duration = random.randint(200, 500) | |
max_display_duration = random.randint(500, 1000) | |
frames_per_second_with_characters = fps * (max_display_duration / 1000) | |
# Use a unique filename with a timestamp for each video | |
timestamp = int(time.time()) # Get the current Unix timestamp | |
filename = f'random.mp4' | |
# Create a VideoWriter object | |
fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
video = cv2.VideoWriter(filename, fourcc, fps, (width, height)) | |
# Randomly choose the frame type for this run | |
frame_type = random.choice(["solid_color", "color_bars", "random_pixels"]) | |
# Generate background color for solid color frames | |
background_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) | |
# Generate solid geometric shapes | |
num_shapes = random.randint(5, 10) | |
shapes = [] | |
for _ in range(num_shapes): | |
shape_type = random.choice(["rectangle", "circle"]) | |
shape_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) | |
shape_thickness = random.randint(2, 140) | |
shape_position = (random.randint(0, width), random.randint(0, height)) | |
shape_size = (random.randint(50, 200), random.randint(50, 200)) | |
shape = { | |
"type": shape_type, | |
"color": shape_color, | |
"thickness": shape_thickness, | |
"position": shape_position, | |
"size": shape_size | |
} | |
shapes.append(shape) | |
# Calculate the number of frames to display characters per second | |
frames_per_second_with_characters = fps * (max_display_duration / 1000) | |
# Generate random frames and write them to the video | |
num_frames = fps * duration | |
# Initialize a persistent background frame | |
background_frame = np.zeros((height, width, 3), dtype=np.uint8) | |
for frame_idx in range(num_frames): | |
# Generate the background frame based on the frame type | |
if frame_type == "solid_color": | |
background_frame[:, :] = background_color | |
elif frame_type == "color_bars": | |
if frame_idx == 0: | |
colors = [(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) for _ in range(8)] | |
bar_height = height // 8 | |
for i in range(8): | |
background_frame[i * bar_height: (i + 1) * bar_height, :] = colors[i] | |
else: # frame_type == "random_pixels" | |
background_frame = np.random.randint(0, 256, (height, width, 3), dtype=np.uint8) | |
# Add solid geometric shapes to the background frame | |
for shape in shapes: | |
shape_type = shape["type"] | |
shape_color = shape["color"] | |
shape_thickness = shape["thickness"] | |
shape_position = shape["position"] | |
shape_size = shape["size"] | |
if shape_type == "rectangle": | |
cv2.rectangle(background_frame, shape_position, (shape_position[0] + shape_size[0], shape_position[1] + shape_size[1]), | |
shape_color, -1, lineType=cv2.LINE_AA) | |
else: # shape_type == "circle" | |
cv2.circle(background_frame, shape_position, shape_size[0] // 2, shape_color, -1, lineType=cv2.LINE_AA) | |
# Copy the background frame to draw characters on top of it | |
frame = background_frame.copy() | |
# Add common special characters to the list of ASCII characters | |
all_characters = [chr(i) for i in range(33, 127)] + ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', ':', ';', '<', '>', '?', '[', ']', '|', '`', '~', '=', '-'] | |
for _ in range(5): | |
letter = random.choice(all_characters) # Generate a random character | |
font = random.choice([ | |
cv2.FONT_HERSHEY_SIMPLEX, | |
cv2.FONT_HERSHEY_COMPLEX, | |
cv2.FONT_HERSHEY_TRIPLEX, | |
cv2.FONT_HERSHEY_PLAIN, | |
cv2.FONT_HERSHEY_COMPLEX_SMALL, | |
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, | |
cv2.FONT_HERSHEY_SCRIPT_COMPLEX, | |
cv2.FONT_ITALIC | |
]) # Example fonts | |
font_size = random.uniform(50, 150) # Random font size for large letters | |
small_font_size = random.uniform(5, 20) # Random font size for small letters | |
text_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)) # Random text color | |
text_thickness = random.randint(3, 6) # Increased text outline thickness | |
text_position = (random.randint(10, width - 50), random.randint(30, height - 10)) # Random text position | |
if random.random() < 0.5: | |
font_size, small_font_size = small_font_size, font_size | |
font_style = random.choice([cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_COMPLEX, cv2.FONT_HERSHEY_TRIPLEX]) | |
# Generate random style options | |
is_bold = bool(random.getrandbits(1)) | |
is_italic = bool(random.getrandbits(1)) | |
is_superscript = bool(random.getrandbits(1)) | |
is_subscript = bool(random.getrandbits(1)) | |
# Set font style options | |
if is_bold: | |
font_thickness = random.choice([text_thickness, text_thickness + 1, text_thickness + 2]) | |
else: | |
font_thickness = text_thickness | |
if is_italic: | |
font_scale = font_size * 0.7 | |
font_thickness -= 1 | |
else: | |
font_scale = font_size | |
if is_superscript: | |
text_position = (text_position[0], text_position[1] - int(font_scale)) | |
if is_subscript: | |
text_position = (text_position[0], text_position[1] + int(font_scale)) | |
# Calculate the size of the letter based on font size and frame dimensions | |
(text_width, text_height), _ = cv2.getTextSize(letter, font_style, font_scale, font_thickness) | |
# Adjust the text position to center the letter within the bounding box | |
text_position = ( | |
text_position[0] - text_width // 2, | |
text_position[1] + text_height // 2 | |
) | |
cv2.putText(frame, letter, text_position, font_style, font_scale, text_color, font_thickness, cv2.LINE_AA) | |
# Determine whether the frame should have characters or not | |
frame_with_characters = random.random() < 0.5 | |
# Write each frame according to its display_duration | |
if frame_with_characters: | |
display_duration = random.randint(min_display_duration, max_display_duration) | |
for _ in range(int(display_duration / (1000 / fps))): | |
video.write(frame) | |
else: | |
for _ in range(int(fps)): | |
video.write(frame) | |
# Release the video writer and close the video file | |
video.release() | |
print("Video produced successfully!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment