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

2
README
View File

@@ -8,4 +8,4 @@ pip packages:
* pymediainfo * pymediainfo
* PIL (should be there by default) * PIL (should be there by default)
* ExifRead * ExifRead
* * opencv-python

View File

@@ -79,39 +79,21 @@ class FileData():
def generateVideoThumbnail(self, file): def generateVideoThumbnail(self, file):
#overall wrapper function for generating video thumbnails #overall wrapper function for generating video thumbnails
frame = self.video_to_frames(file)[0] vcap = cv2.VideoCapture(file)
fthumbnail = self.image_to_thumbs(frame)["160"] 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 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 # # HACK: At present this only handles one path (need to re-factor if we have #

View File

@@ -10,7 +10,8 @@
{% if obj.type=="Image" %} {% if obj.type=="Image" %}
<a href="{{file_data.symlink}}/{{obj.name}}"><img width="128" height="128" src="data:image/jpeg;base64,{{obj.thumbnail}}"></img></a> <a href="{{file_data.symlink}}/{{obj.name}}"><img width="128" height="128" src="data:image/jpeg;base64,{{obj.thumbnail}}"></img></a>
{% elif obj.type == "Video" %} {% elif obj.type == "Video" %}
<i style="font-size:128px;" class="fas fa-file-video"></i> <img width="128" height="128" src="data:image/jpeg;base64,{{obj.thumbnail}}"></img>
<!--<i style="font-size:128px;" class="fas fa-file-video"></i>-->
{% endif %} {% endif %}
<figcaption class="figure-caption text-center">{{obj.name}}</figcaption> <figcaption class="figure-caption text-center">{{obj.name}}</figcaption>
</figure> </figure>