player.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #
  2. # PyQt5 example for VLC Python bindings
  3. # Copyright (C) 2009-2010 the VideoLAN team
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  18. #
  19. """
  20. A simple example for VLC python bindings using PyQt5.
  21. Author: Saveliy Yusufov, Columbia University, sy2685@columbia.edu
  22. Date: 25 December 2018
  23. """
  24. import platform
  25. import os
  26. import sys
  27. from PyQt5 import QtWidgets, QtGui, QtCore
  28. from core import constants
  29. os.environ['PYTHON_VLC_LIB_PATH'] = constants.APP_ROOT / 'core' / 'vlc-core' / 'libvlc.dll'
  30. if 1:
  31. import vlc
  32. class Player(QtWidgets.QMainWindow):
  33. """A simple Media Player using VLC and Qt
  34. """
  35. def __init__(self, master=None):
  36. QtWidgets.QMainWindow.__init__(self, master)
  37. self.setWindowTitle("Media Player")
  38. # Create a basic vlc instance
  39. self.instance = vlc.Instance()
  40. self.media = None
  41. # Create an empty vlc media player
  42. self.mediaplayer = self.instance.media_player_new()
  43. self.create_ui()
  44. self.is_paused = False
  45. def create_ui(self):
  46. """Set up the user interface, signals & slots
  47. """
  48. self.widget = QtWidgets.QWidget(self)
  49. self.setCentralWidget(self.widget)
  50. # In this widget, the video will be drawn
  51. if platform.system() == "Darwin": # for MacOS
  52. self.videoframe = QtWidgets.QMacCocoaViewContainer(0)
  53. else:
  54. self.videoframe = QtWidgets.QFrame()
  55. self.palette = self.videoframe.palette()
  56. self.palette.setColor(QtGui.QPalette.Window, QtGui.QColor(0, 0, 0))
  57. self.videoframe.setPalette(self.palette)
  58. self.videoframe.setAutoFillBackground(True)
  59. self.positionslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
  60. self.positionslider.setToolTip("Position")
  61. self.positionslider.setMaximum(1000)
  62. self.positionslider.sliderMoved.connect(self.set_position)
  63. self.positionslider.sliderPressed.connect(self.set_position)
  64. self.hbuttonbox = QtWidgets.QHBoxLayout()
  65. self.playbutton = QtWidgets.QPushButton("Play")
  66. self.hbuttonbox.addWidget(self.playbutton)
  67. self.playbutton.clicked.connect(self.play_pause)
  68. self.stopbutton = QtWidgets.QPushButton("Stop")
  69. self.hbuttonbox.addWidget(self.stopbutton)
  70. self.stopbutton.clicked.connect(self.stop)
  71. self.hbuttonbox.addStretch(1)
  72. self.volumeslider = QtWidgets.QSlider(QtCore.Qt.Horizontal, self)
  73. self.volumeslider.setMaximum(100)
  74. self.volumeslider.setValue(self.mediaplayer.audio_get_volume())
  75. self.volumeslider.setToolTip("Volume")
  76. self.hbuttonbox.addWidget(self.volumeslider)
  77. self.volumeslider.valueChanged.connect(self.set_volume)
  78. self.vboxlayout = QtWidgets.QVBoxLayout()
  79. self.vboxlayout.addWidget(self.videoframe)
  80. self.vboxlayout.addWidget(self.positionslider)
  81. self.vboxlayout.addLayout(self.hbuttonbox)
  82. self.widget.setLayout(self.vboxlayout)
  83. menu_bar = self.menuBar()
  84. # File menu
  85. file_menu = menu_bar.addMenu("File")
  86. # Add actions to file menu
  87. open_action = QtWidgets.QAction("Load Video", self)
  88. close_action = QtWidgets.QAction("Close App", self)
  89. file_menu.addAction(open_action)
  90. file_menu.addAction(close_action)
  91. open_action.triggered.connect(self.open_file)
  92. close_action.triggered.connect(sys.exit)
  93. self.timer = QtCore.QTimer(self)
  94. self.timer.setInterval(100)
  95. self.timer.timeout.connect(self.update_ui)
  96. def play_pause(self):
  97. """Toggle play/pause status
  98. """
  99. if self.mediaplayer.is_playing():
  100. self.mediaplayer.pause()
  101. self.playbutton.setText("Play")
  102. self.is_paused = True
  103. self.timer.stop()
  104. else:
  105. if self.mediaplayer.play() == -1:
  106. self.open_file()
  107. return
  108. self.mediaplayer.play()
  109. self.playbutton.setText("Pause")
  110. self.timer.start()
  111. self.is_paused = False
  112. def stop(self):
  113. """Stop player
  114. """
  115. self.mediaplayer.stop()
  116. self.playbutton.setText("Play")
  117. def open_file(self):
  118. """Open a media file in a MediaPlayer
  119. """
  120. dialog_txt = "Choose Media File"
  121. filename = QtWidgets.QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
  122. if not filename:
  123. return
  124. # getOpenFileName returns a tuple, so use only the actual file name
  125. self.media = self.instance.media_new(filename[0])
  126. # Put the media in the media player
  127. self.mediaplayer.set_media(self.media)
  128. # Parse the metadata of the file
  129. self.media.parse()
  130. # Set the title of the track as window title
  131. self.setWindowTitle(self.media.get_meta(0))
  132. # The media player has to be 'connected' to the QFrame (otherwise the
  133. # video would be displayed in it's own window). This is platform
  134. # specific, so we must give the ID of the QFrame (or similar object) to
  135. # vlc. Different platforms have different functions for this
  136. if platform.system() == "Linux": # for Linux using the X Server
  137. self.mediaplayer.set_xwindow(int(self.videoframe.winId()))
  138. elif platform.system() == "Windows": # for Windows
  139. self.mediaplayer.set_hwnd(int(self.videoframe.winId()))
  140. elif platform.system() == "Darwin": # for MacOS
  141. self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
  142. self.play_pause()
  143. def set_volume(self, volume):
  144. """Set the volume
  145. """
  146. self.mediaplayer.audio_set_volume(volume)
  147. def set_position(self):
  148. """Set the movie position according to the position slider.
  149. """
  150. # The vlc MediaPlayer needs a float value between 0 and 1, Qt uses
  151. # integer variables, so you need a factor; the higher the factor, the
  152. # more precise are the results (1000 should suffice).
  153. # Set the media position to where the slider was dragged
  154. self.timer.stop()
  155. pos = self.positionslider.value()
  156. self.mediaplayer.set_position(pos / 1000.0)
  157. self.timer.start()
  158. def update_ui(self):
  159. """Updates the user interface"""
  160. # Set the slider's position to its corresponding media position
  161. # Note that the setValue function only takes values of type int,
  162. # so we must first convert the corresponding media position.
  163. media_pos = int(self.mediaplayer.get_position() * 1000)
  164. self.positionslider.setValue(media_pos)
  165. # No need to call this function if nothing is played
  166. if not self.mediaplayer.is_playing():
  167. self.timer.stop()
  168. # After the video finished, the play button stills shows "Pause",
  169. # which is not the desired behavior of a media player.
  170. # This fixes that "bug".
  171. if not self.is_paused:
  172. self.stop()
  173. def main():
  174. """Entry point for our simple vlc player
  175. """
  176. app = QtWidgets.QApplication(sys.argv)
  177. player = Player()
  178. player.show()
  179. player.resize(640, 480)
  180. sys.exit(app.exec_())
  181. if __name__ == "__main__":
  182. main()