1

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

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', {})
kumar
  • 53
  • 2
  • 8
  • check this link [Get video duration in pyhton](https://stackoverflow.com/questions/3844430/how-to-get-the-duration-of-a-video-in-python) and this the other one [Get video duration with avconf](https://stackoverflow.com/questions/32294988/get-video-duration-with-avconv-via-python) – dhentris Oct 25 '19 at 05:36

1 Answers1

3

Another suggestion is to use moviepy.

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

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.

Davit Tovmasyan
  • 3,238
  • 2
  • 20
  • 36