首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用pygame播放h264视频

使用pygame播放h264视频
EN

Stack Overflow用户
提问于 2017-11-07 23:58:42
回答 2查看 243关注 0票数 1

我正在做的树莓派启用项目的图形用户界面使用pygame和HDMI液晶触摸屏。我可以用兼容的摄像头录制h264视频,但是我没有找到任何关于如何使用pygame播放h264视频的例子,有人能给我指出正确的方法吗?

EN

回答 2

Stack Overflow用户

发布于 2018-07-31 01:05:48

Pygame目前不支持H.264视频播放。请尝试使用Pyglet。(注意:如果您没有安装AVBin,它只能播放.wav文件。)

票数 0
EN

Stack Overflow用户

发布于 2021-10-03 15:14:07

在Pygame中播放视频

PLay视频的完整示例

代码语言:javascript
运行
复制
        import pygame
        import cv2
        import numpy
        pygame.init()
        
        width = 1280
        height = 720
        window = pygame.display.set_mode((width, height))
        
        
        class VideoPlayer:
            def __init__(self, surface, x, y, video, play_in_loop=False, mirror_effect=False):
                self.video = video
                self.surface = surface
                self.VideoReader = None
                self.FileOpened = False
                self.x = x
                self.y = y
                self.playInLoop = play_in_loop
                self.open()
                self.FrameResizer = False
                self.mirrorEffect = mirror_effect
        
            def maintainAspectRatio(self):
                video = cv2.VideoCapture(self.video)
                flag, frame = video.read()
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                frame = numpy.rot90(frame)
                frame = pygame.surfarray.make_surface(frame)
                height = frame.get_height()
                width = frame.get_width()
                self.height = int((self.width * (height/width)))
                video.release()
        
            def activeMirrorEffact(self):
                self.mirrorEffect = True
        
            def deactiveMirrorEffact(self):
                self.mirrorEffect = False
        
            def activeFrameResizer(self, width=600, height=600, aspectRatio=False):
                self.FrameResizer = True
                self.width = width
                self.height = height
                if aspectRatio:
                    self.maintainAspectRatio()
        
            def deactiveFrameResizer(self):
                self.FrameResizer = False
        
            def open(self):
                try:
                    self.VideoReader = cv2.VideoCapture(self.video)
                    self.VideoReader.setExceptionMode(False)
                except:
                    self.FileOpend = False
                    return False
                self.FileOpend = True
        
            def close(self):
                self.VideoReader.release()
                self.FileOpend = False
        
            def show(self):
                if self.FileOpend:
                    flag, frame = self.VideoReader.read()
                    if flag:
                        if not self.mirrorEffect:
                            frame = cv2.flip(frame, 1)
        
                        if self.FrameResizer:
                            frame = cv2.resize(frame, (self.width, self.height), fx=0, fy=0, interpolation=cv2.INTER_CUBIC)
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                        frame = numpy.rot90(frame)
                        frame = pygame.surfarray.make_surface(frame)
                        self.surface.blit(frame, (self.x, self.y))
                    else:
                        self.close()
                        if self.playInLoop:
                            self.open()
        
        
        videoPlayer = VideoPlayer(window, 0, 0, "video.mp4", True)
        videoPlayer.activeFrameResizer(600, aspectRatio=True)
        
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
        
            videoPlayer.show()
            pygame.display.update()
            pygame.time.Clock().tick(30)

步骤1:导入cv2,NumPy

第2步:只复制“VideoPlayer”类

代码语言:javascript
运行
复制
        class VideoPlayer:
            def __init__(self, surface, x, y, video, play_in_loop=False, mirror_effect=False):
                self.video = video
                self.surface = surface
                self.VideoReader = None
                self.FileOpened = False
                self.x = x
                self.y = y
                self.playInLoop = play_in_loop
                self.open()
                self.FrameResizer = False
                self.mirrorEffect = mirror_effect

            def maintainAspectRatio(self):
                video = cv2.VideoCapture(self.video)
                flag, frame = video.read()
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                frame = numpy.rot90(frame)
                frame = pygame.surfarray.make_surface(frame)
                height = frame.get_height()
                width = frame.get_width()
                self.height = int((self.width * (height/width)))
                video.release()

            def activeMirrorEffact(self):
                self.mirrorEffect = True

            def deactiveMirrorEffact(self):
                self.mirrorEffect = False

            def activeFrameResizer(self, width=600, height=600, aspectRatio=False):
                self.FrameResizer = True
                self.width = width
                self.height = height
                if aspectRatio:
                    self.maintainAspectRatio()

            def deactiveFrameResizer(self):
                self.FrameResizer = False

            def open(self):
                try:
                    self.VideoReader = cv2.VideoCapture(self.video)
                    self.VideoReader.setExceptionMode(False)
                except:
                    self.FileOpend = False
                    return False
                self.FileOpend = True

            def close(self):
                self.VideoReader.release()
                self.FileOpend = False

            def show(self):
                if self.FileOpend:
                    flag, frame = self.VideoReader.read()
                    if flag:
                        if not self.mirrorEffect:
                            frame = cv2.flip(frame, 1)

                        if self.FrameResizer:
                            frame = cv2.resize(frame, (self.width, self.height), fx=0, fy=0, interpolation=cv2.INTER_CUBIC)
                        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                        frame = numpy.rot90(frame)
                        frame = pygame.surfarray.make_surface(frame)
                        self.surface.blit(frame, (self.x, self.y))
                    else:
                        self.close()
                        if self.playInLoop:
                            self.open()

第3步:这就是制作视频播放器对象的方法

代码语言:javascript
运行
复制
        videoPlayer = VideoPlayer(Surface, x_position, y_position, video_file, Pass True if You want to play in a loop, Pass True if you Want to enable mirror effect)

如果要调整视频的大小,请使用

如果' aspectRatio‘为True,则它将根据您传递的宽度来维护aspectRatio。默认情况下,它将为False

代码语言:javascript
运行
复制
        videoPlayer.activeFrameResizer(width, height, aspectRatio=True)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47162268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档