马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
https://mc.dfrobot.com.cn/thread-314088-1-1.html[M10项目]行空板视频播放
【处理视频】 因行空板屏幕为320*240,所以将视频使用格式工厂处理,并将帧频设置成25帧每秒(程序中好处理)。并将视频 中音频导出成MP3。 【制作背景】 下载视频背景图片,用Fireworks处理图片320*240并旋转图片(行空板横屏),并制作按钮图片,按钮背景透明,生成PNG格式。 【程序编写】 1、播放视频
导入OpenCV,加载视频,并初始化。
- import cv2
- vd = cv2.VideoCapture()
- vd.open("red.mp4")
- screen_rotation = True
- vd.set(cv2.CAP_PROP_FRAME_WIDTH, 320) #设置视频图像宽度
- vd.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置视频摄像头图像高度
- vd.set(cv2.CAP_PROP_BUFFERSIZE, 1) #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
复制代码 导入pygame用mixer加载音频,并设置音量。- import pygame
- pygame.mixer.init()
- pygame.mixer.music.load("red.mp3")
- pygame.mixer.music.set_volume(50 / 100)
复制代码 播放按钮界面设置
- from unihiker import GUI
- bf=False
- bs=True
- # 事件回调函数
- def button_click1():
- global bf
- bf=True
- u_gui=GUI()
- 屏幕=u_gui.draw_image(image="back.JPG",x=0,y=0)
- 按钮=u_gui.draw_image(image="an.png",x=90,y=130)
- 按钮.config(onclick=button_click1)
复制代码 主程序- currenttime=time.time()
- while True:
- if bf==True:
- if bs==True:
- bs=False
- cv2.namedWindow('windows',cv2.WND_PROP_FULLSCREEN) #窗口全屏
- cv2.setWindowProperty('windows', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #窗口全屏
- pygame.mixer.music.play()
- if time.time()-currenttime>=0.039:
- currenttime=time.time()
- ret, img = vd.read()
- if ret:
- #img = cv2.resize(img,(320,240))
- if screen_rotation:
- img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) #旋转屏幕
- cv2.imshow('windows', img)
- else:
- cv2.destroyAllWindows()
- vd.set(cv2.CAP_PROP_POS_MSEC, 0)
- bf=False
- bs=True
- if cv2.waitKey(1) & 0xff== 27:
- break
复制代码 全屏播放
- cv2.namedWindow('windows',cv2.WND_PROP_FULLSCREEN) #窗口全屏
- cv2.setWindowProperty('windows', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #窗口全屏
复制代码 使用横屏播放- img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) #旋转屏幕
复制代码 播放屏幕连续显示,需使用“cv2.waitKey(1)”。
- if cv2.waitKey(1) & 0xff== 27:
- break
复制代码 因视频的帧频为25帧每秒,所以0.04秒播放1帧,因“cv2.waitKey(1)”占了0.001秒,所以每0.039秒播放1帧。
- if time.time()-currenttime>=0.039:
- currenttime=time.time()
- ret, img = vd.read()
复制代码
【完整程序】
- import pygame
- import time
- import cv2
- from unihiker import GUI
- bf=False
- bs=True
- # 事件回调函数
- def button_click1():
- global bf
- bf=True
- u_gui=GUI()
- 屏幕=u_gui.draw_image(image="back.JPG",x=0,y=0)
- 按钮=u_gui.draw_image(image="an.png",x=90,y=130)
- 按钮.config(onclick=button_click1)
- vd = cv2.VideoCapture()
- vd.open("red.mp4")
- screen_rotation = True
- vd.set(cv2.CAP_PROP_FRAME_WIDTH, 320) #设置视频图像宽度
- vd.set(cv2.CAP_PROP_FRAME_HEIGHT, 240) #设置视频摄像头图像高度
- vd.set(cv2.CAP_PROP_BUFFERSIZE, 1) #设置OpenCV内部的图像缓存,可以极大提高图像的实时性。
- pygame.mixer.init()
- pygame.mixer.music.load("red.mp3")
- pygame.mixer.music.set_volume(50 / 100)
- currenttime=time.time()
- while True:
- if bf==True:
- if bs==True:
- bs=False
- cv2.namedWindow('windows',cv2.WND_PROP_FULLSCREEN) #窗口全屏
- cv2.setWindowProperty('windows', cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN) #窗口全屏
- pygame.mixer.music.play()
- if time.time()-currenttime>=0.039:
- currenttime=time.time()
- ret, img = vd.read()
- if ret:
- #img = cv2.resize(img,(320,240))
- if screen_rotation:
- img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) #旋转屏幕
- cv2.imshow('windows', img)
- else:
- cv2.destroyAllWindows()
- vd.set(cv2.CAP_PROP_POS_MSEC, 0)
- bf=False
- bs=True
- if cv2.waitKey(1) & 0xff== 27:
- break
复制代码
行空板——视频播放器
|