got video thumbnails working, added another pip packages to the README, and edited the templates/files.html accordingly.

This commit is contained in:
2021-01-11 12:50:31 +11:00
parent e730d56018
commit eeb24611c8
3 changed files with 16 additions and 33 deletions

View File

@@ -79,39 +79,21 @@ class FileData():
def generateVideoThumbnail(self, file):
#overall wrapper function for generating video thumbnails
frame = self.video_to_frames(file)[0]
fthumbnail = self.image_to_thumbs(frame)["160"]
vcap = cv2.VideoCapture(file)
res, im_ar = vcap.read()
while im_ar.mean() < 15 and res:
res, im_ar = vcap.read()
im_ar = cv2.resize(im_ar, (160, 90), 0, 0, cv2.INTER_LINEAR)
#save on a buffer for direct transmission
res, thumb_buf = cv2.imencode('.jpeg', im_ar)
# '.jpeg' etc are permitted
#get the bytes content
bt = thumb_buf.tostring()
fthumbnail = base64.b64encode(bt)
fthumbnail = str(fthumbnail)[2:-1]
print(fthumbnail)
return fthumbnail
def video_to_frames(self, video_filename):
#Extract frames from video
cap = cv2.VideoCapture(video_filename)
video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
frames = []
if cap.isOpened() and video_length > 0:
frame_ids = [0]
if video_length >= 4:
frame_ids = [round(video_length * 0.25)]
count = 0
success, image = cap.read()
while success:
if count in frame_ids:
frames.append(image)
success, image = cap.read()
count += 1
return frames
def image_to_thumbs(self, img):
#Create thumbs from image
height, width, channels = img.shape
thumbs = {"original": img}
sizes = [160]
for size in sizes:
if (width >= size):
r = (size + 0.0) / width
max_size = (size, int(height * r))
thumbs[str(size)] = cv2.resize(img, max_size, interpolation=cv2.INTER_AREA)
return thumbs
##############################################################################
# HACK: At present this only handles one path (need to re-factor if we have #