Skip to content

Instantly share code, notes, and snippets.

@Absolucy
Created June 4, 2023 21:46
Show Gist options
  • Save Absolucy/f3d4ab4c652cc6112da760f8fabb1a58 to your computer and use it in GitHub Desktop.
Save Absolucy/f3d4ab4c652cc6112da760f8fabb1a58 to your computer and use it in GitHub Desktop.
beestation - clip out mentor chat
import cv2
import numpy as np
import subprocess
import sys
import os
# the pixels to check
pixel_positions = [(1615, 727), (1705, 728)] # example positions
# the minimum color value to check for (in BGR)
min_color_value = [200, 200, 200]
# load the video
cap = cv2.VideoCapture(sys.argv[1])
# store start times of sections to keep
keep_times = []
start_time = None
frame_rate = cap.get(cv2.CAP_PROP_FPS)
while(cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
current_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000.0
# check if the pixels are the right color
if any(np.all(frame[y, x] >= min_color_value) for x, y in pixel_positions):
# if we're not already in a segment to cut, start one
if start_time is not None:
keep_times.append((start_time, current_time))
start_time = None
else:
# if we're not in a segment to keep, start one
if start_time is None:
start_time = current_time
# make sure to keep the segment we were in when the video ended
if start_time is not None:
keep_times.append((start_time, current_time))
print(keep_times)
cap.release()
# cut the video
for i, (start, end) in enumerate(keep_times):
subprocess.call(f'ffmpeg -y -i {sys.argv[1]} -ss {start} -to {end} -c copy clip{i}{sys.argv[1][-4:]}', shell=True)
# create a list of files for ffmpeg to concatenate
with open('list.txt', 'w') as f:
for i in range(len(keep_times)):
f.write(f"file 'clip{i}{sys.argv[1][-4:]}'\n")
# concatenate the clips
subprocess.call(f'ffmpeg -y -f concat -i list.txt -c copy {sys.argv[2]}', shell=True)
for i in range(len(keep_times)):
os.remove(f'clip{i}{sys.argv[1][-4:]}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment