got video thumbnails working, added another pip packages to the README, and edited the templates/files.html accordingly.
This commit is contained in:
2
README
2
README
@@ -8,4 +8,4 @@ pip packages:
|
||||
* pymediainfo
|
||||
* PIL (should be there by default)
|
||||
* ExifRead
|
||||
*
|
||||
* opencv-python
|
||||
|
||||
44
files.py
44
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 #
|
||||
|
||||
@@ -10,7 +10,8 @@
|
||||
{% 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>
|
||||
{% 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 %}
|
||||
<figcaption class="figure-caption text-center">{{obj.name}}</figcaption>
|
||||
</figure>
|
||||
|
||||
Reference in New Issue
Block a user