diff --git a/README.md b/README.md index 6f9cbe5..70b44b8 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,7 @@ More information on contributing and the general code of conduct for discussion | Word to PDF | [Word to PDF](https://github.com/DhanushNehru/Python-Scripts/tree/main/Word%20to%20PDF%20converter) | A Python script to convert an MS Word file to a PDF file. | | Youtube Downloader | [Youtube Downloader](https://github.com/DhanushNehru/Python-Scripts/tree/main/Youtube%20Downloader) | Downloads any video from [YouTube](https://youtube.com) in video or audio format! | | Youtube Playlist Info Scraper | [Youtube Playlist Info Scraper](https://github.com/DhanushNehru/Python-Scripts/tree/main/Youtube%20Playlist%20Info%20Scraper) | This python module retrieve information about a YouTube playlist in json format using playlist link. | +| Random Frame extractor | [Random Frame extractor ](https://github.com/DhanushNehru/Python-Scripts/tree/main/Random%Frame%20Extractor) | This python based command prompt will help you generate n number of frame from a video as an input. | ## Gitpod diff --git a/Random Frame Extractor/README.md b/Random Frame Extractor/README.md new file mode 100644 index 0000000..e69de29 diff --git a/Random Frame Extractor/RandomFrameExtractor.py b/Random Frame Extractor/RandomFrameExtractor.py new file mode 100644 index 0000000..bd3ed9c --- /dev/null +++ b/Random Frame Extractor/RandomFrameExtractor.py @@ -0,0 +1,57 @@ +import cv2 +import os +import random +import uuid +import argparse + +def extract_random_frames(video_path, num_frames=15, output_folder="./datasets/images"): + # Create output directory if it doesn't exist + os.makedirs(output_folder, exist_ok=True) + + # Load video + cap = cv2.VideoCapture(video_path) + if not cap.isOpened(): + print("Error: Could not open video.") + return + + total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) + if total_frames < num_frames: + print(f"Video has only {total_frames} frames. Reducing number of outputs.") + num_frames = total_frames + + # Generate a short UUID for file prefix + video_id = str(uuid.uuid4().hex)[:8] + + # Select random frame indices + selected_frames = sorted(random.sample(range(total_frames), num_frames)) + print(f"Extracting frames at indices: {selected_frames}") + + frame_idx = 0 + save_idx = 1 + + while cap.isOpened(): + ret, frame = cap.read() + if not ret: + break + + if frame_idx in selected_frames: + filename = os.path.join(output_folder, f"{video_id}_{save_idx}.png") + cv2.imwrite(filename, frame) + print(f"Saved: {filename}") + save_idx += 1 + + frame_idx += 1 + if save_idx > num_frames: + break + + cap.release() + print("Done.") + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Extract random frames from a video.") + parser.add_argument("video_path", help="Path to the input video file") + parser.add_argument("num_frames", type=int, help="Number of random frames to extract") + parser.add_argument("--output", default="./datasets/images", help="Output folder for images (default: ./datasets/images)") + + args = parser.parse_args() + extract_random_frames(args.video_path, args.num_frames, args.output)