如何在django中计算视频长度和视频缩略图 [英] How to calculate video length and Thumbnail of video in django
本文介绍了如何在django中计算视频长度和视频缩略图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做django项目.我正在尝试计算上传视频的长度,并尝试创建视频的缩略图.怎么做.
I doing django project. I'm trying to calculate length of upload video and tring to create Thumbnail of the video. How to do this.
models.py
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', {})
推荐答案
另一个建议是使用 moviepy .
要获得持续时间,可以使用 VideoClipFile
For getting a duration you can use 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
For getting a thumbnail, you can use Image
from Pillow
from PIL import Image
frame_data = video.get_frame(1) # 1 means frame at first second
img = Image.fromarray(frame_data, 'RGB')
然后对图像进行任何操作.
And do whatever you want with the image.
这篇关于如何在django中计算视频长度和视频缩略图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文