Filter frames by filesize #1

This commit is contained in:
Oscar Blue 2022-02-14 12:03:49 +00:00
parent 0e477519fe
commit 7d7ea1196f

View file

@ -0,0 +1,56 @@
import cv2
import sys
import argparse
import os
import pathlib
# Process arguments
def parse_arguments(argv=None):
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', dest='inputfile', type=pathlib.Path, help='Specify a video file')
parser.add_argument('-o', '--output', dest='outputfolder', type=pathlib.Path, help='Specify a folder to save frames to')
return parser.parse_args()
# Convert video to frames
def video_to_frames():
capture = cv2.VideoCapture(str(inputfile))
success,image = capture.read()
count = 0
while success:
outputfile = outputfolder + "/frame%d.jpg" % count
print(outputfile)
cv2.imwrite(outputfile, image)
success,image = capture.read()
count +=1
# Shrink set based on filesize
def display_file_sizes():
filesizes = []
for filename in os.listdir(outputfolder):
filepath = outputfolder + "/" + filename
filesize = os.path.getsize(filepath)
print(filepath + ": " + str(filesize))
filesizes.append(filesize)
# work out average
average = sum(filesizes)/len(filesizes)
print ("Average is: " + str(average))
# delete files below average
count = 0
for filename in os.listdir(outputfolder):
filepath = outputfolder + "/" + filename
if filesizes[count] < average:
# print(filepath + ": " + str(filesizes[count]))
os.remove(filepath)
count += 1
#def remove_similar_frames():
# sad
args = parse_arguments()
inputfile = str(args.inputfile.absolute())
outputfolder = str(args.outputfolder.absolute())
# Convert video to frames
video_to_frames()
display_file_sizes()