How to stream video from an IP camera using PyQt5 QtMultimedia?

151 Views Asked by At

The task is to stream an image from 5 IP cameras using QtMultimedia from the PyQt5 framework. Initially I tried to use OpenCV, but the parallel broadcast of 5 video streams reduced fps. We decided to try QtMultimedia in an attempt to improve performance.

To begin with, I tried to connect to 1-2 cameras and further scale this solution. Camera Name: D-Link DCS-931L. I connect the camera to the network via tp-link.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QMainWindow, QMessageBox, QPushButton
from PyQt5.QtMultimedia import QCamera, QCameraInfo
from PyQt5.QtMultimediaWidgets import QCameraViewfinder
from PyQt5.QtCore import QUrl


class DualCameraApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Dual Camera App')
        self.setGeometry(100, 100, 800, 600)

        self.camera1 = QCamera(QCameraInfo.defaultCamera())
        #self.camera2 = QCamera(QCameraInfo.availableCameras()[1])

        self.viewfinder1 = QCameraViewfinder()
        #self.viewfinder2 = QCameraViewfinder()

        self.camera1.setViewfinder(self.viewfinder1)
        #self.camera2.setViewfinder(self.viewfinder2)

        self.camera1.start()
        #self.camera2.start()


        layout = QVBoxLayout()
        layout.addWidget(self.viewfinder1)
        #layout.addWidget(self.viewfinder2)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = DualCameraApp()
    window.show()
    sys.exit(app.exec_())

I use the Camera class to work with cameras. I use the QCameraInfo class to connect the camera. It has the defaultCamera() and available Cameras() methods. The first method gives the default system camera, and the second gives a list of cameras in the system.

Problem: These methods do not allow access to IP cameras. In total, I can only connect to my camera in my laptop.

Then I was able to get a link to the video stream and tried to connect using the link.

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent, QCamera
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtCore import QUrl


class VideoPlayer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("IP Camera Video Stream")
        self.setGeometry(100, 100, 800, 600)

        video_widget = QVideoWidget()
        layout = QVBoxLayout()
        layout.addWidget(video_widget)

        container = QWidget()
        container.setLayout(layout)
        self.setCentralWidget(container)

        self.media_player = QMediaPlayer(self, QMediaPlayer.VideoSurface)
        self.media_player.setVideoOutput(video_widget)

        # http://IP/h264.flv - with sound?
        url = "http://admin:[email protected]/video.cgi"
        self.media_content = QMediaContent(QUrl(url))
        self.media_player.setMedia(self.media_content)
        self.media_player.play()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    player = VideoPlayer()
    player.show()
    sys.exit(app.exec_())

But it is also not successful. At the output, I get an empty window with no video stream.

Please help me figure it out.

Update:

my attempt to rewrite the code in PyQt6

import sys
from PyQt6.QtWidgets import QApplication, QMainWindow, QVBoxLayout, QWidget
from PyQt6.QtMultimedia import QMediaPlayer
from PyQt6.QtMultimediaWidgets import QVideoWidget
from PyQt6.QtCore import QUrl


class VideoPlayer(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("IP Camera Video Stream")
        self.setGeometry(100, 100, 800, 600)

        video_widget = QVideoWidget()
        layout = QVBoxLayout()
        layout.addWidget(video_widget)

        self.container = QWidget()
        self.container.setLayout(layout)
        self.setCentralWidget(self.container)

        url = "rtsp://admin:[email protected]/play1.sdp"
        self.media_player = QMediaPlayer(self)
        self.media_player.setSource(QUrl(url))
        self.video_widget = QVideoWidget()
        self.media_player.setVideoOutput(self.video_widget)
        # self.video_widget.show()
        self.media_player.play()



if __name__ == '__main__':
    app = QApplication(sys.argv)
    player = VideoPlayer()
    player.show()
    sys.exit(app.exec())
0

There are 0 best solutions below