<div dir="ltr"><div class="gmail_quote"><div dir="ltr">hi all<div><br></div><div>I'm trying to play a video via RTP but strange things happen.<br><br></div><div>The video plays but not always and does not return any error message. Sometimes when I open it it works and another does not<br><br>I don't know if I'm missing a flag or I'm doing it wrong.</div><div><br>Any suggestion about this?</div><div><br></div><div>this is my code</div><div><br></div><div>import sys<br>import os<br>from PyQt5.QtGui import *<br>from PyQt5.QtWidgets import *<br>from PyQt5.QtCore import *<br>from PyQt5.QtNetwork import *<br>from PyQt5.QtMultimedia import *<br>from PyQt5.QtMultimediaWidgets import *<br>import time<br><br>fileName = 'rtp://<a href="http://127.0.0.1:8888" target="_blank">127.0.0.1:8888</a>'<br><br><br>class VideoWidgetSurface(QAbstractVideoSurface):<br><br> def __init__(self, widget, parent=None):<br> super(VideoWidgetSurface, self).__init__(parent)<br> <br> self.widget = widget<br> self.imageFormat = QImage.Format_Invalid<br> <br> def supportedPixelFormats(self, handleType=QAbstractVideoBuffer.NoHandle):<br> formats = [QVideoFrame.PixelFormat()]<br> if (handleType == QAbstractVideoBuffer.NoHandle):<br> for f in [QVideoFrame.Format_RGB32,<br> QVideoFrame.Format_ARGB32,<br> QVideoFrame.Format_ARGB32_Premultiplied,<br> QVideoFrame.Format_RGB565,<br> QVideoFrame.Format_RGB555<br> ]:<br> formats.append(f)<br> return formats<br> <br> def isFormatSupported(self, _format):<br> imageFormat = QVideoFrame.imageFormatFromPixelFormat(_format.pixelFormat())<br> size = _format.frameSize()<br> _bool = False<br> if (imageFormat != QImage.Format_Invalid and not \<br> size.isEmpty() and _format.handleType() == QAbstractVideoBuffer.NoHandle):<br> _bool = True<br> return _bool<br> <br> def start(self, _format):<br> imageFormat = QVideoFrame.imageFormatFromPixelFormat(_format.pixelFormat())<br> size = _format.frameSize()<br> if (imageFormat != QImage.Format_Invalid and not size.isEmpty()):<br> self.imageFormat = imageFormat<br> self.imageSize = size<br> self.sourceRect = _format.viewport()<br> QAbstractVideoSurface.start(self, _format)<br> self.widget.updateGeometry()<br> self.updateVideoRect()<br> return True<br> else:<br> return False<br> <br> def stop(self):<br> self.currentFrame = QVideoFrame()<br> self.targetRect = QRect()<br> QAbstractVideoSurface.stop(self)<br> self.widget.update()<br> <br> def present(self, frame):<br> if (self.surfaceFormat().pixelFormat() != frame.pixelFormat() or self.surfaceFormat().frameSize() != frame.size()):<br> self.setError(QAbstractVideoSurface.IncorrectFormatError)<br> self.stop()<br> return False<br> else:<br> self.currentFrame = frame<br> self.widget.repaint(self.targetRect)<br> return True<br> <br> def videoRect(self):<br> return self.targetRect<br> <br> def updateVideoRect(self):<br> size = self.surfaceFormat().sizeHint()<br> size.scale(self.widget.size().boundedTo(size), Qt.KeepAspectRatio)<br> self.targetRect = QRect(QPoint(0, 0), size)<br> self.targetRect.moveCenter(self.widget.rect().center())<br> <br> def paint(self, painter):<br> try: # maybe raise no �cruuentFrame� error<br> if (self.currentFrame.map(QAbstractVideoBuffer.ReadOnly)):<br> oldTransform = painter.transform()<br> <br> if (self.surfaceFormat().scanLineDirection() == QVideoSurfaceFormat.BottomToTop):<br> painter.scale(1, -1);<br> painter.translate(0, -self.widget.height())<br> <br> image = QImage(self.currentFrame.bits(), self.currentFrame.width(), \<br> self.currentFrame.height(), self.currentFrame.bytesPerLine(), self.imageFormat)<br> <br> painter.drawImage(self.targetRect, image, self.sourceRect)<br> painter.setTransform(oldTransform)<br> <br> self.currentFrame.unmap()<br> except Exception as e:<br> print("Error paint : " , str(e))<br> pass<br><br><br>class VideoWidget(QWidget):<br><br> def __init__(self, parent=None):<br> super(VideoWidget, self).__init__(parent)<br> <br> self.setAutoFillBackground(False)<br> self.setAttribute(Qt.WA_NoSystemBackground, True)<br> self.setAttribute(Qt.WA_PaintOnScreen, True)<br> palette = self.palette()<br> palette.setColor(QPalette.Background, Qt.black)<br> self.setPalette(palette)<br> self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)<br> self.surface = VideoWidgetSurface(self)<br> <br> def videoSurface(self):<br> return self.surface<br> <br> def closeEvent(self, event):<br> del self.surface<br> <br> def sizeHint(self):<br> return self.surface.surfaceFormat().sizeHint()<br> <br> def paintEvent(self, event):<br> painter = QPainter(self)<br> painter.setRenderHint(QPainter.HighQualityAntialiasing)<br> print ("surface active : ", self.surface.isActive())<br> if (self.surface.isActive()):<br> self.surface.paint(painter)<br> else:<br> painter.fillRect(event.rect(), self.palette().window())<br> <br> def resizeEvent(self, event):<br> QWidget.resizeEvent(self, event)<br> self.surface.updateVideoRect()<br><br><br>class VideoPlayer(QWidget):<br><br> def __init__(self, parent=None):<br> super(VideoPlayer, self).__init__(parent)<br> <br> self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface or QMediaPlayer.StreamPlayback)<br> self.videoWidget = VideoWidget()<br> self.openButton = QPushButton("Open...")<br> self.openButton.clicked.connect(self.openFile)<br> self.playButton = QPushButton()<br> self.playButton.setEnabled(False)<br> self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))<br> self.playButton.clicked.connect(self.play)<br> <br> self.positionSlider = QSlider(Qt.Horizontal)<br> self.positionSlider.setRange(0, 0)<br> self.positionSlider.sliderMoved.connect(self.setPosition)<br> self.controlLayout = QHBoxLayout()<br> self.controlLayout.setContentsMargins(0, 0, 0, 0)<br> self.controlLayout.addWidget(self.openButton)<br> self.controlLayout.addWidget(self.playButton)<br> self.controlLayout.addWidget(self.positionSlider)<br> layout = QVBoxLayout()<br> layout.addWidget(self.videoWidget)<br> layout.addLayout(self.controlLayout)<br><br> self.setLayout(layout)<br><br> self.mediaPlayer.setVideoOutput(self.videoWidget.videoSurface())<br> self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)<br> self.mediaPlayer.positionChanged.connect(self.positionChanged)<br> self.mediaPlayer.durationChanged.connect(self.durationChanged)<br><br> url = QUrl(fileName)<br> media = QMediaContent(url)<br><br> self.mediaPlayer.setMedia(media)<br> self.playButton.setEnabled(True)<br> time.sleep(.300)<br> self.mediaPlayer.play()<br><br> def openFile(self):<br> file_name = QFileDialog.getOpenFileName(self, "Open Movie", QDir.homePath())[0]<br> if os.path.exists(file_name):<br> self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile(file_name)))<br> self.playButton.setEnabled(True)<br><br> def play(self):<br> if self.mediaPlayer.state() == QMediaPlayer.PlayingState:<br> self.mediaPlayer.pause()<br> else:<br> self.mediaPlayer.play()<br> <br> def mediaStateChanged(self, state):<br> if state == QMediaPlayer.PlayingState:<br> self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPause))<br> else:<br> self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))<br> <br> def positionChanged(self, position):<br> self.positionSlider.setValue(position)<br> <br> def durationChanged(self, duration):<br> self.positionSlider.setRange(0, duration)<br> <br> def setPosition(self, position):<br> self.mediaPlayer.setPosition(position)<br><br><br>if __name__ == '__main__':<br> app = QApplication(sys.argv)<br> <br> player = VideoPlayer()<br> player.setGeometry(100, 300, 600, 380)<br> player.setFixedSize(QSize(600, 600)) # setFixedSize(QSize)<br> player.show()<br> <br> sys.exit(app.exec_())<br><br><br>And for start server I'm using </div><div><br></div><div>ffmpeg -fflags +genpts -stream_loop -1 -re -i "C:\Users\Fran Raga\Desktop\video_test\Cheyenne.ts" -f rtp_mpegts -c copy -map 0:v -map 0:a -map 0:d rtp://<a href="http://127.0.0.1:8888" target="_blank">127.0.0.1:8888</a></div><div><br></div><div>thanks</div><div><br><div><div dir="ltr" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><font size="1"><b>Francisco Raga</b></font><b style="font-size:x-small"> | </b><span style="font-size:x-small">Full-Stack </span><span style="font-size:x-small">Open Source </span><span style="font-size:x-small">GIS Developer </span></div><div><span style="font-size:x-small"> </span></div><div><font size="1">Móvil: (+34) 654275432<b> | </b>e-Mail: <a href="mailto:franka1986@gmail.com" target="_blank">franka1986@gmail.com</a> <b>| </b>skype: francisco_raga<br>Github: <a href="https://goo.gl/ydNTjY" target="_blank">https://goo.gl/ydNTjY</a></font><span style="font-size:x-small"> </span><b><span style="font-size:x-small">|</span><span style="font-size:x-small"> </span></b><span style="font-size:x-small">Linkedin: </span><a href="https://goo.gl/TCfj8S" style="font-size:x-small" target="_blank">https://goo.gl/TCfj8S</a><span style="font-size:x-small"> </span><b><span style="font-size:x-small">|</span><span style="font-size:x-small"> </span></b><span style="font-size:x-small">Site: </span><a href="https://goo.gl/qiypDj" style="font-size:x-small" target="_blank">https://goo.gl/qiypDj</a></div><div><font size="1"><br>"La vida real no tiene ningún mapa.." Ivy Compton Burnett</font></div></div></div></div></div></div></div></div></div></div></div>
</div></div>