【问题标题】:How to capture picture or generate thumbnail from video in python django restframework?如何在 python django rest 框架中从视频中捕获图片或生成缩略图?
【发布时间】:2021-09-30 09:24:33
【问题描述】:

实际上,我正在尝试从 python Django 中上传的视频中捕获以制作缩略图。然后我必须将此缩略图保存在单独的模型字段中以供以后使用。

class Post(models.Model):
    user= models.ForeignKey(Profile, on_delete=models.CASCADE)
    video = models.FileField(upload_to='post_videos/%Y/%m', null=True)
    thumbnail = models.ImageField(upload_to='post_images/%Y/%m', null=True)

我只想上传视频,在任何时间戳捕获缩略图,然后将其保存到缩略图字段。请让我知道是否有人可以帮助我。我尝试使用 FFmpeg 和 moviepy 但无法解决问题。等待帮助。谢谢

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    你可以用 ffmpeg 做到这一点。你遇到了什么问题?

    import subprocess
    video_input_path = post_object.video.path
    # or whatever the path of the uploaded video file is
    img_output_path = 'path/to/image'
    subprocess.call(['ffmpeg', '-i', video_input_path, '-ss', '00:00:00.000', '-vframes', '1', img_output_path])
    

    然后您可以将其分配给缩略图字段

    【讨论】:

    • def save(self, *args, **kwargs): super(Post, self).save(*args, **kwargs) if self.post_image: picture = Image.open(self.post_image.path) picture.save(self.post_image.path, optimize=True, quality=10) 我想改写缩略图的保存方法。对于视频生成的缩略图,我不知道该怎么做。
    • 使用django-imagekit会节省很多时间。
    • @kinshukdua django-imagkit 在使用处理器时是否保持图像的比例=[ResizeToFill(100, 50)] ?
    • 这不起作用。该 imagekit 用于从图片制作缩略图。但是我们必须从上传的视频中捕获帧,然后将其保存到目录中。为了捕获图像,我们必须打开视频,然后在给定的视频持续时间内捕获帧。让我知道是否有人可以提供帮助。谢谢
    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 2017-10-15
    • 2012-10-16
    • 2019-07-26
    • 2013-10-22
    • 2012-09-01
    • 2012-01-08
    相关资源
    最近更新 更多