从Django媒体视频文件中提取图像

时间:2019-05-10 08:14:40

标签: django opencv

我正在尝试使用模型的save方法在django中存储来自视频文件的图像。我基于this thread

这是我的模型保存方法

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)

我看过success,它总是出现False(我尝试循环while not success并最终超时)

1 个答案:

答案 0 :(得分:0)

我将保存更改为此。当视频名称为拉丁字符而不是希腊语时有效

    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'

只是意识到视频具有希腊字符集是不起作用的