-
Notifications
You must be signed in to change notification settings - Fork 0
/
gif_creation.py
95 lines (73 loc) · 3.66 KB
/
gif_creation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import numpy as np
import os
def extract_frames_from_videos(video_paths, target_frame_count, fps):
"""Extract frames from multiple videos and ensure each video contributes the same number of frames."""
all_videos_frames = []
for video_path in video_paths:
cap = cv2.VideoCapture(video_path)
frames = []
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Calculate frame interval to get the target frame count
frame_interval = max(1, total_frames // target_frame_count)
count = 0
while cap.isOpened() and len(frames) < target_frame_count:
ret, frame = cap.read()
if not ret:
break
if count % frame_interval == 0:
frames.append(frame)
count += 1
cap.release()
# Ensure the frames list is of the target length
if len(frames) < target_frame_count:
frames += [frames[-1]] * (target_frame_count - len(frames)) # Repeat last frame if needed
all_videos_frames.append(frames)
return all_videos_frames
def create_grid_video(frames, grid_size, output_path, fps):
"""Create a video in a grid format from the extracted frames of multiple videos."""
num_videos = len(frames)
grid_height, grid_width = frames[0][0].shape[:2]
output_height, output_width = grid_height * grid_size, grid_width * grid_size
# Define the output video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (output_width, output_height))
# Iterate through frames and stitch them together in a grid
for frame_idx in range(len(frames[0])):
grid_frame = np.zeros((output_height, output_width, 3), dtype=np.uint8)
for idx, video_frames in enumerate(frames):
i, j = divmod(idx, grid_size)
small_frame = video_frames[frame_idx]
y1, y2 = i * grid_height, (i + 1) * grid_height
x1, x2 = j * grid_width, (j + 1) * grid_width
grid_frame[y1:y2, x1:x2] = small_frame
# Write the grid frame to the output video
out.write(grid_frame)
out.release()
def main(input_folder, output_video_path):
# Get the first 16 video files from the folder
video_files = sorted([os.path.join(input_folder, f) for f in os.listdir(input_folder) if f.endswith(('.mp4', '.avi', '.mov'))])[:16]
if len(video_files) < 16:
print("Not enough videos in the folder. At least 16 videos are required.")
return
# Extract frames from videos, ensuring each video is 3 seconds long at 30 fps
fps = 30 # Frames per second
duration_seconds = 3 # Output video duration in seconds
target_frame_count = fps * duration_seconds
frames = extract_frames_from_videos(video_files, target_frame_count, fps)
# Create a 4x4 grid video
grid_size = 4
create_grid_video(frames, grid_size, output_video_path, fps)
if __name__ == "__main__":
input_folder = "output_segments" # Replace with your folder containing videos
output_video_path = "output_gif_og.mp4" # Output MP4 path
main(input_folder, output_video_path)
print("4x4 Grid Video created successfully!")
input_folder = "output_segments1" # Replace with your folder containing videos
output_video_path = "output_gif_black.mp4" # Output MP4 path
main(input_folder, output_video_path)
print("4x4 Grid Video created successfully!")
input_folder = "output_segments2" # Replace with your folder containing videos
output_video_path = "output_gif_eyes.mp4" # Output MP4 path
main(input_folder, output_video_path)
print("4x4 Grid Video created successfully!")