Store thumbnails width in annotations
authorThierry Florac <thierry.florac@onf.fr>
Fri, 04 Dec 2015 16:37:02 +0100
changeset 24 0f4851b0aef3
parent 23 d49c3f386782
child 25 3ee119b3968f
Store thumbnails width in annotations
src/pyams_media/video.py
--- a/src/pyams_media/video.py	Fri Dec 04 16:36:30 2015 +0100
+++ b/src/pyams_media/video.py	Fri Dec 04 16:37:02 2015 +0100
@@ -40,6 +40,7 @@
 
 
 THUMBNAIL_ANNOTATION_KEY = 'pyams_media.video.thumbnail'
+THUMBNAIL_SIZE_ANNOTATION_KEY = 'pyams_media.video.thumbnail_size'
 
 
 @adapter_config(context=IVideo, provides=IThumbnail)
@@ -48,19 +49,24 @@
 
     def __init__(self, video):
         self.video = video
-        annotations = IAnnotations(video)
+        annotations = self.annotations = IAnnotations(video)
         self.thumbnail = annotations.get(THUMBNAIL_ANNOTATION_KEY)
 
     def get_image_size(self):
-        if self.thumbnail is not None:
-            return self.thumbnail.get_image_size()
-        else:
-            mpeg = FFmpeg('ffprobe')
-            streams = mpeg.info(self.video).get('streams', ())
-            for stream in streams:
-                if stream.get('codec_type') != 'video':
-                    continue
-                return stream.get('width'), stream.get('height')
+        image_size = self.annotations.get(THUMBNAIL_SIZE_ANNOTATION_KEY)
+        if image_size is None:
+            if self.thumbnail is not None:
+                image_size = self.thumbnail.get_image_size()
+            else:
+                mpeg = FFmpeg('avprobe')
+                streams = mpeg.info(self.video).get('streams', ())
+                for stream in streams:
+                    if stream.get('codec_type') != 'video':
+                        continue
+                    image_size = stream.get('width'), stream.get('height')
+            if image_size is not None:
+                self.annotations[THUMBNAIL_SIZE_ANNOTATION_KEY] = image_size
+        return image_size
 
     def get_thumbnail_size(self, thumbnail_name, forced=False):
         if self.thumbnail is not None:
@@ -104,7 +110,7 @@
 
     def get_thumbnail(self, thumbnail_name, format=None, time=5):
         if self.thumbnail is None:
-            pipe = subprocess.Popen(('ffmpeg', '-i', '-', '-ss', str(time), '-f', 'image2', '-vframes', '1', '-'),
+            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)
@@ -114,7 +120,7 @@
                     output = NamedTemporaryFile(prefix='video_', suffix='.thumb')
                     output.write(self.video.data)
                     output.file.flush()
-                    pipe = subprocess.Popen(('ffmpeg', '-i', output.name, '-ss', str(time), '-f', 'image2',
+                    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: