Added default conversion filters
authorThierry Florac <thierry.florac@onf.fr>
Tue, 08 Sep 2015 16:59:10 +0200
changeset 5 73e9b218db71
parent 4 36c3db8c50a4
child 6 b78dab193043
Added default conversion filters
src/pyams_media/converter.py
--- a/src/pyams_media/converter.py	Tue Sep 08 16:30:58 2015 +0200
+++ b/src/pyams_media/converter.py	Tue Sep 08 16:59:10 2015 +0200
@@ -20,7 +20,8 @@
 from tempfile import NamedTemporaryFile
 
 # import interfaces
-from pyams_media.interfaces import IMediaVideoConverter, IMediaAudioConverter, IMediaConversionUtility, IMediaConverter
+from pyams_media.interfaces import IMediaVideoConverter, IMediaAudioConverter, IMediaConversionUtility, IMediaConverter, \
+    VIDEO_FRAME_SIZE
 
 # import packages
 from pyams_media.ffdocument import FFDocument
@@ -56,7 +57,7 @@
             if isinstance(media, str):
                 media = StringIO(media)
             document = FFDocument(media)
-        self.add_filters(document)
+        self.add_common_filters(document)
         for loop in self.get_conversion_loop(document):
             if self.require_temp_file:
                 output = NamedTemporaryFile(prefix='media_', suffix='.%s' % self.format)
@@ -66,10 +67,14 @@
             else:
                 yield loop, document.get_output(self.format)
 
+    def add_common_filters(self, document):
+        pass
+
     def add_filters(self, document):
         pass
 
     def get_conversion_loop(self, document):
+        self.add_filters(document)
         yield None
 
 
@@ -81,6 +86,15 @@
 class BaseAudioConverter(BaseMediaConverter):
     """Base media converter"""
 
+    def add_common_filters(self, document):
+        super(BaseAudioConverter, self).add_common_filters(document)
+        utility = query_utility(IMediaConversionUtility)
+        if utility is not None:
+            if utility.audio_sampling:
+                document.audiosampling(utility.audio_sampling)
+            if utility.audio_bitrate:
+                document.audiobitrate(utility.audio_bitrate)
+
 
 @utility_config(name='audio/wav', provides=IMediaConverter)
 class WavAudioConverter(BaseAudioConverter):
@@ -139,9 +153,22 @@
     def get_conversion_loop(self, document):
         utility = query_utility(IMediaConversionUtility)
         if utility is not None:
+            # Get video resolution
+            video_info = document.get_stream_info('video')
+            if video_info is not None:
+                frame_width = video_info.get('width')
+            else:
+                frame_width = (0, 0)
+            # Check same resolution conversion
+            if video_info.get('format', {}).get('format_name') != self.format:
+                yield None
+            # Convert to lower resolutions
+            self.add_filters(document)
             for size in utility.video_frame_size or ():
-                document.size(size)
-                yield size
+                target_width = VIDEO_FRAME_SIZE.get(size, (0, 0))[0]
+                if target_width < frame_width:
+                    document.size(size)
+                    yield size
 
 
 @utility_config(name='video/x-flv', provides=IMediaConverter)
@@ -151,6 +178,14 @@
     label = _("FLV (Flash Video) video converter")
     format = 'flv'
 
+    def add_common_filters(self, document):
+        super(FlvVideoConverter, self).add_common_filters(document)
+        # Audio sampling is required by FLV converter!
+        utility = query_utility(IMediaConversionUtility)
+        if utility is not None:
+            if utility.video_audio_sampling:
+                document.audiosampling(utility.video_audio_sampling)
+
 
 @utility_config(name='video/mp4', provides=IMediaConverter)
 class Mp4VideoConverter(BaseVideoConverter):
@@ -160,6 +195,16 @@
     format = 'mp4'
     require_temp_file = True
 
+    def add_common_filters(self, document):
+        effects = document.__effects__
+        effects['filter:v'] = 'setsar=1/1'
+        effects['pix_fmt'] = 'yuv420p'
+        effects['preset:v'] = 'slow'
+        effects['profile:v'] = 'baseline'
+        effects['x264opts'] = 'level=3.0:ref=1'
+        effects['r:v'] = '25/1'
+        effects['movflags'] = '+faststart'
+
 
 @utility_config(name='video/ogg', provides=IMediaConverter)
 class OggVideoConverter(BaseVideoConverter):
@@ -176,6 +221,12 @@
     label = _("WebM video converter")
     format = 'webm'
 
+    def add_common_filters(self, document):
+        effects = document.__effects__
+        effects['filter:v'] = 'setsar=1/1'
+        effects['pix_fmt'] = 'yuv420p'
+        effects['r:v'] = '25/1'
+
 
 class VideoConvertersVocabulary(SimpleVocabulary):
     """Video converters vocabulary"""