0

i'm making a game and on windows i can't play a movie file. But in Ubuntu, i can play it. So how do you play a video in pygame 1.9.5 on windows.

i tried changing the pygame version, tried adding modules. But none of them works.

movie_screen = pygame.Surface(movie.get_size())

movie.set_display(movie_screen)
movie.play()


cnt = 0
playing = True
while playing:
    screen.fill( (0,0,0) )
    cnt+=1
    if cnt>=1875:
        cnt=0
        movie.rewind()
        movie.play()
    for event in pygame.event.get():
        if controlstart == True:
            if event.type == pygame.KEYDOWN:
                if event.key==pygame.K_KP_ENTER or 
                event.key==pygame.K_RETURN:
                    pygame.mixer.music.stop()
                    pygame.mixer.Channel(2).play(enter_sfx)
                    y += 1
                    fade(1280, 720)
                    xb = 0
                    yb = 0
                    if y == 3236:
                        controlstart = False
                    if y == 436:
                        movie.stop()
                        playing = False
                        pygame.quit()
                        quit()
                if event.key == pygame.K_UP:
                    pygame.mixer.Channel(3).play(move_sfx)
                    y += 1
                    if y == 3236:
                        y = 235
                        y1 = 3000
                    if y == 236:
                        y = 435
                        y1 = 3000
                    if y == 436:
                        y1 =335
                        y = 3235

                if event.key == pygame.K_DOWN:   
                    pygame.mixer.Channel(4).play(move_sfx)
                    y += 1
                    if y == 236:
                        y = 3235
                        y1 = 335
                    if y == 3236:
                        y1 = 3000
                        y = 435
                    if y == 436:
                        y1 = 3000
                        y = 235
        if event.type == pygame.QUIT:
            movie.stop()
            playing = False
            pygame.quit()
            quit()

    screen.blit(movie_screen,(0, 0))

i expect it to work, but it didn't and can't play a video

furas
  • 134,197
  • 12
  • 106
  • 148
Desjul20
  • 129
  • 8
  • in [documentation for 1.9.2](https://pygame.bitbucket.io/docs/pygame/ref/movie.html) you can see **NOTE: On NT derived Windows versions (e.g. XT) the default SDL directx video driver is problematic.**. In [documentation for 1.9.5](https://www.pygame.org/docs/ref/display.html) I can't find `pygame.movie` - probably they removed this module. – furas Apr 06 '19 at 12:40
  • So then how do you play a video in pygame on windows? – Desjul20 Apr 06 '19 at 13:10
  • i don't display video in PyGame :) In other modules - like CV2 - I create loop which gets single frame from stream and `blit()` it. Pyglet has [function to display video](https://pyglet.readthedocs.io/en/pyglet-1.3-maintenance/programming_guide/media.html#incorporating-video) and Pyglet is written in pure Python so maybe you can find solution in its code. – furas Apr 06 '19 at 13:18
  • there is example how to use cv2 (OpenCV) to display local video or stream from internet or video from local camera: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html – furas Apr 06 '19 at 13:24
  • but i need it to loop and not go over or freeze every other module. – Desjul20 Apr 06 '19 at 13:56
  • with CV you create loop which gets single frame from stream and display it - your can use your `while playing` loop for this. Inside `while playing` you get single frame from stream and blit it as you blit images. In next loop you get next frame from stream and blit it, etc. Or you can use module `threading` to run video in separated thread. As I know PyGame use thread to play music. – furas Apr 06 '19 at 14:02
  • so what's the code for it? – Desjul20 Apr 06 '19 at 16:33
  • I need time to think and creat code for tests. There is no problem to open file and read single frame in loop. But cv2 can keep frame as numpy's array and PyGame need `pygame.Surface` to display. As I remeber in documentation is example how to convert numpy's array to string and later string to `pygame.Surface` – furas Apr 06 '19 at 16:58
  • Okay, tell me when you're done – Desjul20 Apr 06 '19 at 18:16

1 Answers1

1

PyGame 1.9.5 doesn't have module to dispaly movie (at least not in documentation) maybe because it was incomplet and they try to convert code from library SDL 1.2 to SDL 2.0 which doesn't have module for movie.

You can try to use cv2 with pygame

This code display directly on screen from any stream (built-in camera, local file, remove stream). Window must have the same size as the stream. I don't know why but I had to transpose image before displaying.

import pygame
import cv2

# --- local (built-in) camera ---
#stream = 0

# --- local file ---
#stream = '2019-03-26_08-43-15.mkv'

# --- http stream ---
# doesn't work any more
#stream = 'http://media.dumpert.nl/tablet/9f7c6290_Verstappen_vs._Rosberg_with_Horner_Smile___Streamable.mp4.mp4.mp4'

# --- rtsp stream ---
#stream = 'rtsp://streaming1.osu.edu/media2/ufsap/ufsap.mov'

# --- rtmp stream ---
# Big Buck Bunny
stream = 'rtmp://184.72.239.149/vod/mp4:bigbuckbunny_1500.mp4'

# open stream
cap = cv2.VideoCapture(stream)

# read one frame and check if there was no problem
ret, img = cap.read()
if not ret:
    print("Can't read stream")
    #exit()

# transpose/rotate frame 
#img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
img = cv2.transpose(img)

# display its width, height, color_depth
print('shape:', img.shape)

pygame.init()

# create window with the same size as frame
screen = pygame.display.set_mode((img.shape[0], img.shape[1]))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # read one frame and check if there was no problem
    ret, img = cap.read()
    if not ret:
        running = False
        break
    else:
        # transpose/rotate frame
        #img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
        #img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        img = cv2.transpose(img)

        # blit directly on screen         
        pygame.surfarray.blit_array(screen, img)

    pygame.display.flip()

pygame.quit()

This code use Surface to get frame from stream so window may have different size.

import pygame
import cv2

# --- local (built-in) camera ---
#stream = 0

# --- local file ---
#stream = '2019-03-26_08-43-15.mkv'

# --- http stream ---
# doesn't work any more
#stream = 'http://media.dumpert.nl/tablet/9f7c6290_Verstappen_vs._Rosberg_with_Horner_Smile___Streamable.mp4.mp4.mp4'

# --- rtsp stream ---
#stream = 'rtsp://streaming1.osu.edu/media2/ufsap/ufsap.mov'

# --- rtmp stream ---
# Big Buck Bunny
stream = 'rtmp://184.72.239.149/vod/mp4:bigbuckbunny_1500.mp4'

cap = cv2.VideoCapture(stream)

ret, img = cap.read()
if not ret:
    print("Can't read stream")
    #exit()

#img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
img = cv2.transpose(img)
print('shape:', img.shape)

pygame.init()

screen = pygame.display.set_mode((800, 600))
surface = pygame.surface.Surface((img.shape[0], img.shape[1]))

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    ret, img = cap.read()
    if not ret:
        running = False
        break
    else:
        #img = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)
        #img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE)
        img = cv2.transpose(img)

        pygame.surfarray.blit_array(surface, img)
        screen.blit(surface, (0,0))

    pygame.display.flip()

pygame.quit()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/191382/discussion-on-answer-by-furas-how-do-you-play-videos-in-pygame-1-9-5-on-windows). – Samuel Liew Apr 07 '19 at 00:54