I've been working with python for a while. I am writing a script in which the VLС media player should be launched in a PyQT5. The media player starts the video by the number entered in the terminal. After playing the video, the media player waits for the number to be written to the terminal to play the next file. I will be grateful for any help.
import sys
import time
import vlc
from PyQt5 import QtGui, QtWidgets
from PyQt5.QtCore import Qt
class Player(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Player, self).__init__(parent)
#self.setWindowTitle("Media Player")
# creating a basic vlc instance
self.instance = vlc.Instance()
self.instance.log_unset()
self.mediaplayer = self.instance.media_player_new()
##########video frame
self.videoframe = QtWidgets.QFrame(
frameShape=QtWidgets.QFrame.Box, frameShadow=QtWidgets.QFrame.Raised
)
if sys.platform.startswith("linux"): # for Linux using the X Server
self.mediaplayer.set_xwindow(self.videoframe.winId())
elif sys.platform == "win32": # for Windows
self.mediaplayer.set_hwnd(self.videoframe.winId())
elif sys.platform == "darwin": # for MacOS
self.mediaplayer.set_nsobject(self.videoframe.winId())
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
self.setWindowFlags(Qt.FramelessWindowHint)
#self.setWindowState(Qt.WindowFullScreen)
lay = QtWidgets.QVBoxLayout(central_widget)
lay.addWidget(self.videoframe)
filename = (f"/home/neuro/Видео/fluger/лопасти/{numberfile}.mp4")
media = self.instance.media_new(filename)
self.mediaplayer.set_media(media)
self.mediaplayer.play()
#time.sleep(10)
#self.mediaplayer.stop()
def main():
app = QtWidgets.QApplication(sys.argv)
player = Player()
player.show()
player.move(1, 1)
player.resize(406, 86)
player.setObjectName("MainWindow")
player.setStyleSheet("#MainWindow{background-color:black}")
sys.exit(app.exec_())
if __name__ == "__main__":
while True:
numberfile = input("Номер файла: ")
main()
I understand that there should be a cycle. but how to implement it so that after the first playback it re-requests the number for the next playback?
Related
I am working on an application where several vlc streams (rtsp) are shown and by double-clicking one of them, the stream should be displayed fullscreen.
The application is python 3.7 using pyqt5 and vlc-qt.
Code as follows:
import sys
import vlc
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.sizeHint = lambda: QSize(1280, 900)
self.move(100, 10)
self.videoFrame = QFrame()
self.setCentralWidget(self.videoFrame)
self.vlcInstance = vlc.Instance(['--video-on-top'])
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer.set_mrl("rtsp://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer.set_xwindow(self.videoFrame.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer.set_hwnd(self.videoFrame.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer.set_nsobject(int(self.videoFrame.winId()))
self.videoPlayer.play()
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("VLC Test")
window = MainWindow()
app.exec_()
When double clicking the video, the following console message appears:
[000001e0a128e630] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[000001e0a12c8710] mmdevice audio output error: cannot initialize COM (error 0x80010106)
[000001e0a2927420] main vout display error: Failed to set fullscreen
The message "Failed to set fullscreen" appears as soon as I double-click.
Does anyone have an idea what the problem might be?
Thanks in advance
import sys
import vlc
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.sizeHint = lambda: QSize(1280, 900)
self.move(100, 10)
self.mainFrame = QFrame()
self.setCentralWidget(self.mainFrame)
t_lay_parent = QHBoxLayout()
t_lay_parent.setContentsMargins(0, 0, 0, 0)
self.videoFrame = QFrame()
self.videoFrame.mouseDoubleClickEvent = self.mouseDoubleClickEvent
t_lay_parent.addWidget(self.videoFrame)
self.vlcInstance = vlc.Instance(['--video-on-top'])
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer = self.vlcInstance.media_player_new()
self.videoPlayer.video_set_mouse_input(False)
self.videoPlayer.video_set_key_input(False)
self.videoPlayer.set_mrl("http://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer.set_xwindow(self.videoFrame.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer.set_hwnd(self.videoFrame.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer.set_nsobject(int(self.videoFrame.winId()))
self.videoPlayer.play()
self.videoFrame1 = QFrame()
t_lay_parent.addWidget(self.videoFrame1)
self.videoFrame1.mouseDoubleClickEvent = self.mouseDoubleClickEvent1
self.vlcInstance1 = vlc.Instance(['--video-on-top'])
self.videoPlayer1 = self.vlcInstance1.media_player_new()
self.videoPlayer1 = self.vlcInstance1.media_player_new()
self.videoPlayer1.video_set_mouse_input(False)
self.videoPlayer1.video_set_key_input(False)
self.videoPlayer1.set_mrl("rtmp://xxx.xxx.xxx.xxx", "network-caching=300")
self.videoPlayer1.audio_set_mute(True)
if sys.platform.startswith('linux'): # for Linux using the X Server
self.videoPlayer1.set_xwindow(self.videoFrame1.winId())
elif sys.platform == "win32": # for Windows
self.videoPlayer1.set_hwnd(self.videoFrame1.winId())
elif sys.platform == "darwin": # for MacOS
self.videoPlayer1.set_nsobject(int(self.videoFrame1.winId()))
self.videoPlayer1.play()
self.mainFrame.setLayout(t_lay_parent)
self.show()
def mouseDoubleClickEvent(self, event):
if event.button() == Qt.LeftButton:
if self.windowState() == Qt.WindowNoState:
self.videoFrame1.hide()
self.videoFrame.show()
self.setWindowState(Qt.WindowFullScreen)
else:
self.videoFrame1.show()
self.setWindowState(Qt.WindowNoState)
def mouseDoubleClickEvent1(self, event):
if event.button() == Qt.LeftButton:
if self.windowState() == Qt.WindowNoState:
self.videoFrame.hide()
self.videoFrame1.show()
self.setWindowState(Qt.WindowFullScreen)
else:
self.videoFrame.show()
self.setWindowState(Qt.WindowNoState)
if __name__ == "__main__":
app = QApplication(sys.argv)
app.setApplicationName("VLC Test")
window = MainWindow()
app.exec_()
I’m creating a special purpose video player in Python 3.6 using OpenCV3 and ffmepg for handling the images and using PyQt5 for the Windows environment. I chose this combination of packages because ffmpeg handles a wider variety of codecs than QtMultimedia.
I’ve run into one snag. My player does not play at regular speed – it plays at roughly ¾ of normal speed. I use QTimer.timer to loop my display engine (nextFrameSlot) at a speed of 1/framerate.
Any suggestions on how to get the video to play at regular speed? Here is an abbreviated set of code that demonstrates my problem.
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QFormLayout, QPushButton, QMainWindow
from PyQt5.QtWidgets import QAction, QMessageBox, QApplication, QFileDialog
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtCore import QTimer
import cv2
class VideoCapture(QWidget):
def __init__(self, filename, parent):
super(QWidget, self).__init__()
self.cap = cv2.VideoCapture(str(filename[0]))
self.length = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
self.frame_rate = self.cap.get(cv2.CAP_PROP_FPS)
#self.codec = self.cap.get(cv2.CAP_PROP_FOURCC)
self.video_frame = QLabel()
parent.layout.addWidget(self.video_frame)
def nextFrameSlot(self):
ret, frame = self.cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
self.video_frame.setPixmap(pix)
def start(self):
self.timer = QTimer()
self.timer.timeout.connect(self.nextFrameSlot)
self.timer.start(1000.0/self.frame_rate)
def pause(self):
self.timer.stop()
def deleteLater(self):
self.cap.release()
super(QWidget, self).deleteLater()
class VideoDisplayWidget(QWidget):
def __init__(self,parent):
super(VideoDisplayWidget, self).__init__(parent)
self.layout = QFormLayout(self)
self.startButton = QPushButton('Start', parent)
self.startButton.clicked.connect(parent.startCapture)
self.startButton.setFixedWidth(50)
self.pauseButton = QPushButton('Pause', parent)
self.pauseButton.setFixedWidth(50)
self.layout.addRow(self.startButton, self.pauseButton)
self.setLayout(self.layout)
class ControlWindow(QMainWindow):
def __init__(self):
super(ControlWindow, self).__init__()
self.setGeometry(50, 50, 800, 600)
self.setWindowTitle("PyTrack")
self.capture = None
self.isVideoFileLoaded = False
self.quitAction = QAction("&Exit", self)
self.quitAction.setShortcut("Ctrl+Q")
self.quitAction.triggered.connect(self.closeApplication)
self.openVideoFile = QAction("&Open Video File", self)
self.openVideoFile.setShortcut("Ctrl+Shift+V")
self.openVideoFile.triggered.connect(self.loadVideoFile)
self.mainMenu = self.menuBar()
self.fileMenu = self.mainMenu.addMenu('&File')
self.fileMenu.addAction(self.openVideoFile)
self.fileMenu.addAction(self.quitAction)
self.videoDisplayWidget = VideoDisplayWidget(self)
self.setCentralWidget(self.videoDisplayWidget)
def startCapture(self):
if not self.capture and self.isVideoFileLoaded:
self.capture = VideoCapture(self.videoFileName, self.videoDisplayWidget)
self.videoDisplayWidget.pauseButton.clicked.connect(self.capture.pause)
self.capture.start()
def endCapture(self):
self.capture.deleteLater()
self.capture = None
def loadVideoFile(self):
try:
self.videoFileName = QFileDialog.getOpenFileName(self, 'Select a Video File')
self.isVideoFileLoaded = True
except:
print ("Please Select a Video File")
def closeApplication(self):
choice = QMessageBox.question(self, 'Message','Do you really want to exit?',QMessageBox.Yes | QMessageBox.No)
if choice == QMessageBox.Yes:
print("Closing....")
sys.exit()
else:
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = ControlWindow()
window.show()
sys.exit(app.exec_())
Solved - I needed to specify self.timer.setTimerType(Qt.PreciseTimer) after the statement self.timer = QTimer() in the function start(self). By default, QTimer() uses a coarse timer. For Windows, the coarse time is 15.6 msec intervals.
I'm trying to make a simple video player app by embedding a VLC instance inside a PyQt widget (a QFrame). I found a few examples that go me going, but my code doesn't quite work. When I launch it, it plays "test_video.mp4", but it launches the regular VLC player app in its own, separate window. When I close out of the VLC player window, obviously the video stops, but the audio continues playing until I close my own Qt (PyQt) window.
edit 1: Forgot to mention I am using python-vlc, downloaded via pip.
### video_player.py
import sys
import vlc
from PyQt4 import QtCore, QtGui
from video_player_main_window import Ui_MainWindow
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.vlc_instance = vlc.Instance("--no-xlib --sout-all")
self.mediaplayer = self.vlc_instance.media_player_new()
self.mediaplayer.set_xwindow(self.ui.video_frame.winId())
print(self.ui.video_frame.winId())
self.media_path = "test_video.mp4"
self.media = self.vlc_instance.media_new(self.media_path)
self.mediaplayer = self.vlc_instance.media_player_new()
self.mediaplayer.set_media(self.media)
self.mediaplayer.play()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
I added a "print(self.ui.video_frame.win())" just for debugging / sanity check to make sure that was a legitimate value. Command line output below. The "X server failure" shows up after I close the VLC window while my PyQt window is still running.
### command line output
106954771
[00007f9c48055168] vdpau_avcodec generic error: Xlib is required for VDPAU
[00007f9c3c003968] xcb_window window error: X server failure
The "video_player_main_window" is the module that QtDesigner (+ pyuic4) generates. "video_frame" is the name of the QFrame object I'm trying to put the VLC instance into. See full code for video_player_main_window.py here: http://pastebin.com/cHpAHZN2
how if like this :
import sys
import vlc
from PyQt4 import QtCore, QtGui
from video_player_main_window import Ui_MainWindow
class StartQT4(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.vlc_instance = vlc.Instance()
self.mediaplayer = self.vlc_instance.media_player_new()
self.mediaplayer.set_hwnd(int(self.frame.winId()))
self.media_path = "test_video.mp4"
self.media = self.vlc_instance.media_new(self.media_path)
self.media.get_mrl()
self.mediaplayer.set_media(self.media)
self.mediaplayer.play()
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
sys.exit(app.exec_())
i usualy use this for my simple player.
I am trying to make an audio player based on pyqt5 . hovewer reading the examples at the site packages and visiting also the QAudioOutput strange peak sound at beginning in PyQt4 i couldn't make a step forward and it seems there is no audio output. Thanks in advance , this is my current code (python3.3)
import sys
from PyQt5 import QtGui, QtCore, QtWidgets, QtMultimedia
class Window(QtWidgets.QWidget):
def __init__(self, parent = None):
QtWidgets.QWidget.__init__(self, parent)
format = QtMultimedia.QAudioFormat()
format.setSampleRate(44100)
format.setSampleSize(16)
format.setChannelCount(1)
format.setCodec("audio/wav")
format.setByteOrder(QtMultimedia.QAudioFormat.LittleEndian)
format.setSampleType(QtMultimedia.QAudioFormat.SignedInt)
self.output = QtMultimedia.QAudioOutput(QtMultimedia.QAudioDeviceInfo.defaultOutputDevice(), format, self)
self.file=QtCore.QFile()
self.file.setFileName("data/voicemail/99dc6d529c0c5d6b2ec5cda7b26cc1bb9af58bd4774d161f66186f62bf3093cd.wav")
self.file.open(QtCore.QIODevice.ReadOnly)
self.file.seek(44)
self.output.start(self.file)
#self.file.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
I am currently using PyQt4 to develop a video player GUI, and I use QtGui.QSlider to control the progress of video stream. And I am wondering how can I change the slider's value when video is playing?
import sys, os
import xml.etree.ElementTree as ET
from xml.dom import minidom
from PyQt4 import QtCore, QtGui, uic
from PyQt4.phonon import Phonon
class vidplayer(QtGui.QWidget):
def __init__(self,url,xml_url,parent = None):
self.url = url
super(vidplayer,self).__init__()
self.initUI()
self.getElm_from_XML(xml_url);
def printXMLInfo(self):
itemlist = self.doc.getElementsByTagName('object')
for item in itemlist:
print ("frame-span:"+str(item.attributes['framespan'].value)+
" Event tag: "+ str(item.attributes['name'].value));
print(len(itemlist))
def getElm_from_XML(self,xml_url):
self.doc = minidom.parse(xml_url)
self.clipList = self.doc.getElementsByTagName('object');
print("Reading XML done...\n Have read %s elements.\n" %(len(self.clipList)))
def initUI(self):
## create widgets
# phonon video player and media
self.vp = Phonon.VideoPlayer()
media = Phonon.MediaSource(self.url)
# layout components (boxes)
self.vbox_play = QtGui.QVBoxLayout()
self.hbox_ctrl_vid = QtGui.QHBoxLayout()
self.hbox_ctrl_dec = QtGui.QHBoxLayout()
self.hbox = QtGui.QHBoxLayout()
self.vbox_control = QtGui.QVBoxLayout()
# bottons to control
self.btn_go_prev = QtGui.QPushButton("|<")
self.btn_go_next = QtGui.QPushButton(">|")
self.btn_play = QtGui.QPushButton("Play(Pause)")
self.btn_accept = QtGui.QPushButton("Accept")
self.btn_reject = QtGui.QPushButton("Reject")
# slider to interact with videoplayer
self.sld = QtGui.QSlider(QtCore.Qt.Horizontal,self)
#self.sld = Phonon.SeekSlider(self)
## layout components setup
self.vbox_control.addStretch(1)
self.hbox_ctrl_vid.addStretch(1)
self.hbox_ctrl_dec.addStretch(1)
self.vbox_play.addStretch(1)
self.hbox.addStretch(1)
self.vbox_control.setDirection(QtGui.QBoxLayout.BottomToTop)
self.vbox_play.setDirection(QtGui.QBoxLayout.BottomToTop)
## widgets inits
self.vp.load(media)
self.vp.play()
self.sld.setFocusPolicy(QtCore.Qt.NoFocus)
self.sld.setRange(1,1000)
## widgets assignment
self.hbox_ctrl_vid.addWidget(self.btn_go_prev)
self.hbox_ctrl_vid.addWidget(self.btn_play)
self.hbox_ctrl_vid.addWidget(self.btn_go_next)
self.hbox_ctrl_dec.addWidget(self.btn_accept)
self.hbox_ctrl_dec.addWidget(self.btn_reject)
self.vbox_play.addWidget(self.vp)
self.vbox_play.addWidget(self.sld)
self.vbox_control.addLayout(self.hbox_ctrl_dec)
self.vbox_control.addLayout(self.hbox_ctrl_vid)
self.hbox.addLayout(self.vbox_play)
self.hbox.addLayout(self.vbox_control)
## main setup and display
self.setLayout(self.hbox)
self.setGeometry(300,300,600,400)
self.setWindowTitle('CCNY_SRI TRECVid iSED UI')
self.setWindowIcon(QtGui.QIcon('./icon.png'))
self.show()
## connection set up
self.sld.valueChanged[int].connect(self.sld_changeValue)
self.vp.finished.connect(self.onReachingFinish)
self.btn_play.clicked.connect(self.onClicked_play)
self.btn_go_next.clicked.connect(self.onClicked_nextClip)
self.btn_go_prev.clicked.connect(self.onClicked_prevClip)
###################### callable functions ##################
def sld_changeValue(self,value):
totalT = self.vp.totalTime()
print totalT
newT = totalT*value/1000
self.vp.seek(newT)
def onClicked_play(self):
# BUG: sth wrong with boundary
if self.vp.isPaused():
self.vp.play()
print("resume play")
elif self.vp.isPlaying():
self.vp.pause()
print("pause at",self.sld.value())
elif self.sld.value()<1000:
self.vp.play()
def onClicked_nextClip(self):
print("go next")
def onClicked_prevClip(self):
print("go prev")
def onClicked_acc(self):
print("accepted")
def onClicked_rej(self):
print("rejected")
def onReachingFinish(self):
self.vp.pause()
self.vp.stop()
def main():
app = QtGui.QApplication(sys.argv)
window = vidplayer(sys.argv[1],sys.argv[2])
sys.exit(app.exec_())
if __name__ == '__main__':
main()
There is a dedicated class for that in Phonon: Phonon.SeekSlider:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
# creating player resource
self.player = Player()
# adding controls
self.sliders = Sliders(self.player)
class Sliders(QtGui.QWidget):
def __init__(self, player):
QtGui.QWidget.__init__(self)
self.seek_slider = Phonon.SeekSlider(player , self)
class Player(Phonon.MediaObject):
def __init__(self):
Phonon.MediaObject.__init__(self)
self.audioOutput = Phonon.AudioOutput(Phonon.MusicCategory, self)
self.path = Phonon.createPath(self, self.audioOutput)
This slider will directly pilot the play and its position will be updated during media playing. But, as that's a drawback I'm looking for a workaround, there is no valueChanged signal :-/