def set_thumbnails(self, fp, screenshot_name=None): img = Image.open(fp) if screenshot_name is None: screenshot_name = fp.name self.image = thumb_img(img, name=screenshot_name) self.thumbnail = thumb_img(img, 190, 123, name=screenshot_name) self.icon = thumb_img(img, 60, name=screenshot_name)
def make_thumbs(self): img = Image.open(self.object.original_file.file).copy() img = img.convert('RGB') self.object.original_file.file.seek(0) self.object.image = thumb_img(img, 938) self.object.thumbnail = thumb_crop_img(img, width=190, height=123) self.object.icon = thumb_img(img, 60) del img self.object.save()
def run(self, video_id, **kwargs): """ Converts the Video and creates the related files. """ logger = self.get_logger(**kwargs) Video.objects.filter(pk=video_id).update(status=Video.CONVERTING) try: video = Video.objects.get(pk=video_id) except Video.DoesNotExist: return 'Video was deleted' logger.info("Starting screen shoots of video %s" % video.pk) for preview in video.preview_set.all(): preview.delete() options = ConvertOptions.objects.get(title='hq_file') source_file = NamedTemporaryFile(delete=False, suffix='.mp4', prefix='screen_shot_source') source_file.write(video.best_quality_file().read()) logger.info("Downloaded best {0} ({1})".format(video, source_file.name)) source_file.close() try: file_params = media_info(source_file.name) except OSError: self._error_handle(video) return "Stop screenshoot - bad source file" if not file_params.get('Video', None): self._error_handle(video) return "Stop screenshoot - bad source file" options.update(file_params) size = '{0}x{1}'.format(options.width, options.height) video.best_quality_file().open() duration = video.seconds_duration() if duration > 30: offset = 10 else: offset = 1 screenable_duration = duration - offset * 2 previews_count = settings.PREVIEWS_COUNT if screenable_duration < 1: previews_count = screenable_duration = 1 elif screenable_duration < previews_count: previews_count = screenable_duration step = screenable_duration / previews_count for offset in (offset+step for i in xrange(previews_count)): result_file = NamedTemporaryFile(suffix='.jpg', prefix='screen_shot') preview = Preview(owner=video) cmd = self.get_commandline(source_file.name, random.choice( range(offset, offset+step)), size, result_file.name) process = subprocess.call(cmd, shell=False) if process: Video.objects.filter(pk=video_id).update(status=Video.ERROR) logger.error("Some error during ffmpeging %s" % video.pk) return "Stop making screen shoots - video is deleted" try: orig = Image.open(result_file.name) img = orig.copy() result_file.close() preview_name = '{0}.jpg'.format(offset) preview.image = thumb_img(img, name=preview_name) preview.thumbnail = thumb_img(img, 190, name=preview_name) preview.icon = thumb_img(img, 60, name=preview_name) preview.save() except IOError: result_file.close() logger.error('error during imagining {0}'.format( result_file.name)) logger.info("Screened {0} ({1})".format(video, source_file.name)) os.unlink(source_file.name) Video.objects.filter(pk=video_id).update(status=Video.READY) return "Ready"