moviepy: reduce video to 100mb

0

I would like to know if there is a way to reduce a video to 100mb. I have some videos and I want to convert it to 100mb, regardless of the quality of the video.

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 it by 100mb? Is there any way? (PYTHON 2.7)

    
asked by anonymous 10.11.2018 / 00:31

1 answer

0

Unfortunately, it is not easy to predict what size a compressed video will be, before trying to compress it. This depends on the content of the video - videos where the image becomes more static spend less space, while other videos in the same resolution and duration can spend much more if the content of the frames that make it change more often.

The only way would be to make an algorithm that tries to increase 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")
    
10.11.2018 / 17:17