Play a mp4 movie file with Pygame and Moviepy

How to play movies with python, using pygame and moviepy

Hi, here is how to play a movie, an mp4 file, for example, with the library moviepy, that needs pygame

After you installed the 2 libraries with

pip install pygame

pip install moviepy

from the cmd (windows button + R and then write cmd and press Return)

Press windows button è R and then cmd – you can also write cmd in the search tab (see next pic)
type cmd there
In the command line, you can install pygame like this. If you do not have python, install it from pygame.org

Now you can import the libraries and make the clip play

from moviepy.editor import *
import pygame

clip = VideoFileClip('pygame4.mp4')
clip.preview()

pygame.quit()

As you can see, you just have to pass the name of the mp4 file to load it and you use the preview method to watch it. At the end you use pygame.quit to close the window

Whatch a movie with python

Now it could be interesting to resize the window.

Resize the movie screen window

To resize the window, scaling the movie images, you just have to use resize, like this

import moviepy
from moviepy.editor import *
import pygame


clip = VideoFileClip('pygame4.mp4').resize(0.5)
clip.preview()

pygame.quit()

This way you can see the image that is the half of the original size, but you can also put something like this:

import moviepy
from moviepy.editor import *
import pygame


clip = VideoFileClip('pygame4.mp4').resize((600, 500))
clip.preview()

pygame.quit()

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.