Esempio n. 1
0
    def _encode_images_from_meta(self, m, num_images=4):
        target_files = tuple(m.processed_files['image{}'.format(index + 1)]
                             for index in range(num_images))
        if all(target_file.exists for target_file in target_files):
            log.debug(
                'Processed Destination was created with the same input sources - no thumbnail gen required'
            )
            return True

        source_file_absolute = m.source_files['video'].get('absolute')
        if not source_file_absolute:  # If no video source, attempt to degrade to single image input
            source_file_absolute = m.source_files['image'].get('absolute')
        if not source_file_absolute:
            log.warning(
                'No video or image input in meta to extract thumbnail images')
            return False
        if not os.path.exists(source_file_absolute):
            log.error(
                'The source file to extract images from does not exist {}'.
                format(source_file_absolute))
            return False

        if file_ext(source_file_absolute).ext in EXTS['image']:
            # If input is a single image, we use it as an imput video and take
            # a frame 4 times from frame zero.
            # This feels inefficent, but we need 4 images for the import check.
            # Variable numbers of images would need more data in the meta
            times = (0, ) * num_images
        else:
            video_duration = m.source_details.get('duration')
            if not video_duration:
                log.warning(
                    'Unable to assertain video duration; unable to extact images'
                )
                return False
            times = (float("%.3f" % (video_duration * offset))
                     for offset in (x / (num_images + 1)
                                    for x in range(1, num_images + 1)))

        with tempfile.TemporaryDirectory() as tempdir:
            for index, time in enumerate(times):
                image_file = os.path.join(tempdir, '{}.jpg'.format(index))
                encode_succes, cmd_result = external_tools.extract_image(
                    source=source_file_absolute,
                    destination=image_file,
                    time=time)
                if not encode_succes:
                    if self.pdb:
                        import pdb
                        pdb.set_trace()
                    log.error(cmd_result)
                    return False
                target_files[index].move(image_file)

        return True
Esempio n. 2
0
 def _wrap_scan_data_item(self, data):
     if not data:
         return {}
     file_no_ext, ext = file_ext(data['relative'])
     return ChainMap(
         dict(
             absolute=os.path.abspath(os.path.join(self.source_path, data['relative'])),
             ext=ext,
             file_no_ext=file_no_ext,
         ),
         # IMPORTANT! Any modification to a chainmap is ALWAYS made to the first dict.
         # We DON'T want any changes to the underlying data from a wrapper
         data,
     )
Esempio n. 3
0
    def _encode_images_from_meta(self, m, num_images=4):
        target_files = tuple(m.processed_files["image{}".format(index + 1)] for index in range(num_images))
        if all(target_file.exists for target_file in target_files):
            log.debug("Processed Destination was created with the same input sources - no thumbnail gen required")
            return True

        source_file_absolute = m.source_files["video"].get("absolute")
        if not source_file_absolute:  # If no video source, attempt to degrade to single image input
            source_file_absolute = m.source_files["image"].get("absolute")
        if not source_file_absolute:
            log.warning("No video or image input in meta to extract thumbnail images")
            return False
        if not os.path.exists(source_file_absolute):
            log.error("The source file to extract images from does not exist {}".format(source_file_absolute))
            return False

        if file_ext(source_file_absolute).ext in EXTS["image"]:
            # If input is a single image, we use it as an imput video and take
            # a frame 4 times from frame zero.
            # This feels inefficent, but we need 4 images for the import check.
            # Variable numbers of images would need more data in the meta
            times = (0,) * num_images
        else:
            video_duration = m.source_details.get("duration")
            if not video_duration:
                log.warning("Unable to assertain video duration; unable to extact images")
                return False
            times = (
                float("%.3f" % (video_duration * offset))
                for offset in (x / (num_images + 1) for x in range(1, num_images + 1))
            )

        with tempfile.TemporaryDirectory() as tempdir:
            for index, time in enumerate(times):
                image_file = os.path.join(tempdir, "{}.jpg".format(index))
                encode_succes, cmd_result = external_tools.extract_image(
                    source=source_file_absolute, destination=image_file, time=time
                )
                if not encode_succes:
                    if self.pdb:
                        import pdb

                        pdb.set_trace()
                    log.error(cmd_result)
                    return False
                target_files[index].move(image_file)

        return True
Esempio n. 4
0
 def _wrap_scan_data_item(self, data):
     """
     Wrap the stored source data dict item in a ChainMap that adds additional common derived fields
     """
     if not data:
         return {}
     file_no_ext, ext = file_ext(data['relative'])
     return ChainMap(
         dict(
             absolute=os.path.abspath(os.path.join(self.source_path, data['relative'])),
             ext=ext,
             file_no_ext=file_no_ext,
         ),
         # IMPORTANT! Any modification to a chainmap is ALWAYS made to the first dict.
         # We DON'T want any changes to the underlying data from a wrapper
         data,
     )
Esempio n. 5
0
    def _encode_images_from_meta(self, m, source_files, num_images=ProcessedFilesManager.DEFAULT_NUMBER_OF_IMAGES):
        target_files = tuple(
            self.processed_files_manager.get_processed_file(m.source_hash, 'image', index)
            for index in range(num_images)
        )
        if all(target_file.exists for target_file in target_files):
            log.debug('Processed Destination was created with the same input sources - no thumbnail gen required')
            return True

        source_file_absolute = source_files['video'].get('absolute')
        if not source_file_absolute:  # If no video source, attempt to degrade to single image input
            source_file_absolute = self.fileitem_wrapper.wrap_scan_data(m)['image'].get('absolute')
        if not source_file_absolute:
            log.warn('No video or image input in meta to extract thumbnail images')
            return False
        if not os.path.exists(source_file_absolute):
            log.error('The source file to extract images from does not exist {}'.format(source_file_absolute))
            return False

        if file_ext(source_file_absolute).ext in EXTS['image']:
            # If input is a single image, we use it as an imput video and take
            # a frame 4 times from frame zero.
            # This feels inefficent, but we need 4 images for the import check.
            # Variable numbers of images would need more data in the meta
            times = (0, ) * num_images
        else:
            video_duration = m.source_details.get('duration')
            if not video_duration:
                self._update_source_details(m)  # Bugfix for being tenatious in aquiring duration
                video_duration = m.source_details.get('duration')
            if not video_duration:
                log.warn('Unable to assertain video duration; unable to extact images')
                return False
            times = (float("%.3f" % (video_duration * offset)) for offset in (x/(num_images+1) for x in range(1, num_images+1)))

        with tempfile.TemporaryDirectory() as tempdir:
            for index, time in enumerate(times):
                image_file = os.path.join(tempdir, '{}.jpg'.format(index))
                encode_succes, cmd_result = external_tools.extract_image(source_file_absolute, image_file, time)
                if not encode_succes:
                    #import pdb ; pdb.set_trace()
                    log.error(cmd_result)
                    return False
                target_files[index].move(image_file)

        return True