How to scale up video with Python moviepy?

Question:

I am using Python 3.10 and moviepy library to process videos. I need to scale up (zoom) video without changing its resolution. There are a lot of example of using moviepy resize method, but it changes only the resolution.

Are there any options of scaling video with moviepy or maybe you can suggest some solutions with openCV?

Asked By: barelydrown

||

Answers:

For achieving zoomed in video, we may combine scaling and cropping.

Example:

from moviepy.editor import VideoFileClip

clip = VideoFileClip("input.mp4")
width, height = clip.size
resized_and_cropped_clip = clip.resize(2).crop(x1=width//2, y1=height//2, x2=width*3//2, y2=height*3//2)
resized_and_cropped_clip.write_videofile("output.mp4")
  • resize(2) – resize the video by a factor of 2 in each axis.
  • crop(x1=width//2, y1=height//2, x2=width*3//2, y2=height*3//2) – crop a rectangle with the original image size around the center of the resized video.

Alternately we may first crop and then resize.
It is more efficient, but may result minor degradation at the frame’s margins:

from moviepy.editor import VideoFileClip

clip = VideoFileClip("input.mp4")
width, height = clip.size
resized_and_cropped_clip = clip.crop(x1=width//4, y1=height//4, x2=width*3//4, y2=height*3//4).resize(2)
resized_and_cropped_clip.write_videofile("output.mp4")

The above examples show zoom in by a factor of x2.
For zooming by other factor, we have to adjust the computation of x1, y1, x2, y2 arguments.

Answered By: Rotem
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.