moviepy: reduce video to 100mb

Asked

Viewed 287 times

0

I’d like to know if there’s any way to narrow a video down to 100mb. I have some videos and I want to convert it to 100mb, regardless of the quality it will stay.

In this example, I reduce to 360p in quality

import moviepy.editor as mp
clip = mp.VideoFileClip("movie.mp4")
clip_resized = clip.resize(height=360) # make the height 360px ( According to moviePy documenation The width is then computed so that the width/height ratio is conserved.)
clip_resized.write_videofile("movie_resized.mp4")

How would you reduce by 100mb? Is there any way? (PYTHON 2.7)

1 answer

1


Unfortunately it is not easy to predict the size a compressed video will get before trying to compress it. This depends on the content of the video - videos where the image gets more static spend less space, while other videos at the same resolution and duration can spend a lot more if the content of the frames that make up it changes more frequently.

The only way would be to make an algorithm that tries to increase the quality until it finds an ideal size:

tamanho = 100 * 1024 * 1024 # 100 megabytes
resolucoes = [70, 140, 240, 360, 480, 640, 720, 1080]
clip = mp.VideoFileClip("movie.mp4")
for resolucao in resolucoes:   
    clip_resized = clip.resize(height=resolucao) 
    clip_resized.write_videofile("movie_resized_temp.mp4")
    if os.path.getsize("movie_resized_temp.mp4") > tamanho:
        os.remove("movie_resized_temp.mp4")
        break # Para quando encontrar um tamanho bom
    try:
        os.remove("movie_resized.mp4")
    except OSError:
        pass
    os.rename("movie_resized_temp.mp4", "movie_resized.mp4")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.