当前位置: 代码网 > it编程>前端脚本>Python > 基于Python制作简易视频播放器

基于Python制作简易视频播放器

2024年11月25日 Python 我要评论
先上效果图:这个就是用python-pyqt5-opencv做出来的简易视频播放器,主要实现本地视频文件播放、本地摄像头播放和远程摄像头播放三个功能。核心代码:def showcamera(self,

先上效果图:

 这个就是用python-pyqt5-opencv做出来的简易视频播放器,主要实现本地视频文件播放、本地摄像头播放和远程摄像头播放三个功能。

核心代码:

def showcamera(self, url):
    try:
        if url == none:
            self.cap = cv2.videocapture(0)
        else:
            self.cap = cv2.videocapture(url)
 
        print('摄像头是否开启: {}'.format(self.cap.isopened()))
        if self.cap.isopened:
            self.cap.set(cv2.cap_prop_frame_width, 640)
            self.cap.set(cv2.cap_prop_frame_height, 480)
            self.cap.set(cv2.cap_prop_fps, 25)
 
            print(self.cap.get(3))
            print(self.cap.get(4))
            print(self.cap.get(5))
            print('开始读取摄像头数据......')
 
            while(true):
                ret, color_frame = self.cap.read()
                if ret == false:
                    return
                if url == none:
                    color_frame = cv2.flip(color_frame, 1)
                cv2.waitkey(1)
                im = cv2.cvtcolor(color_frame, cv2.color_rgb2bgr)
                a = qimage(im.data, im.shape[1], im.shape[0], qimage.format_rgb888)
                self.setpic(a)
 
            self.cap.release()
        else:
            print('camera open failed')
    except exception as e:
        print(str(e))

三类播放使用的都是同一个showcamera()函数,唯一的区别就是函数中的url参数不同。

文件播放:url=文件名

本地相机:url=0

网络串流:url=‘rtsp://……’

除了这个核心代码,打开文件使用的是qfiledialog,打开网络串流使用的是自定义的qinputdialog,两个代码如下:

def openfile(self):
    filename, filetype = qfiledialog.getopenfilename(self, '选择文件')
    print(filename, filetype)
    self.showcamera(filename)
def remote(self):
    input_dialog = qtwidgets.qinputdialog(self)
    input_dialog.setinputmode(qinputdialog.textinput)
    input_dialog.setwindowtitle('打开网络串流')
    input_dialog.setlabeltext('请输入网络串流地址rtsp://')
    input_dialog.setfixedsize(500, 100)
    input_dialog.show()
    if input_dialog.exec_() == input_dialog.accepted:
        text = input_dialog.textvalue()
        if text != '':
            print(text)
            self.showcamera(text)
        else:
            print('地址错误或空')

最后,是用qlabel加载图片的代码:

def setpic(self, image):
    self.label.setpixmap(qpixmap.fromimage(image))

剩下的就是ui界面的定义了

到此这篇关于基于python制作简易视频播放器的文章就介绍到这了,更多相关python视频播放器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com