--- a/src/pyams_media/interfaces/__init__.py Mon Feb 18 16:12:44 2019 +0100
+++ b/src/pyams_media/interfaces/__init__.py Mon Sep 28 15:24:32 2020 +0200
@@ -105,6 +105,9 @@
def has_conversion(self, formats):
"""Check if one of given formats is available in conversions"""
+ def get_sources(self, request):
+ """Get media sources in JSON format"""
+
class IMediaConversion(Interface):
"""Marker interface for already converted media files"""
--- a/src/pyams_media/media.py Mon Feb 18 16:12:44 2019 +0100
+++ b/src/pyams_media/media.py Mon Sep 28 15:24:32 2020 +0200
@@ -12,7 +12,9 @@
__docformat__ = 'restructuredtext'
+import json
import re
+from collections import OrderedDict
from mimetypes import guess_extension, guess_type
from pyramid.events import subscriber
@@ -27,12 +29,18 @@
from pyams_file.interfaces import IFile
from pyams_media.ffbase import FFmpeg
from pyams_media.ffdocument import FFDocument
-from pyams_media.interfaces import CUSTOM_AUDIO_TYPES, CUSTOM_VIDEO_TYPES, IMediaConversion, IMediaConversionUtility, \
- IMediaConversions, IMediaInfo, MEDIA_CONVERSIONS_KEY, VIDEO_FRAME_SIZE
-from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config, get_annotation_adapter
+from pyams_media.interfaces import CUSTOM_AUDIO_TYPES, CUSTOM_VIDEO_TYPES, IMediaConversion, \
+ IMediaConversionUtility, IMediaConversions, IMediaInfo, MEDIA_CONVERSIONS_KEY, \
+ VIDEO_FRAME_SIZE
+from pyams_media.video import get_video_type
+from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config, \
+ get_annotation_adapter
+from pyams_utils.date import get_timestamp
from pyams_utils.factory import factory_config
from pyams_utils.interfaces.tales import ITALESExtension
from pyams_utils.registry import get_global_registry, query_utility
+from pyams_utils.url import absolute_url
+
#
# Media infos
@@ -80,10 +88,10 @@
content_type = target.content_type
if isinstance(content_type, bytes):
content_type = content_type.decode()
- target_name = '{name}{width}.{extension}'.format(name=content_type.split('/', 1)[0]
- if content_type else 'media',
- width='-{0}'.format(width) if width else '',
- extension=extension)
+ target_name = '{name}{width}.{extension}'.format(
+ name=content_type.split('/', 1)[0] if content_type else 'media',
+ width='-{0}'.format(width) if width else '',
+ extension=extension)
if target_name in self:
del self[target_name]
target.filename = target_name
@@ -113,6 +121,24 @@
return True
return False
+ def get_sources(self, request):
+ sources = []
+ types_order = ('video/mp4','video/webm','video/ogg','video/x-flv')
+ media_width = 0
+ previous_type = None
+ for conversion in self.get_conversions(order=types_order):
+ video_type = get_video_type(conversion, request.registry)
+ min_width = ((media_width or 0) + 1) if (video_type == previous_type) else 0
+ media_width = self.get_conversion_width(conversion.__name__)
+ media_src = '{0}?_={1}'.format(absolute_url(conversion, request),
+ get_timestamp(conversion))
+ sources.append({'type': video_type,
+ 'min': min_width,
+ 'max': media_width,
+ 'src': media_src})
+ previous_type = video_type
+ return json.dumps(sources)
+
@adapter_config(context=IFile, provides=IMediaConversions)
def media_conversions_factory(context):
@@ -129,7 +155,9 @@
return IMediaConversions(self.context)
-@adapter_config(name='conversions', context=(Interface, Interface, Interface), provides=ITALESExtension)
+@adapter_config(name='conversions',
+ context=(Interface, Interface, Interface),
+ provides=ITALESExtension)
class ConversionsExtension(ContextRequestViewAdapter):
"""extension:conversions(media) TALES extension"""