def file_to_video(self, f, **kwargs): mimetype = f.get_mimetype() if Video.mimetype_is_video(mimetype): v = Video.Video(URI=f._get_text_uri()) v.set_from_instance(f) if len(kwargs) > 0: return self.transcode_video(v, **kwargs) else: return v else: return None
def transcode_video(self, video, **kwargs): #FIXME: This code fails with flv. Should we add an exception? mimetype = video.get_mimetype() if not Video.mimetype_is_video(mimetype): log.debug("File %s is not video type: %s" % (video, mimetype)) return None #Check if we need to convert the video if kwargs.get('mimetype', None) == mimetype: #Check if the video is smaller or equal then the required dimensions #If it does, we dont need to convert it width = kwargs.get('width', None) height = kwargs.get('height', None) if width or height: (video_width, video_height) = video.get_video_size() if (not width or video_width <= width) and \ (not height or video_height <= height): log.debug( "Video matches the required dimensions, not converting" ) return video else: #There is no required dimensions, and we match the mimetype, #so we dont convert it log.debug("Video matches the mimetype, not converting") return video kwargs['in_file'] = video.get_local_uri() kwargs['out_file'] = self._get_output_file(kwargs['in_file'], **kwargs) if os.path.exists(kwargs['out_file']): log.debug('Converted video already exists, using it') return Video.Video(kwargs['out_file']) if 'width' in kwargs and 'height' in kwargs: (width, height) = video.get_video_size() if not width and not height: log.debug("Can't get video dimensions") return None kwargs['width'], kwargs['height'] = Utils.get_proportional_resize( desiredW=int(kwargs['width']), desiredH=int(kwargs['height']), currentW=int(width), currentH=int(height)) log.debug("Scaling video to %swx%sh" % (kwargs['width'], kwargs['height'])) try: gst_converter = GStreamerConverter() sucess = gst_converter.convert(**kwargs) except Exception, e: log.debug("Error transcoding video: %s" % e) return None
def _is_allowed_type(self, mimetype): ok = False if not ok and self.downloadPhotos: ok = Photo.mimetype_is_photo(mimetype) if not ok and self.downloadAudio: ok = Audio.mimetype_is_audio(mimetype) if not ok and self.downloadVideo: ok = Video.mimetype_is_video(mimetype) return ok
def file_to_video(self, f, **kwargs): mimetype = f.get_mimetype() if Video.mimetype_is_video(mimetype): v = Video.Video( URI=f._get_text_uri() ) v.set_from_instance(f) if len(kwargs) > 0: return self.transcode_video(v,**kwargs) else: return v else: return None
def transcode_video(self, video, **kwargs): #FIXME: This code fails with flv. Should we add an exception? mimetype = video.get_mimetype() if not Video.mimetype_is_video(mimetype): log.debug("File %s is not video type: %s" % (video,mimetype)) return None #Check if we need to convert the video if kwargs.get('mimetype', None) == mimetype: #Check if the video is smaller or equal then the required dimensions #If it does, we dont need to convert it width = kwargs.get('width', None) height = kwargs.get('height', None) if width or height: (video_width, video_height) = video.get_video_size() if (not width or video_width <= width) and \ (not height or video_height <= height): log.debug("Video matches the required dimensions, not converting") return video else: #There is no required dimensions, and we match the mimetype, #so we dont convert it log.debug("Video matches the mimetype, not converting") return video kwargs['in_file'] = video.get_local_uri() kwargs['out_file'] = self._get_output_file(kwargs['in_file'], **kwargs) if os.path.exists(kwargs['out_file']): log.debug('Converted video already exists, using it') return Video.Video(kwargs['out_file']) if 'width' in kwargs and 'height' in kwargs: (width, height) = video.get_video_size() if not width and not height: log.debug("Can't get video dimensions") return None kwargs['width'],kwargs['height'] = Utils.get_proportional_resize( desiredW=int(kwargs['width']), desiredH=int(kwargs['height']), currentW=int(width), currentH=int(height) ) log.debug("Scaling video to %swx%sh" % (kwargs['width'],kwargs['height'])) try: gst_converter = GStreamerConverter() sucess = gst_converter.convert(**kwargs) except Exception, e: log.debug("Error transcoding video: %s" % e) return None
currentH=int(height)) log.debug("Scaling video to %swx%sh" % (kwargs['width'], kwargs['height'])) try: gst_converter = GStreamerConverter() sucess = gst_converter.convert(**kwargs) except Exception, e: log.debug("Error transcoding video: %s" % e) return None if not sucess: log.debug("Error transcoding video\n") return None return Video.Video(kwargs['out_file']) def transcode_audio(self, audio, **kwargs): mimetype = audio.get_mimetype() if not Audio.mimetype_is_audio(mimetype): log.debug("File %s is not audio type: %s" % (audio, mimetype)) return None kwargs['in_file'] = audio.get_local_uri() kwargs['out_file'] = self._get_output_file(kwargs['in_file'], **kwargs) if kwargs.get('mimetype', None) == mimetype: log.debug('No need to convert file') return audio #convert audio