1

I'm trying to create a rectangle and display a video in a square inside the rectangle. The problem is that when the preview is executed, the rectangle disappears (or is resizing). Can someone help me?

import moviepy.video.fx.all
from moviepy.editor import *
import pygame

class VideoSprite(pygame.sprite.Sprite):
    def __init__(self, rect, filename):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((rect.width, rect.height), pygame.HWSURFACE)
        self.rect = self.image.get_rect()
        self.rect.x = rect.x
        self.rect.y = rect.y
        self.video = moviepy.editor.VideoFileClip(filename).resize((self.rect.width, self.rect.height))        
        self.video_stop = False

    def update(self, time=pygame.time.get_ticks()):
        if not self.video_stop:
            try:
                raw_image = self.video.get_frame(time / 1000)
                self.image = pygame.image.frombuffer(raw_image,(self.rect.width, self.rect.height), 'RGB')
            except:
                self.video_stop = True

WINDOW_WIDTH    = 300
WINDOW_HEIGHT   = 600
WINDOW_SURFACE  = pygame.HWSURFACE|pygame.DOUBLEBUF|pygame.RESIZABLE

DARK_BLUE = (   3,   5,  54)
pygame.init()
pygame.mixer.init()
pygame.display.set_caption('Natália')
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ), WINDOW_SURFACE )

vds = VideoSprite(pygame.Rect( 50, 50, 100, 100 ),'theVideo.mp4')
sprite_group = pygame.sprite.GroupSingle()
sprite_group.add(vds)
sprite_group.update()
window.fill( DARK_BLUE )
sprite_group.draw( window )
pygame.display.flip()
vds.video.preview()

input()
Bóris
  • 11
  • 1
  • Not an expert on this, but... Your `except` is very broad, perhaps try to specify what exceptions you are expecting. Are you sure you don't get some exception and your video stops? – Melon May 13 '22 at 11:14
  • Thank you for your attention. To answer your question, I don't get any exceptions. The video plays until the end, the problem is that it doesn't play as a frame inside the rectangle. – Bóris May 13 '22 at 19:27
  • I'm a beginner in python. What I'm intending to do is play a video in a small square over an image, like a monitor over the image. – Bóris May 13 '22 at 19:33

0 Answers0