vlc_.py 7.8 KB

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