Puja Singh
Puja Singh

Reputation: 25

Video upload and view in different resolution in django

I am building a video streaming app like youtube in django for learning purposes. I want to implement the functionality of uploading video by user and view it in different resolutions like 360p, 480p etc. I dont know how to achieve this..

Should I save all the versions of video?? Wouldn't that be redundant??

Also how to convert the video in different resolutions!

I want to use aws s3 for this.

Upvotes: 1

Views: 1088

Answers (1)

LaCharcaSoftware
LaCharcaSoftware

Reputation: 1094

Yes, I think you have to save all the versions of your video. I don't think you can resize a video from Python. Maybe you should use a subprocess, like ffmpeg:

Example (this example changes the size and deletes the original video, assuming that the video is a models.FileField):

import subprocess

def do_video_resize(video):
  filename = video.file.path.split('/')[-1]
  if ffmpeg('-v', '-8', '-i', video.file.path, '-vf', 'scale=-2:480', '-preset', 'slow', '-c:v', 'libx264', '-strict', 'experimental', '-c:a', 'aac', '-crf', '20', '-maxrate', '500k', '-bufsize', '500k', '-r', '25', '-f', 'mp4', ('/tmp/'+ filename ), '-y'):
    resized_video = open('/tmp/' + filename)
    video.file.save(filename ,File(resized_video))
    resized_video.close()
    os.remove('/tmp/'+ filename)
    return video

def ffmpeg(*cmd):
  try:
    subprocess.check_output(['ffmpeg'] + list(cmd))
  except subprocess.CalledProcessError:
    return False
  return True

Upvotes: 2

Related Questions