【问题标题】:How to calculate video length and Thumbnail of video in django如何在django中计算视频长度和视频缩略图
【发布时间】:2019-10-25 05:06:09
【问题描述】:

我在做 django 项目。我正在尝试计算上传视频的长度并尝试创建视频的缩略图。如何做到这一点。

models.py

class Video(models.Model):
    video_format = models.CharField(max_length=15, blank=True, null=True)
    video_name = models.CharField(max_length=15, blank=True, null=True)
    video_size = models.CharField(max_length=15, blank=True, null=True)
    video_duration = models.IntegerField(blank=True, null=True)
    created_date = models.DateTimeField(default= datetime.now(), blank=True, null=True)
    status = models.CharField(max_length=15, blank=True, null=True)
    thumbline = models.CharField(max_length=15, blank=True, null=True)

views.py

def home(request):
    if request.method == 'POST':
        video = request.FILES.get('video')
        video_file = FileSystemStorage()
        file_upload = Video(video_file= video_file.save(video.name, video))
        file_upload.save()
        return render(request, 'home.html', {})

【问题讨论】:

标签: django


【解决方案1】:

另一个建议是使用moviepy

要获得持续时间,您可以使用VideoClipFile

from moviepy.editor import VideoClipFile

video = VideoClipFile(<path to your video file or a video file object>)

video.duration  # this will return the length of the video in seconds

要获取缩略图,您可以使用 Pillow 中的Image

from PIL import Image

frame_data = video.get_frame(1)  # 1 means frame at first second

img = Image.fromarray(frame_data, 'RGB')

然后对图像做任何你想做的事情。

【讨论】:

  • video_file= request.FILES.get('video') video=VideoClipFile(video_file) 这是正确的方法,因为我遇到了错误
  • @kumar 添加了该选项。
猜你喜欢
  • 1970-01-01
  • 2011-08-26
  • 2010-12-01
  • 2012-05-19
  • 2016-01-25
  • 1970-01-01
  • 2013-06-05
  • 2013-06-15
  • 2023-04-05
相关资源
最近更新 更多