0

I'm trying to store an image from a video file in django in the save method of a model. I've based in on this thread

This is my save method for the model

def save(self, *args, **kwargs):

    if not self.slug: 
        orig = self.slug = slugify(unidecode(self.name))
        for x in itertools.count(1):
            if not Project.objects.filter(slug=self.slug).exists() or (Project.objects.filter(slug=self.slug).exists() and self == Project.objects.get(slug=self.slug)):
                break
            self.slug = '%s-%d' % (orig, x)

    super(Project, self).save(*args, **kwargs)

    if not self.image and self.project_type=='video':
        vidcap = cv2.VideoCapture(settings.MEDIA_ROOT + self.video.url)
        success,image = vidcap.read()
        self.image = image
        self.image.name = 'video_images/' + self.slug + '.jpg'
        new_path = settings.MEDIA_ROOT + self.image.name
        cv2.imwrite(new_path, image)
        super(Project, self).save(*args, **kwargs)

I've taken a look at success and it always comes out False (I've tried looping while not success and it eventually timesout)

HenryM
  • 5,557
  • 7
  • 49
  • 105

1 Answers1

0

I changed the save to this. It works when the video name is latin characters but not for Greek

    if not self.image and self.project_type=='video':
        vidcap = cv2.VideoCapture(settings.BASE_DIR + self.video.url)
        success,image = vidcap.read()
        new_path = settings.MEDIA_ROOT + '/video_images/' + self.slug + '.jpg'
        cv2.imwrite(new_path, image)
        self.image = new_path
        self.image.name = 'video_images/' + self.slug + '.jpg'

Just realised it doesn't work when video has Greek character set

HenryM
  • 5,557
  • 7
  • 49
  • 105