Skip to content

Instantly share code, notes, and snippets.

@fangzhangmnm
Created January 20, 2026 03:15
Show Gist options
  • Select an option

  • Save fangzhangmnm/61ab59201f32fac5bcd94c21a48cc701 to your computer and use it in GitHub Desktop.

Select an option

Save fangzhangmnm/61ab59201f32fac5bcd94c21a48cc701 to your computer and use it in GitHub Desktop.
import os
import cv2
from scenedetect import SceneManager, ContentDetector, open_video
from tqdm import tqdm
def extract_scenes_pro(video_path, output_folder="shots"):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
video = open_video(video_path)
print(dir(video))
total_frames = video.frame_number
print(f"Total frames to process: {total_frames}")
fps = video.frame_rate
min_duration_seconds = 1.0
min_frames = int(min_duration_seconds * fps)
scene_manager = SceneManager()
scene_manager.add_detector(ContentDetector(threshold=27.0, min_scene_len=min_frames))
scene_manager.auto_downscale = True
scene_manager.detect_scenes(video, show_progress=True)
scene_list = scene_manager.get_scene_list()
print(f"\nFound {len(scene_list)} unique shots.")
cap = cv2.VideoCapture(video_path)
for i, scene in enumerate(tqdm(scene_list, desc="Saving JPGs")):
start_frame = scene[0].get_frames()
cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
ret, frame = cap.read()
if ret:
# Format: shots/shot_001_at_00-05-22.jpg
timestamp = scene[0].get_timecode().replace(":", "-").replace(".", "_")
cv2.imwrite(f"{output_folder}/shot_{i+1:03d}_{timestamp}.jpg", frame)
cap.release()
print("Done!")
extract_scenes_pro(input("Enter the path to the video file: "),
input("Enter the output folder (default 'shots'): ") or "shots")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment