Created
November 15, 2022 22:24
-
-
Save dennisseah/618c069d0edf203345fff365e9455456 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import cv2 | |
import json | |
import os | |
import sys | |
def metadata(filename, video): | |
return { | |
"file_name": filename.split(os.path.sep)[-1], | |
"frame_rate": int(video.get(cv2.CAP_PROP_FPS)), | |
"frame_count": int(video.get(cv2.CAP_PROP_FRAME_COUNT)), | |
"frame_width": int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), | |
"frame_height": int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)), | |
"bit_rate": int(video.get(cv2.CAP_PROP_BITRATE)) | |
} | |
def main(): | |
parser = argparse.ArgumentParser( | |
prog="Video2JPEG", | |
description="capture frames in video to JPEG files") | |
parser.add_argument("-i", "--input-file", required=True, | |
help="path to video file") | |
parser.add_argument("-g", "--grey_scale", action="store_true", | |
help="having images in grey scale") | |
parser.add_argument("-s", "--frames-to-skip", type=int, | |
default=0, help="number frame to skip") | |
args = parser.parse_args() | |
filename = args.input_file | |
skip = 0 if args.frames_to_skip == 0 else args.frames_to_skip + 1 | |
video = cv2.VideoCapture(filename) | |
print(json.dumps(metadata(filename, video), indent=4)) | |
success, image = video.read() | |
count = 0 | |
while success: | |
if skip == 0 or count % skip == 0: | |
# save frame as JPEG file | |
if args.grey_scale: | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
cv2.imwrite("frame%d.jpg" % count, image) | |
success, image = video.read() | |
count += 1 | |
video.release() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment