Reputation: 13
I'm looking for a way to resize videos by adding a white background instead of changing its proportions.
(A video with proportions 250x300px would transform to 300x300px by adding white band on each side of the video)
I've tried implementing the code I found on this thread to no result using ffmpeg, I'm very noob with image/video processing: Thread
def Reformat_Image(ImageFilePath):
from PIL import Image
image = Image.open(ImageFilePath, 'r')
image_size = image.size
width = image_size[0]
height = image_size[1]
if(width != height):
bigside = width if width > height else height
background = Image.new('RGBA', (bigside, bigside), (255, 255, 255, 255))
offset = (int(round(((bigside - width) / 2), 0)), int(round(((bigside - height) / 2),0)))
background.paste(image, offset)
background.save('out.png')
print("Image has been resized !")
else:
print("Image is already a square, it has not been resized !")
Essentially I would love to be able to do what is done in that thread, but with videos.
Any help is extremely appreciated
Upvotes: 1
Views: 1432
Reputation: 32104
You can use ffmpeg-python with pad video filter:
import ffmpeg
input = ffmpeg.input('in.avi')
output = ffmpeg.output(input, 'out.mp4', vcodec='libx264', vf='pad=w=iw+50:h=ih:x=25:y=0:color=white')
output.run()
Testing:
Create in.avi
for testing using FFmpeg in command line:
ffmpeg -y -r 10 -f lavfi -i mandelbrot=rate=10:size=250x300 -t 5 -c:v rawvideo -pix_fmt bgr24 in.avi
Python execution result (last frame of out.mp4
):
Remarks:
in.avi
as raw video format (without compression). out.mp4
with x264 codec (x264 / H.264 with default encoding parameters of FFmpeg). Upvotes: 2