简体   繁体   中英

How to scale up video with Python moviepy?

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM