Skip to content

Instantly share code, notes, and snippets.

@faceslog
Last active July 29, 2023 20:49
Show Gist options
  • Save faceslog/90eea762aff06b197edb8c3a081ef3ea to your computer and use it in GitHub Desktop.
Save faceslog/90eea762aff06b197edb8c3a081ef3ea to your computer and use it in GitHub Desktop.
Extract and assemble frames from a video using opencv used to create Stable Diffusion Video

Extract the frames

Create a virtual env

python -m venv myenv
# To activate the environment on Windows:
./myenv/Scripts/Activate.ps1
# To activate the environment on Linux:
source myenv/bin/activate

Install the required packages:

pip install -r requirements.txt

Make sure you have a input.mp4 in the same folder and start:

python extract.py
import cv2
import os
import glob
import re
# Define the output video file
output_file = "output.mp4"
# Get a list of all image files
img_files = glob.glob("sd_frames/*.jpg")
# Function to extract frame number from filename
def extract_number(filename):
# This regular expression finds a number in the filename
numbers = re.findall(r'\d+', filename)
# If we found a number, return it as integer
if numbers:
return int(numbers[0])
else:
return None
# Sort the image files based on the frame number
img_files.sort(key=extract_number)
# Read the first image to get the size and color info
img = cv2.imread(img_files[0])
# Get image properties.
height, width, layers = img.shape
# Define the codec and create a VideoWriter object.
# FourCC is a 4-byte code used to specify the video codec.
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # or use 'XVID'
out = cv2.VideoWriter(output_file, fourcc, 30.0, (width, height))
for img_file in img_files:
# Read the image file
img = cv2.imread(img_file)
# Write the frame to the video file
out.write(img)
# After going through all images, release the video writer
out.release()
import cv2
import os
# Define the name of the video file and output folder
video_file = "input.mp4"
output_folder = "extracted_frames"
# Create the output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Open the video file
cap = cv2.VideoCapture(video_file)
# Check if camera opened successfully
if not cap.isOpened():
print("Error opening video stream or file")
frame_num = 0
while cap.isOpened():
# Read a frame
ret, frame = cap.read()
# If we got a frame, save it to the output folder
if ret:
# Name the file with the frame number
output_file = os.path.join(output_folder, f"frame_{frame_num}.jpg")
# Save the frame as an image file
cv2.imwrite(output_file, frame)
frame_num += 1
else:
break
# After going through all frames, release the video capture object
cap.release()
# Close all the frames
cv2.destroyAllWindows()
opencv-python
glob2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment