Added "get_video_type()" function (extracted from "video_type" TALES extension)
authorThierry Florac <tflorac@ulthar.net>
Mon, 28 Sep 2020 15:25:24 +0200
changeset 131 41ec6731bf1b
parent 130 0ec5e1dc039c
child 132 2f7e36cb0e05
Added "get_video_type()" function (extracted from "video_type" TALES extension)
src/pyams_media/video.py
--- a/src/pyams_media/video.py	Mon Sep 28 15:24:32 2020 +0200
+++ b/src/pyams_media/video.py	Mon Sep 28 15:25:24 2020 +0200
@@ -12,31 +12,25 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 import os.path
 import subprocess
-
 from tempfile import NamedTemporaryFile
 
-# import interfaces
-from pyams_file.interfaces import IVideo, IThumbnails, IThumbnailFile
-from pyams_media.interfaces import IVideoType
-from pyams_utils.interfaces.tales import ITALESExtension
+import transaction
+from pyramid.threadlocal import get_current_registry
 from zope.annotation.interfaces import IAnnotations
+from zope.interface import Interface, alsoProvides
+from zope.lifecycleevent import ObjectAddedEvent, ObjectCreatedEvent
+from zope.location import locate
 from zope.traversing.interfaces import ITraversable
 
-# import packages
-import transaction
-
 from pyams_file.file import ImageFile, get_magic_content_type
 from pyams_file.image import ThumbnailGeometry
+from pyams_file.interfaces import IThumbnailFile, IThumbnails, IVideo
 from pyams_media.ffbase import FFmpeg
-from pyams_utils.adapter import adapter_config, ContextAdapter, ContextRequestViewAdapter
-from pyramid.threadlocal import get_current_registry
-from zope.interface import alsoProvides, Interface
-from zope.lifecycleevent import ObjectCreatedEvent, ObjectAddedEvent
-from zope.location import locate
+from pyams_media.interfaces import IVideoType
+from pyams_utils.adapter import ContextAdapter, ContextRequestViewAdapter, adapter_config
+from pyams_utils.interfaces.tales import ITALESExtension
 
 
 THUMBNAIL_ANNOTATION_KEY = 'pyams_media.video.thumbnail'
@@ -110,8 +104,15 @@
 
     def get_thumbnail(self, thumbnail_name, format=None, time=5):
         if self.thumbnail is None:
-            pipe = subprocess.Popen(('avconv', '-i', '-', '-ss', str(time), '-f', 'image2', '-vframes', '1', '-'),
-                                    stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+            pipe = subprocess.Popen(('avconv',
+                                     '-i', '-',
+                                     '-ss', str(time),
+                                     '-f', 'image2',
+                                     '-vframes', '1',
+                                     '-'),
+                                    stdin=subprocess.PIPE,
+                                    stdout=subprocess.PIPE,
+                                    stderr=subprocess.PIPE)
             if pipe:
                 stdout, stderr = pipe.communicate(self.video.data)
                 # Some videos formats can't be converted via pipes
@@ -120,9 +121,15 @@
                     output = NamedTemporaryFile(prefix='video_', suffix='.thumb')
                     output.write(self.video.data)
                     output.file.flush()
-                    pipe = subprocess.Popen(('avconv', '-i', output.name, '-ss', str(time), '-f', 'image2',
-                                             '-vframes', '1', '-'),
-                                            stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+                    pipe = subprocess.Popen(('avconv',
+                                             '-i', output.name,
+                                             '-ss', str(time),
+                                             '-f', 'image2',
+                                             '-vframes', '1',
+                                             '-'),
+                                            stdin=subprocess.PIPE,
+                                            stdout=subprocess.PIPE,
+                                            stderr=subprocess.PIPE)
                     if pipe:
                         stdout, stderr = pipe.communicate()
                 # Create final image
@@ -195,24 +202,30 @@
         return 'video/flash'
 
 
-@adapter_config(name='video_type', context=(Interface, Interface, Interface), provides=ITALESExtension)
+def get_video_type(context, registry):
+    """Get video type of given context"""
+    if not IVideo.providedBy(context):
+        return None
+    content_type = context.content_type
+    if isinstance(content_type, bytes):
+        content_type = content_type.decode()
+    adapter = registry.queryAdapter(context, IVideoType, name=content_type)
+    if adapter is None:
+        adapter = registry.queryAdapter(context, IVideoType)
+    if adapter is not None:
+        video_type = adapter.video_type
+        if isinstance(video_type, bytes):
+            video_type = video_type.decode()
+        return video_type
+
+
+@adapter_config(name='video_type',
+                context=(Interface, Interface, Interface),
+                provides=ITALESExtension)
 class VideoTypeExtension(ContextRequestViewAdapter):
     """extension:video_type(media) TALES extension"""
 
     def render(self, context=None):
         if context is None:
             context = self.context
-        if not IVideo.providedBy(context):
-            return None
-        registry = self.request.registry
-        content_type = context.content_type
-        if isinstance(content_type, bytes):
-            content_type = content_type.decode()
-        adapter = registry.queryAdapter(context, IVideoType, name=content_type)
-        if adapter is None:
-            adapter = registry.queryAdapter(context, IVideoType)
-        if adapter is not None:
-            video_type = adapter.video_type
-            if isinstance(video_type, bytes):
-                video_type = video_type.decode()
-            return video_type
+        return get_video_type(context, self.request.registry)