diff --git a/setup.py b/setup.py index 3bf1933..22fcb94 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='video2image', - version='1.2.0', + version='1.3.0', author='Sakthivel J', author_email='sakthivel1023@gmail.com', description='A Python script to extract frames from video files.', diff --git a/video2image/__init__.py b/video2image/__init__.py index 301b852..9100fa3 100644 --- a/video2image/__init__.py +++ b/video2image/__init__.py @@ -10,7 +10,8 @@ def main(): parser.add_argument("-o", "--output", default="", help="Path to the output directory\n" "(default: create a directory with the same name as the input video in the input directory)") parser.add_argument("-p", "--parent", action="store_true", help="Include the parent directory of the video in the output directory path") - parser.add_argument("-H", "--show-help", action="help", help="Show this help message and exit") # Modified option string + parser.add_argument("-H", "--show-help", action="help", help="Show this help message and exit") + parser.add_argument("-f", "--format", default="jpg", choices=["jpg", "png"], help="Image format for extracted frames (default: jpg)") args = parser.parse_args() # Use input video directory as output if -o is empty @@ -19,7 +20,7 @@ def main(): # Handle single video file if os.path.isfile(args.input): - extract_frames(args.input, args.output, args.parent) + extract_frames(args.input, args.output, args.parent, args.format) # Handle directory with multiple video files elif os.path.isdir(args.input): @@ -28,7 +29,7 @@ def main(): print("No video files found in the input directory") else: for video_file in video_files: - extract_frames(video_file, args.output, args.parent) + extract_frames(video_file, args.output, args.parent, args.format) else: print("Invalid input: not a file or directory") diff --git a/video2image/converter.py b/video2image/converter.py index ef54e58..5a3b1e7 100644 --- a/video2image/converter.py +++ b/video2image/converter.py @@ -1,14 +1,15 @@ import os import cv2 import shutil +from tqdm import tqdm -def extract_frames(video_path, output_folder, include_parent=False): +def extract_frames(video_path, output_folder, include_parent=False, image_format="jpg"): # Create the output folder if it doesn't exist os.makedirs(output_folder, exist_ok=True) - + # Get the video file name without extension video_name = os.path.splitext(os.path.basename(video_path))[0] - + if include_parent: # Include the parent directory in the output folder path parent_folder = os.path.dirname(video_path) @@ -16,39 +17,55 @@ def extract_frames(video_path, output_folder, include_parent=False): else: # Create a folder for the video frames directly in the output folder frame_folder = os.path.join(output_folder, video_name) - + if os.path.exists(frame_folder): # If the folder already exists, remove it and create again shutil.rmtree(frame_folder) os.makedirs(frame_folder) - + # Open the video file video = cv2.VideoCapture(video_path) - + # Check if video file opened successfully if not video.isOpened(): print(f"Error opening video file: {video_path}") return - - # Initialize variables - frame_count = 0 - + + # Get video details + fps = video.get(cv2.CAP_PROP_FPS) + frame_count = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) + video_format = video.get(cv2.CAP_PROP_FORMAT) + video_size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))) + + print(f"Video Details:") + print(f" Format: {video_format}") + print(f" Size: {video_size}") + print(f" FPS: {fps}") + print(f" Frame Count: {frame_count}") + + # Set up progress bar + progress_bar = tqdm(total=frame_count, desc="Extracting Frames", unit="frame") + while True: # Read the next frame from the video ret, frame = video.read() - + # If frame reading was not successful, break the loop if not ret: break - + # Save the frame as an image - frame_path = os.path.join(frame_folder, f"frame_{frame_count:04d}.jpg") + frame_path = os.path.join(frame_folder, f"frame_{frame_count:04d}.{image_format}") cv2.imwrite(frame_path, frame) - - # Increment frame count - frame_count += 1 - + + # Update progress bar + progress_bar.update(1) + + # Decrement frame count + frame_count -= 1 + # Release the video object video.release() - - print(f"Frames extracted: {frame_count}") + + # Close progress bar + progress_bar.close()