From 050ce696170ff3efb9cccebaf9960c2234d167e9 Mon Sep 17 00:00:00 2001 From: Cam Date: Mon, 11 Jan 2021 12:04:57 +1100 Subject: [PATCH 1/2] added generation of video thumbnails --- files.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/files.py b/files.py index c3e3d10..1da3ba4 100644 --- a/files.py +++ b/files.py @@ -13,6 +13,7 @@ import hashlib import exifread import base64 import numpy +import cv2 ################################################################################ # Local Class imports @@ -76,6 +77,42 @@ class FileData(): except: return False + 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"] + 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 # # multiple valid paths in import_path) # @@ -106,6 +143,7 @@ class FileData(): fthumbnail = self.getExif(file) elif self.isVideo(file): ftype = 'Video' + fthumbnail = self.generateVideoThumbnail(file) else: ftype = 'File' From eeb24611c8496a78f0be0938cd9be01fa80787fb Mon Sep 17 00:00:00 2001 From: Cam Date: Mon, 11 Jan 2021 12:50:31 +1100 Subject: [PATCH 2/2] got video thumbnails working, added another pip packages to the README, and edited the templates/files.html accordingly. --- README | 2 +- files.py | 44 +++++++++++++------------------------------- templates/files.html | 3 ++- 3 files changed, 16 insertions(+), 33 deletions(-) diff --git a/README b/README index 7717f70..3d805c8 100644 --- a/README +++ b/README @@ -8,4 +8,4 @@ pip packages: * pymediainfo * PIL (should be there by default) * ExifRead - * + * opencv-python diff --git a/files.py b/files.py index 1da3ba4..0f3c467 100644 --- a/files.py +++ b/files.py @@ -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 # diff --git a/templates/files.html b/templates/files.html index c953fce..37e8348 100644 --- a/templates/files.html +++ b/templates/files.html @@ -10,7 +10,8 @@ {% if obj.type=="Image" %} {% elif obj.type == "Video" %} - + + {% endif %}
{{obj.name}}