Skip to content

adding videos thumbnails support #399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions filebrowser/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.contrib import admin
from django.db import models

class browse(models.Model):
class Meta:
verbose_name_plural = "browse"

admin.site.register(browse)
28 changes: 20 additions & 8 deletions filebrowser/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from filebrowser.settings import (ADMIN_VERSIONS, DEFAULT_PERMISSIONS,
EXTENSIONS, IMAGE_MAXBLOCK, SELECT_FORMATS,
STRICT_PIL, VERSION_QUALITY, VERSIONS,
VERSIONS_BASEDIR)
from filebrowser.utils import get_modified_time, path_strip, process_image
VERSIONS_BASEDIR, VIDEO_THUMBNAIL)
from filebrowser.utils import get_modified_time, path_strip, process_image, get_video_image

from .namers import get_namer

Expand All @@ -31,7 +31,6 @@
import Image
import ImageFile


ImageFile.MAXBLOCK = IMAGE_MAXBLOCK # default is 64k


Expand Down Expand Up @@ -342,10 +341,13 @@ def url(self):
@cached_property
def dimensions(self):
"Image dimensions as a tuple"
if self.filetype != 'Image':
if self.filetype != 'Image' and self.filetype != 'Video':
return None
try:
im = Image.open(self.site.storage.open(self.path))
if VIDEO_THUMBNAIL and self.filetype == 'Video':
im = Image.open(get_video_image(self.path_full))
elif self.filetype == 'Image':
im = Image.open(self.site.storage.open(self.path))
return im.size
except:
pass
Expand Down Expand Up @@ -514,7 +516,17 @@ def _generate_version(self, version_path, version_suffix, options):
f = self.site.storage.open(self.path)
except IOError:
return ""
im = Image.open(f)

im = None

if VIDEO_THUMBNAIL and self.filetype == 'Video':
im = Image.open(get_video_image(self.path_full))
elif self.filetype == 'Image':
im = Image.open(f)

if not im:
return None

version_dir, version_basename = os.path.split(version_path)
root, ext = os.path.splitext(version_basename)
version = process_image(im, options)
Expand All @@ -526,9 +538,9 @@ def _generate_version(self, version_path, version_suffix, options):
version = m(version)

# IF need Convert RGB
if ext in [".jpg", ".jpeg"] and version.mode not in ("L", "RGB"):
if ext.lower() in [".jpg", ".jpeg"] and version.mode not in ("L", "RGB"):
version = version.convert("RGB")

# save version
quality = VERSIONS.get(version_suffix, {}).get("quality", VERSION_QUALITY)
try:
Expand Down
2 changes: 2 additions & 0 deletions filebrowser/namers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ def __init__(self, **kwargs):
setattr(self, k, v)

def get_version_name(self):
if self.file_object.filetype == 'Video':
return self.file_object.filename_root + "_" + self.version_suffix + '.jpg'
return self.file_object.filename_root + "_" + self.version_suffix + self.extension

def get_original_name(self):
Expand Down
5 changes: 4 additions & 1 deletion filebrowser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# EXTENSIONS AND FORMATS
# Allowed Extensions for File Upload. Lower case is important.
EXTENSIONS = getattr(settings, "FILEBROWSER_EXTENSIONS", {
'Image': ['.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff'],
'Image': ['.jpg', '.jpeg', '.gif', '.png', '.tif', '.tiff', '.heic'],
'Document': ['.pdf', '.doc', '.rtf', '.txt', '.xls', '.csv', '.docx'],
'Video': ['.mov', '.mp4', '.m4v', '.webm', '.wmv', '.mpeg', '.mpg', '.avi', '.rm'],
'Audio': ['.mp3', '.wav', '.aiff', '.midi', '.m4p']
Expand Down Expand Up @@ -48,6 +48,9 @@
# Which Version should be used as Admin-thumbnail.
ADMIN_THUMBNAIL = getattr(settings, 'FILEBROWSER_ADMIN_THUMBNAIL', 'admin_thumbnail')

VIDEO_THUMBNAIL = getattr(settings, 'FILEBROWSER_VIDEO_THUMBNAIL', True)
VIDEO_THUMBNAIL_FRAME = getattr(settings, 'FILEBROWSER_ADMIN_VIDEO_THUMBNAIL_FRAME', 10)

VERSION_PROCESSORS = getattr(settings, 'FILEBROWSER_VERSION_PROCESSORS', [
'filebrowser.utils.scale_and_crop',
])
Expand Down
2 changes: 1 addition & 1 deletion filebrowser/templates/filebrowser/custom_field.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load i18n fb_versions %}
<input id="{{ final_attrs.id }}" type="text" class="vFileBrowseField{% if final_attrs.class %} {{ final_attrs.class }}{% endif %}" name="{{ final_attrs.name }}" value="{{ value.path }}" {% for k, v in final_attrs.data_attrs.items %}{{k}}="{{v}}" {% endfor %}/><a href="javascript:FileBrowser.show('{{ final_attrs.id }}', '{{ url }}?pop=1{% if final_attrs.directory %}&amp;dir={{ final_attrs.directory|urlencode|urlencode }}{% endif %}{% if final_attrs.format %}&amp;type={{ final_attrs.format }}{% endif %}');" class="fb_show"></a>
{% if value.filetype == "Image" and value.exists %}
{% if value.filetype == "Image" or fileobject.filetype == "Video" and value.exists %}
{% version value.path final_attrs.ADMIN_THUMBNAIL as thumbnail_version %}
{% if thumbnail_version %}
<p class="preview" id="preview_{{ final_attrs.id }}">
Expand Down
2 changes: 1 addition & 1 deletion filebrowser/templates/filebrowser/custom_upload_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ul class="errorlist"><li>{% trans "File not found" %}</li></ul>
{% endif %}
</div>
{% if value.filetype == "Image" %}
{% if value.filetype == "Image" or fileobject.filetype == "Video" %}
{% version value.path final_attrs.ADMIN_THUMBNAIL as thumbnail_version %}
{% if thumbnail_version %}
<p class="preview" id="preview_{{ final_attrs.id }}">
Expand Down
4 changes: 2 additions & 2 deletions filebrowser/templates/filebrowser/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h2 class="grp-collapse-handler">{% trans "File Information" %}</h2>
</div>
</div>
</div>
{% if fileobject.filetype == "Image" %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" %}
<div class="grp-row">
<div class="l-2c-fluid l-d-4">
<div class="c-1"><label>{% trans "Size" %}</label></div>
Expand All @@ -136,7 +136,7 @@ <h2 class="grp-collapse-handler">{% trans "File Information" %}</h2>
{% endif %}
</fieldset>
{% endif %}
{% if fileobject.filetype == "Image" %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" %}
<fieldset class="grp-module grp-collapse grp-open">
<h2 class="grp-collapse-handler">{% trans "Image Versions" %}</h2>
{% if settings_var.ADMIN_THUMBNAIL %}
Expand Down
10 changes: 5 additions & 5 deletions filebrowser/templates/filebrowser/include/filelisting.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% for fileobject in page.object_list %}

<!-- THUMBNAIL-VERSION FOR IMAGE-OBJECT -->
{% if fileobject.filetype == "Image" %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" %}
{% version fileobject settings_var.ADMIN_THUMBNAIL as thumbnail_version %}
{% endif %}

Expand All @@ -16,7 +16,7 @@
<!-- select original -->
<button class="grp-button fb_selectlink" onclick="FileSubmit('{{ fileobject.path }}', '{{ fileobject.url }}', '{{ thumbnail_version.url }}', '{{ fileobject.filetype }}');">{% trans "Select" %}</button>
<!-- select versions -->
{% if fileobject.filetype == "Image" and settings_var.ADMIN_VERSIONS %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" and settings_var.ADMIN_VERSIONS %}
<div class="grp-pulldown-versions-container">
<a href="javascript://" class="grp-pulldown-versions-handler" title="{% trans "Versions" %}">&nbsp;</a>
<ul class="grp-pulldown-versions">
Expand All @@ -41,7 +41,7 @@
<!-- select original -->
<button class="grp-button fb_selectlink" onclick="FileBrowserDialogue.fileSubmit('{{ fileobject.url|escapejs }}');">{% trans "Select" %}</button>
<!-- select versions -->
{% if fileobject.filetype == "Image" and settings_var.ADMIN_VERSIONS %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" and settings_var.ADMIN_VERSIONS %}
<div class="grp-pulldown-versions-container">
<a href="javascript://" class="grp-pulldown-versions-handler" title="{% trans "Versions" %}">&nbsp;</a>
<ul class="grp-pulldown-versions">
Expand All @@ -65,7 +65,7 @@
<!-- select original -->
<button class="grp-button fb_selectlink" onclick="OpenFile(ProtectPath('{{ fileobject.url|escapejs }}'));return false;">{% trans "Select" %}</button>
<!-- select versions -->
{% if fileobject.filetype == "Image" and settings_var.ADMIN_VERSIONS %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" and settings_var.ADMIN_VERSIONS %}
<div class="grp-pulldown-versions-container">
<a href="javascript://" class="grp-pulldown-versions-handler" title="{% trans "Versions" %}">&nbsp;</a>
<ul class="grp-pulldown-versions">
Expand Down Expand Up @@ -93,7 +93,7 @@

<!-- THUMBNAIL -->
<td class="fb_thumbnail">
{% if fileobject.filetype == "Image" %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" %}
<a href="{{ fileobject.url }}" class="fb_viewlink"><img src="{{ thumbnail_version.url }}" title="{% trans 'View Image' %}" /></a>
{% endif %}
</td>
Expand Down
2 changes: 1 addition & 1 deletion filebrowser/templates/filebrowser/version.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

<!-- CONTENT -->
{% block content %}
{% if fileobject.filetype == "Image" %}
{% if fileobject.filetype == "Image" or fileobject.filetype == "Video" %}
{% version fileobject.path settings_var.ADMIN_THUMBNAIL as thumbnail_version %}
{% version fileobject.path query.version as image_version %}
{% ifequal query.pop '1' %} <!-- FileBrowseField -->
Expand Down
21 changes: 20 additions & 1 deletion filebrowser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from django.utils.module_loading import import_string
from filebrowser.settings import (CONVERT_FILENAME, NORMALIZE_FILENAME,
STRICT_PIL, VERSION_PROCESSORS)
STRICT_PIL, VERSION_PROCESSORS, VIDEO_THUMBNAIL_FRAME)

if STRICT_PIL:
from PIL import Image
Expand All @@ -19,6 +19,11 @@
except ImportError:
import Image

try:
import cv2
from io import BytesIO
except ImportError:
pass

def convert_filename(value):
"""
Expand Down Expand Up @@ -119,3 +124,17 @@ def get_modified_time(storage, path):
if hasattr(storage, "get_modified_time"):
return storage.get_modified_time(path)
return storage.modified_time(path)

def get_video_image(path):
v = cv2.VideoCapture(path)
vtf = VIDEO_THUMBNAIL_FRAME
if v.get(cv2.CAP_PROP_FRAME_COUNT) -1 < vtf:
vtf = v.get(cv2.CAP_PROP_FRAME_COUNT) -1
if v.set(cv2.CAP_PROP_POS_FRAMES, vtf):
success, frame = v.read()
if success:
success, buffer = cv2.imencode(".jpg", frame)
temp = BytesIO(buffer)
temp.seek(0)
return temp
return None