Created
April 6, 2023 17:39
-
-
Save msfeldstein/b8e86e837c82457a0cef2e1d176d3bcc to your computer and use it in GitHub Desktop.
Remove Red Frames
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 | |
from moviepy.editor import VideoFileClip, concatenate_videoclips | |
def is_red_pixel(pixel): | |
blue,green,red = pixel | |
return red > 200 and green < 100 and blue < 100 | |
def process_image(image): | |
# Convert image to BGR format as used by OpenCV | |
image_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | |
top_left_pixel = image_bgr[0, 0] | |
print(top_left_pixel, is_red_pixel(top_left_pixel)) | |
return is_red_pixel(top_left_pixel) | |
def remove_red_frames(video_file, output_file): | |
clip = VideoFileClip(video_file) | |
clips_to_keep = [] | |
for t in np.arange(0, clip.duration, 1/clip.fps): | |
frame = clip.get_frame(t) | |
if not process_image(frame): | |
clips_to_keep.append(clip.subclip(t, t + 1/clip.fps)) | |
filtered_clip = concatenate_videoclips(clips_to_keep) | |
filtered_clip.write_videofile(output_file, codec="libx264", audio_codec="aac") | |
if __name__ == "__main__": | |
import sys | |
if len(sys.argv) != 2: | |
print("Usage: python remove_red_frames.py input_video") | |
sys.exit(1) | |
input_video = sys.argv[1] | |
output_video = "cut." + input_video | |
remove_red_frames(input_video, output_video) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment