Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sakthivelj committed Jun 6, 2023
1 parent 09aee9c commit 708f237
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='video2image',
version='1.2.0',
version='1.3.0',
author='Sakthivel J',
author_email='[email protected]',
description='A Python script to extract frames from video files.',
Expand Down
7 changes: 4 additions & 3 deletions video2image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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")
Expand Down
55 changes: 36 additions & 19 deletions video2image/converter.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,71 @@
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)
frame_folder = os.path.join(output_folder, os.path.basename(parent_folder), video_name)
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()

0 comments on commit 708f237

Please sign in to comment.