ValueError in moviepy [duplicate] - pygame

I'm having a problem. I want to load and play a video in pygame but it doesn't start. The only thing that I am seeing is a black screen. Here is my code:
import pygame
from pygame import display,movie
pygame.init()
screen = pygame.display.set_mode((1024, 768))
background = pygame.Surface((1024, 768))
screen.blit(background, (0, 0))
pygame.display.update()
movie = pygame.movie.Movie('C:\Python27\1.mpg')
mrect = pygame.Rect(0,0,140,113)
movie.set_display(screen, mrect.move(65, 150))
movie.set_volume(0)
movie.play()
Can you help me??

The pygame.movie module is deprecated and not longer supported.
If you only want to show the video you can use MoviePy (see also How to be efficient with MoviePy):
import pygame
import moviepy.editor
pygame.init()
video = moviepy.editor.VideoFileClip("video.mp4")
video.preview()
pygame.quit()
An alternative solution is to use the OpenCV VideoCapture. Install OpenCV for Python (cv2) (see opencv-python). However, it should be mentioned that cv2.VideoCapture does not provide a way to read the audio from the video file.
This is only a solution to show the video but no audio is played.
Opens a camera for video capturing:
video = cv2.VideoCapture("video.mp4")
Get the frames per second form the VideoCapture object:
fps = video.get(cv2.CAP_PROP_FPS)
Create a pygame.time.Clock:
clock = pygame.time.Clock()
Grabs a video frame and limit the frames per second in the application loop:
clock.tick(fps)
success, video_image = video.read()
Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:
video_surf = pygame.image.frombuffer(video_image.tobytes(), video_image.shape[1::-1], "BGR")
See also Video:
Minimal example:
import pygame
import cv2
video = cv2.VideoCapture("video.mp4")
success, video_image = video.read()
fps = video.get(cv2.CAP_PROP_FPS)
window = pygame.display.set_mode(video_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, video_image = video.read()
if success:
video_surf = pygame.image.frombuffer(
video_image.tobytes(), video_image.shape[1::-1], "BGR")
else:
run = False
window.blit(video_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()

You are not actually blitting it to a screen. You are also not utilizing a clock object so it will play as fast as possible. Try this:
# http://www.fileformat.info/format/mpeg/sample/index.dir
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
I just got that MELT.MPG from the link provided in the comment. You should be able to simply switch out that string for your actual MPG you want to play and it will work... maybe.

As you probably know, the pygame.movie module is deprecated and no longer exists in the latest version of pygame.
An alternative would be to read in frames of the video one by one and blit them onto the pygame screen using the the cv2 module (OpenCV), that can be installed with the command prompt command:
pip install opencv-python
Then, you can run the code:
import cv2
import pygame
cap = cv2.VideoCapture('video.mp4')
success, img = cap.read()
shape = img.shape[1::-1]
wn = pygame.display.set_mode(shape)
clock = pygame.time.Clock()
while success:
clock.tick(60)
success, img = cap.read()
for event in pygame.event.get():
if event.type == pygame.QUIT:
success = False
wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
pygame.display.update()
pygame.quit()

Related

how to add a video to python at the touch of a button? [duplicate]

I'm having a problem. I want to load and play a video in pygame but it doesn't start. The only thing that I am seeing is a black screen. Here is my code:
import pygame
from pygame import display,movie
pygame.init()
screen = pygame.display.set_mode((1024, 768))
background = pygame.Surface((1024, 768))
screen.blit(background, (0, 0))
pygame.display.update()
movie = pygame.movie.Movie('C:\Python27\1.mpg')
mrect = pygame.Rect(0,0,140,113)
movie.set_display(screen, mrect.move(65, 150))
movie.set_volume(0)
movie.play()
Can you help me??
The pygame.movie module is deprecated and not longer supported.
If you only want to show the video you can use MoviePy (see also How to be efficient with MoviePy):
import pygame
import moviepy.editor
pygame.init()
video = moviepy.editor.VideoFileClip("video.mp4")
video.preview()
pygame.quit()
An alternative solution is to use the OpenCV VideoCapture. Install OpenCV for Python (cv2) (see opencv-python). However, it should be mentioned that cv2.VideoCapture does not provide a way to read the audio from the video file.
This is only a solution to show the video but no audio is played.
Opens a camera for video capturing:
video = cv2.VideoCapture("video.mp4")
Get the frames per second form the VideoCapture object:
fps = video.get(cv2.CAP_PROP_FPS)
Create a pygame.time.Clock:
clock = pygame.time.Clock()
Grabs a video frame and limit the frames per second in the application loop:
clock.tick(fps)
success, video_image = video.read()
Convert the camera frame to a pygame.Surface object using pygame.image.frombuffer:
video_surf = pygame.image.frombuffer(video_image.tobytes(), video_image.shape[1::-1], "BGR")
See also Video:
Minimal example:
import pygame
import cv2
video = cv2.VideoCapture("video.mp4")
success, video_image = video.read()
fps = video.get(cv2.CAP_PROP_FPS)
window = pygame.display.set_mode(video_image.shape[1::-1])
clock = pygame.time.Clock()
run = success
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
success, video_image = video.read()
if success:
video_surf = pygame.image.frombuffer(
video_image.tobytes(), video_image.shape[1::-1], "BGR")
else:
run = False
window.blit(video_surf, (0, 0))
pygame.display.flip()
pygame.quit()
exit()
You are not actually blitting it to a screen. You are also not utilizing a clock object so it will play as fast as possible. Try this:
# http://www.fileformat.info/format/mpeg/sample/index.dir
import pygame
FPS = 60
pygame.init()
clock = pygame.time.Clock()
movie = pygame.movie.Movie('MELT.MPG')
screen = pygame.display.set_mode(movie.get_size())
movie_screen = pygame.Surface(movie.get_size()).convert()
movie.set_display(movie_screen)
movie.play()
playing = True
while playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
movie.stop()
playing = False
screen.blit(movie_screen,(0,0))
pygame.display.update()
clock.tick(FPS)
pygame.quit()
I just got that MELT.MPG from the link provided in the comment. You should be able to simply switch out that string for your actual MPG you want to play and it will work... maybe.
As you probably know, the pygame.movie module is deprecated and no longer exists in the latest version of pygame.
An alternative would be to read in frames of the video one by one and blit them onto the pygame screen using the the cv2 module (OpenCV), that can be installed with the command prompt command:
pip install opencv-python
Then, you can run the code:
import cv2
import pygame
cap = cv2.VideoCapture('video.mp4')
success, img = cap.read()
shape = img.shape[1::-1]
wn = pygame.display.set_mode(shape)
clock = pygame.time.Clock()
while success:
clock.tick(60)
success, img = cap.read()
for event in pygame.event.get():
if event.type == pygame.QUIT:
success = False
wn.blit(pygame.image.frombuffer(img.tobytes(), shape, "BGR"), (0, 0))
pygame.display.update()
pygame.quit()

program only works half the time

for some reason this python program i saw on a youtube tutorial only works sometimes. Whenever i run the code, i get an error in the program telling me the program doesnt answer. But once in a while the code suddenly works perfectly.
import pygame, sys
from sys import exit
# crosshair class
class Crosshair(pygame.sprite.Sprite):
def __init__(self, picture_path):
super().__init__()
self.image = pygame.image.load(picture_path)
self.rect = self.image.get_rect()
def update(self):
self.rect.center = pygame.mouse.get_pos()
# general setup
pygame.init()
clock = pygame.time.Clock()
# create the screen
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Runner')
background = pygame.image.load("sprites/graphics/bg.png")
background = pygame.transform.scale(background, (800, 400))
pygame.mouse.set_visible(False)
#crosshair
crosshair = Crosshair('sprites/graphics/crosshair.png')
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)
# while loop
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit
exit()
screen.blit(background,(0,0))
crosshair_group.draw(screen)
crosshair_group.update()
clock.tick(60)
pygame.display.update()
You need to do pygame.quit() instead of pygame.quit. Missing the parentheses means that you are not actually calling the function, and the window never closes.
You are getting a not responding message when you attempt to X out the window because exit() is being called, which ends your program, including the event-handling loop. The window is left with no program controlling it or making it respond to inputs such as closing, so you get that message.
Calling the pygame.quit() function will close the window right before the program quits, so it is all taken care of.

pygame background image not showing

I am working on a pygame space invaders game. When I try to blit the background image it doesn't work. I am fairly new to pygame so I don't know what to do. I tried downloading another picture and using that but the problem persists.
import pygame
import os
import time
import random
import math
import sys
pygame.init()
screen = pygame.display.set_mode((750,750))
pygame.display.set_caption("Space Invaders")
FPS = 60
clock = pygame.time.Clock()
#Ships
RED_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_red_small.png'))
GREEN_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_green_small.png'))
BLUE_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_blue_small.png'))
YELLOW_SPACE_SHIP = pygame.image.load(os.path.join('assets','pixel_ship_yellow.png'))
#Lasers
RED_LASER = pygame.image.load(os.path.join('assets','pixel_laser_red.png'))
BLUE_LASER = pygame.image.load(os.path.join('assets','pixel_laser_blue.png'))
YELLOW_LASER = pygame.image.load(os.path.join('assets','pixel_laser_yellow.png'))
GREEN_LASER = pygame.image.load(os.path.join('assets','pixel_laser_green.png'))
#Background
BG = pygame.transform.scale2x(pygame.image.load(os.path.join('assets','background-black.png')).convert_alpha())
def main():
run = True
def redraw_window():
screen.blit(BG, (0, 0))
pygame.display.update()
while True:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.blit(BG, (0,0))
pygame.display.update
clock.tick(120)
pygame.quit()
You need parenthesis when calling display update:
screen.blit(BG, (0,0))
pygame.display.update() # need parenthesis
clock.tick(120)

pygame window not responding when runing program

i have been working on a pygame platformer and been trying to run it buy my python window just says not responding and then crashes is ther anything wrong with my code. Here is my code
link to my code: https://drive.google.com/drive/u/0/folders/1QRNYi2hd5RBhIa-EwKdxdRPUdxWHALon
Add this at the top of your code:
done=False
while not done:
#event handler
for event in pygame.event.get():
if event.type==QUIT:
done=True
pygame.quit()
This is the code to open a window, change it to red and then close it when the cross is pressed:
import pygame
#Initialize the game engine
pygame.init()
red = (255, 0, 0)
#Set width and hight
size = [700, 500]
screen = pygame.display.set_mode(size)
#The caption on top of the window
pygame.display.set_caption("My game")
#Loop until the user clicks the button
done = False
#Manages how fast the screen updates
clock = pygame.time.Clock()
#--------MAIN PROGRAM LOOP--------
while done == False:
for event in pygame.event.get(): #User did something
if event.type == pygame.QUIT: #If user clicked close
done = True
screen.fill(red)
pygame.display.flip()
#Limit to 20 frames per second
clock.tick(20)
#Exits window if the original loop is broken
pygame.quit()
I hope that this helps

Need assistance with Pygame and creating windows

For some reason, when I use the code below:
import pygame, sys
pygame.init()
def create_window():
global window, window_height, window_width, window_title
window_width, window_height = 1280, 720
window_title = "The Adventure of Nate"
pygame.display.set.caption(window_title)
window = pygame.display.set_mode((window_width, window_height, pygame.HWSURFACE|pygame.DOUBLEBUFF)
create_window()
isrunning = True
while isrunning == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
isRunning = False
window.fill(0, 0, 0)
pygame.display.update()
pygame.quit()
sys.exit()
I get the following error:
C:\Python3.6\python.exe "C:/Users/home/PycharmProjects/Basic RPG/Base
Game.py" File "C:/Users/home/PycharmProjects/Basic RPG/Base
Game.py", line 16
create_window()
^ SyntaxError: invalid syntax
Process finished with exit code 1
If anyone is experienced in the matter, can they help me correct my code.
(P.S: This is my first time coding without assistance, so sorry if my code is all over the place XD)
You're missing a ) in the line window = pygame.display.set_mode((window_width, window_height, pygame.HWSURFACE|pygame.DOUBLEBUFF)
It should be window = pygame.display.set_mode((window_width, window_height), pygame.HWSURFACE|pygame.DOUBLEBUFF)