def execute(self, workspace, # workspace object item_id, # item pk source_variant, # name of the variant output_variant, # name of the variat output_preset, # a mime type or a Mediadart PRESET **preset_params # additional parameters (see adapter server for explanation) ): log.info('%s.execute' % self) log.info('output_preset %s'%output_preset) log.info('self.presets %s'%self.presets) if output_preset not in self.presets: raise Exception('%s: unsupported output_preset' % (self, output_preset)) try: output_type = Type.objects.get_or_create_by_mime(self.presets[output_preset]) item, source = get_source_rendition(item_id, source_variant, workspace) output_variant_obj = Variant.objects.get(name = output_variant) output_component = item.create_variant(output_variant_obj, workspace, output_type) output_component.source = source output_file = get_storage_file_name(item.ID, workspace.pk, output_variant_obj.name, output_type.ext) except Exception, e: self.deferred.errback(Failure(e)) return
def get_cmdline(self, output_variant_name, output_preset, **preset_params): try: cmd = globals()['CMD_%s' % output_preset] except KeyError: raise Exception('%s: unsupported output_preset' % (output_preset)) av = self.source.get_features() self.cmdline = cmd['cmdline'] self.out_type = Type.objects.get_or_create_by_mime(cmd['mime']) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, self.out_type.ext) params = {} params.update(cmd['defaults']) params.update(preset_params) params.update({ 'in_filename': self.source.uri, 'out_filename': self.out_file, }) self.cmdline = self.cmdline % params if TBD in self.cmdline: raise Exception('missing required parameter from preset')
def execute( self, workspace, # workspace object item_id, # item pk source_variant, # name of the variant output_variant, # name of the variat output_preset, # a mime type or a Mediadart PRESET **preset_params # additional parameters (see adapter server for explanation) ): log.info("%s.execute" % self) log.info("output_preset %s" % output_preset) log.info("self.presets %s" % self.presets) if output_preset not in self.presets: raise Exception("%s: unsupported output_preset" % (self, output_preset)) try: output_type = Type.objects.get_or_create_by_mime(self.presets[output_preset]) item, source = get_source_rendition(item_id, source_variant, workspace) output_variant_obj = Variant.objects.get(name=output_variant) output_component = item.create_variant(output_variant_obj, workspace, output_type) output_component.source = source output_file = get_storage_file_name(item.ID, workspace.pk, output_variant_obj.name, output_type.ext) except Exception, e: self.deferred.errback(Failure(e)) return
def get_cmdline(self, output_variant_name, output_extension, frame_w, frame_h, position): features = self.source.get_features() self.out_type = Type.objects.get_or_create_by_filename('foo%s' % output_extension) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, output_extension) thumb_w, thumb_h = resize_image(features.get_video_width(), features.get_video_height(), frame_w, frame_h ) real_pos = float(features.get_video_duration()) * (float(position)/100.) self.cmdline = CMDLINE % {'infile': self.source.uri, 'outfile':self.out_file, 'thumb_w':thumb_w, 'thumb_h':thumb_h, 'real_pos':real_pos}
def get_cmdline(self, output_variant_name, output_extension, max_size): global pipe1, pipe2 if max_size: pipe = pipe1 else: pipe = pipe2 self.out_type = Type.objects.get_or_create_by_filename('foo%s' % output_extension) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, output_extension) self.cmdline = pipe % {'infile': self.source.uri, 'outfile':self.out_file, 'xsize':max_size, 'ysize':max_size, }
def get_cmdline(self, output_variant_name, output_extension, max_size): global pipe1, pipe2 if max_size: pipe = pipe1 else: pipe = pipe2 self.out_type = Type.objects.get_or_create_by_filename( 'foo%s' % output_extension) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, output_extension) self.cmdline = pipe % { 'infile': self.source.uri, 'outfile': self.out_file, 'xsize': max_size, 'ysize': max_size, }
def get_cmdline(self, output_variant, output_preset, **preset_params): try: cmd = globals()['CMD_%s' % output_preset] except KeyError: raise Exception('%s: unsupported output_preset' % (output_preset)) av = self.source.get_features() self.cmdline = cmd['cmdline'] self.out_type = Type.objects.get_or_create_by_mime(cmd['mime']) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant, self.out_type.ext) params = {} params.update(cmd['defaults']) params.update(preset_params) w, h = resize_image(av.get_video_width(), av.get_video_height(), int(params.get('video_width', 0)), int(params.get('video_height', 0))) params.update({ 'in_filename': self.source.uri, 'out_filename': self.out_file, 'video_width': w, 'video_height': h, }) if not av.has_audio(): try: # try to create a silent audio duration = float(av.get_video_duration()) params.update({ 'num_buffers': str(int(math.ceil(duration * (44100. / 2100.)))) }) self.cmdline = synthetic_audio(self.cmdline) except: # for some reason we could not get the video duration self.cmdline = rm_audio(self.cmdline) self.cmdline = self.cmdline % params if TBD in self.cmdline: raise Exception('missing required parameter from preset')
def get_cmdline(self, output_variant_name, output_extension, frame_w, frame_h, position): features = self.source.get_features() self.out_type = Type.objects.get_or_create_by_filename( 'foo%s' % output_extension) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, output_extension) thumb_w, thumb_h = resize_image(features.get_video_width(), features.get_video_height(), frame_w, frame_h) real_pos = float( features.get_video_duration()) * (float(position) / 100.) self.cmdline = CMDLINE % { 'infile': self.source.uri, 'outfile': self.out_file, 'thumb_w': thumb_w, 'thumb_h': thumb_h, 'real_pos': real_pos }
def _upload_variant(item, variant, workspace, user, file_name, file): """ """ try: logger.debug('user %s'%user) #TODO: refresh url in gui, otherwise old variant will be shown if not isinstance(file_name, unicode): file_name = unicode(file_name, 'utf-8') ext = os.path.splitext(file_name)[1] res_id = item.ID final_file_name = get_storage_file_name(res_id, workspace.pk, variant.name, ext) final_path = os.path.join(settings.MEDIADART_STORAGE, final_file_name) logger.debug('before move') if hasattr(file, 'file_name'): tmp_file_name = file.file_name else: tmp_file_name = file.name shutil.move(tmp_file_name , final_path) media_type = Type.objects.get_or_create_by_filename(file_name) _create_variant(file_name, final_file_name,media_type, item, workspace, variant) uploaders = [] if variant.name == 'original': triggers_name = 'upload' params = {} else: triggers_name = 'replace_rendition' params = {'*':{'source_variant_name': variant.name}} _run_pipelines([item], triggers_name, user, workspace, params) except Exception, ex: logger.exception(ex) raise ex
def get_cmdline(self, output_variant_name, output_preset, **preset_params): try: cmd = globals()['CMD_%s' % output_preset] except KeyError: raise Exception('%s: unsupported output_preset' % (output_preset)) av = self.source.get_features() self.cmdline = cmd['cmdline'] self.out_type = Type.objects.get_or_create_by_mime(cmd['mime']) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, self.out_type.ext) params = {} params.update(cmd['defaults']) params.update(preset_params) params.update({'in_filename': self.source.uri, 'out_filename': self.out_file,}) self.cmdline = self.cmdline % params if TBD in self.cmdline: raise Exception('missing required parameter from preset')
def _upload_variant(item, variant, workspace, user, file_name, file): """ """ try: logger.debug('user %s'%user) #TODO: refresh url in gui, otherwise old variant will be shown if not isinstance(file_name, unicode): file_name = unicode(file_name, 'utf-8') ext = os.path.splitext(file_name)[1] res_id = item.ID final_file_name = get_storage_file_name(res_id, workspace.pk, variant.name, ext) final_path = os.path.join(settings.MPROCESSOR_STORAGE, final_file_name) logger.debug('before move') if hasattr(file, 'file_name'): tmp_file_name = file.file_name else: tmp_file_name = file.name shutil.move(tmp_file_name , final_path) media_type = Type.objects.get_or_create_by_filename(file_name) _create_variant(file_name, final_file_name,media_type, item, workspace, variant) uploaders = [] if variant.name == 'original': triggers_name = 'upload' params = {} else: triggers_name = 'replace_rendition' params = {'*':{'source_variant_name': variant.name}} _run_pipelines([item], triggers_name, user, workspace, params) except Exception, ex: logger.exception(ex) raise ex
def get_cmdline(self, output_variant, output_preset, **preset_params): try: cmd = globals()['CMD_%s' % output_preset] except KeyError: raise Exception('%s: unsupported output_preset' % (output_preset)) av = self.source.get_features() self.cmdline = cmd['cmdline'] self.out_type = Type.objects.get_or_create_by_mime(cmd['mime']) self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant, self.out_type.ext) params = {} params.update(cmd['defaults']) params.update(preset_params) w, h = resize_image(av.get_video_width(), av.get_video_height(), int(params.get('video_width', 0)), int(params.get('video_height', 0))) params.update({'in_filename': self.source.uri, 'out_filename': self.out_file, 'video_width': w, 'video_height': h, }) if not av.has_audio(): try: # try to create a silent audio duration = float(av.get_video_duration()) params.update({'num_buffers': str(int(math.ceil(duration * (44100./2100.))))}) self.cmdline = synthetic_audio(self.cmdline) except: # for some reason we could not get the video duration self.cmdline = rm_audio(self.cmdline) self.cmdline = self.cmdline % params if TBD in self.cmdline: raise Exception('missing required parameter from preset')
def _create_items(dir_name, variant_name, user, workspace, make_copy=True, recursive = True, force_generation = False, link = False): """ """ logger.debug('########## _create_items') def copy_file(original_filename, final_path, link, make_copy): logger.debug('original_filename %s'%original_filename) logger.debug('final_path %s'%final_path) if link: logger.debug('link') #os.symlink(original_filename, final_path) if os.path.exists(final_path): os.remove(final_path) os.link(original_filename, final_path) elif make_copy: shutil.copyfile(original_filename, final_path) else: shutil.move(original_filename, final_path) variant = Variant.objects.get(name = variant_name) for entry in os.walk(dir_name): root_dir, sub_dirs, files = entry files = [os.path.join(root_dir, x) for x in files] for original_filename in files: res_id = new_id() try: media_type = Type.objects.get_or_create_by_filename(original_filename) except MimeError, ex: logger.error(ex) continue final_filename = get_storage_file_name(res_id, workspace.pk, variant.name, media_type.ext) final_path = os.path.join(settings.MEDIADART_STORAGE, final_filename) upload_filename = os.path.basename(original_filename) tmp = upload_filename.split('_') item, created = _create_item(user, workspace, res_id, media_type, original_filename) logger.debug('created %s'%created) logger.debug('+++++++++++++ force_generation %s'%force_generation) if created: logger.debug('file created') if len(tmp) > 1: upload_filename = '_'.join(tmp[1:]) _create_variant(upload_filename, final_filename, media_type, item, workspace, variant) logger.debug('yielding item %s'%item) copy_file(original_filename, final_path, link, make_copy) yield item elif force_generation: logger.debug('force_generation') component = variant.get_component(workspace, item) dam_hash = md5(component.get_file_path()) dir_hash = md5(original_filename) #if force_generation or dir_hash != dam_hash: logger.debug('component.get_file_path() %s'%component.get_file_path()) logger.debug('original_filename %s'%original_filename) logger.debug('dir_hash != dam_hash %s'%(dir_hash != dam_hash)) if dir_hash != dam_hash: copy_file(original_filename, component.get_file_path(), link, make_copy) yield item if not recursive: break
class AdaptImage(Adapter): remote_exe = 'convert' md_server = 'MediumLoad' #fake = True def get_cmdline(self, output_variant_name, output_extension, actions, resize_h, resize_w, crop_w, crop_h, crop_x, crop_y, crop_ratio, pos_x_percent, pos_y_percent, wm_id, rotation): if not isinstance(actions, list): actions = [actions] if output_extension == 'same_as_source': self.out_type = self.source.media_type output_extension = self.source.media_type.ext else: self.out_type = Type.objects.get_or_create_by_filename( 'foo%s' % output_extension) features = self.source.get_features() argv = "" new_rotation = 0 for action in actions: if action == 'resize': argv += '-auto-orient -resize %sx%s' % (resize_w, resize_h) elif action == 'crop': if crop_ratio: x, y = crop_ratio.split(':') argv += ' -gravity center -crop %sx%s%%+0+0' % (x, y) else: crop_x = crop_x or 0 # here None means 0 crop_y = crop_y or 0 argv += ' -gravity center -crop %sx%s+%s+%s' % ( int(crop_w), int(crop_h), int(crop_x), int(crop_y)) elif action == 'watermark': pos_x = int( float(pos_x_percent) * float(features.get_width()) / 100.) pos_y = int( float(pos_y_percent) * float(features.get_height()) / 100.) argv += ' -gravity NorthWest "file://%s" -geometry +%s+%s -composite' % ( wm_id, pos_x, pos_y) elif action == 'rotate': new_rotation = int(rotation) try: ws = self.workspace variant = Variant.objects.get(name=output_variant_name) component = self.item.get_variant(ws, variant) schema = MetadataProperty.objects.get( namespace__prefix='notreDAM', field_name='manual_rotation') last = component.get_metadata_values(schema) ctype = ContentType.objects.get_for_model(Component) if last != None: new_rotation += int(last) MetadataValue.objects.save_metadata_value( [self.item], {schema.id: [int(new_rotation)]}, output_variant_name, ws) except Exception, err: log.debug( "***===> Exception while setting manual_rotation metadata %s" % (err)) argv += ' -rotate %s' % (new_rotation) log.debug("calling adapter") self.out_file = get_storage_file_name(self.item.ID, self.workspace.pk, output_variant_name, output_extension) self.cmdline = '"file://%s[0]" %s "outfile://%s"' % ( self.source.uri, argv, self.out_file)
def _create_items(dir_name, variant_name, user, workspace, make_copy=True, recursive = True, force_generation = False, link = False): """ """ logger.debug('########## _create_items') def copy_file(original_filename, final_path, link, make_copy): logger.debug('original_filename %s'%original_filename) logger.debug('final_path %s'%final_path) if link: logger.debug('link') #os.symlink(original_filename, final_path) if os.path.exists(final_path): os.remove(final_path) os.link(original_filename, final_path) elif make_copy: shutil.copyfile(original_filename, final_path) else: shutil.move(original_filename, final_path) variant = Variant.objects.get(name = variant_name) for entry in os.walk(dir_name): root_dir, sub_dirs, files = entry files = [os.path.join(root_dir, x) for x in files] for original_filename in files: res_id = new_id() try: media_type = Type.objects.get_or_create_by_filename(original_filename) except MimeError, ex: logger.error(ex) continue final_filename = get_storage_file_name(res_id, workspace.pk, variant.name, media_type.ext) final_path = os.path.join(settings.MPROCESSOR_STORAGE, final_filename) upload_filename = os.path.basename(original_filename) tmp = upload_filename.split('_') item, created = _create_item(user, workspace, res_id, media_type, original_filename) logger.debug('created %s'%created) logger.debug('+++++++++++++ force_generation %s'%force_generation) if created: logger.debug('file created') if len(tmp) > 1: upload_filename = '_'.join(tmp[1:]) _create_variant(upload_filename, final_filename, media_type, item, workspace, variant) logger.debug('yielding item %s'%item) copy_file(original_filename, final_path, link, make_copy) yield item elif force_generation: logger.debug('force_generation') component = variant.get_component(workspace, item) dam_hash = md5(component.get_file_path()) dir_hash = md5(original_filename) #if force_generation or dir_hash != dam_hash: logger.debug('component.get_file_path() %s'%component.get_file_path()) logger.debug('original_filename %s'%original_filename) logger.debug('dir_hash != dam_hash %s'%(dir_hash != dam_hash)) if dir_hash != dam_hash: copy_file(original_filename, component.get_file_path(), link, make_copy) yield item if not recursive: break