极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 7208|回复: 1

在PyQt4窗体中播放mjpg-streamer输出视频

[复制链接]
发表于 2012-11-23 22:01:17 | 显示全部楼层 |阅读模式
     最近买了TP-LINK703N路由器,按照论坛中的资料成功刷入openwrt。安装mjpg-streamer后,可正常在网页中观看视频。后面打算自己写个上位机程序,就研究了下如何用在基于PyQt4的窗口中播放视频。以下示例代码可正常播放视频,希望对有相同需求的同学有所帮助。

两个文件:PyStreamClientUi.py,PyStreamClient.py。双击PyStreamClient.py运行
  1. # -*- coding: utf-8 -*-
  2. #filename: PyStreamClientUi.py
  3. # Form implementation generated from reading ui file 'StreamClient.ui'
  4. #
  5. # Created: Fri Nov 23 21:50:32 2012
  6. #      by: PyQt4 UI code generator 4.8.3
  7. #
  8. # WARNING! All changes made in this file will be lost!

  9. from PyQt4 import QtCore, QtGui

  10. try:
  11.     _fromUtf8 = QtCore.QString.fromUtf8
  12. except AttributeError:
  13.     _fromUtf8 = lambda s: s

  14. class Ui_PyStreamClientForm(object):
  15.     def setupUi(self, PyStreamClientForm):
  16.         PyStreamClientForm.setObjectName(_fromUtf8("PyStreamClientForm"))
  17.         PyStreamClientForm.resize(345, 259)
  18.         self.label = QtGui.QLabel(PyStreamClientForm)
  19.         self.label.setGeometry(QtCore.QRect(10, 10, 320, 240))
  20.         self.label.setFrameShape(QtGui.QFrame.StyledPanel)
  21.         self.label.setText(_fromUtf8(""))
  22.         self.label.setObjectName(_fromUtf8("label"))

  23.         self.retranslateUi(PyStreamClientForm)
  24.         QtCore.QMetaObject.connectSlotsByName(PyStreamClientForm)

  25.     def retranslateUi(self, PyStreamClientForm):
  26.         PyStreamClientForm.setWindowTitle(QtGui.QApplication.translate("PyStreamClientForm", "PyStreamClient", None, QtGui.QApplication.UnicodeUTF8))

复制代码
  1. #-*- coding: utf-8 -*-
  2. #author: apache
  3. #filename: PyStreamClient.py

  4. import sys, httplib
  5. from PyQt4.QtGui import QWidget, QApplication, QPixmap
  6. from PyQt4.QtCore import QTimer
  7. from PyStreamClientUi import Ui_PyStreamClientForm

  8. class QueryImage:
  9.     def __init__(self, host, port, argv="/?action=snapshot"):
  10.         self.host = host
  11.         self.port = port
  12.         self.argv = argv
  13.         
  14.     def queryImage(self):
  15.         http = httplib.HTTP(self.host, self.port)
  16.         http.putrequest('GET', self.argv)
  17.         http.putheader('Host', self.host)
  18.         http.putheader('User-agent', 'python-httplib')
  19.         http.putheader('Content-type', 'image/jpeg')
  20.         http.endheaders()
  21.         (returncode, returnmsg, headers) = http.getreply()
  22.         if returncode != 200:
  23.             print returncode, returnmsg
  24.             return None
  25.             
  26.         return http.getfile().read()
  27.         
  28. class StreamClientForm(QWidget):
  29.     TIMEOUT = 50
  30.     def __init__(self, parent=None):
  31.         QWidget.__init__(self, parent)
  32.         self.ui = Ui_PyStreamClientForm()
  33.         self.queryImage = QueryImage("192.168.2.1", 8080)
  34.         self.timer = QTimer(timeout=self.playStream)
  35.         self.__setup()
  36.         
  37.     def __setup(self):
  38.         self.ui.setupUi(self)
  39.         self.timer.start(StreamClientForm.TIMEOUT)
  40.    
  41.     def transToPixmap(self):
  42.         data = self.queryImage.queryImage()
  43.         if not data:
  44.             return None
  45.         pixmap = QPixmap()
  46.         pixmap.loadFromData(data)
  47.         return pixmap
  48.         
  49.     def playStream(self):
  50.         pixmap = self.transToPixmap()
  51.         if pixmap:
  52.             self.ui.label.setPixmap(pixmap)
  53.         else:
  54.             print "frame lost"
  55.             
  56. if __name__ == "__main__":
  57.     app = QApplication(sys.argv)
  58.     app.setStyle("plastique")
  59.     form = StreamClientForm()
  60.     form.show()
  61.     sys.exit(app.exec_())
  62.    
复制代码
回复

使用道具 举报

发表于 2013-2-26 12:26:13 | 显示全部楼层
请问楼主,用的703N刷的openwrt挂载的是什么摄像头?UVC的摄像头要怎么安装驱动呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊 ( 浙ICP备09023225号 )

GMT+8, 2019-12-16 17:47 , Processed in 0.043275 second(s), 24 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表