1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#! /usr/bin/env python3
#
# PyQt5-based video-sync example for VLC Python bindings
# Copyright (C) 2009-2010 the VideoLAN team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
#
"""
This module contains a bare-bones VLC player class to play videos.
Author: Saveliy Yusufov, Columbia University, sy2685@columbia.edu
Date: 25 January 2019
"""
import os
import sys
import queue
import platform
from PyQt5 import QtWidgets, QtGui, QtCore
import vlc
from network import Client
class MiniPlayer(QtWidgets.QMainWindow):
"""Stripped-down PyQt5-based media player class to sync with "master" video.
"""
def __init__(self, data_queue, master=None):
QtWidgets.QMainWindow.__init__(self, master)
self.setWindowTitle("Mini Player")
self.statusbar = self.statusBar()
self.statusbar.showMessage("Ready")
# Create a basic vlc instance
self.instance = vlc.Instance()
self.media = None
# Create an empty vlc media player
self.mediaplayer = self.instance.media_player_new()
self.init_ui()
self.open_file()
self.timer = QtCore.QTimer(self)
self.timer.setInterval(10)
self.timer.timeout.connect(self.update_ui)
self.data_queue = data_queue
self.timer.start()
def init_ui(self):
"""Set up the user interface
"""
self.widget = QtWidgets.QWidget(self)
self.setCentralWidget(self.widget)
# In this widget, the video will be drawn
if platform.system() == "Darwin": # for MacOS
self.videoframe = QtWidgets.QMacCocoaViewContainer(0)
else:
self.videoframe = QtWidgets.QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor(QtGui.QPalette.Window, QtGui.QColor(0, 0, 0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.vboxlayout = QtWidgets.QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.widget.setLayout(self.vboxlayout)
def open_file(self):
"""Open a media file in a MediaPlayer
"""
dialog_txt = "Choose Media File"
filename = QtWidgets.QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
if not filename[0]:
return
# getOpenFileName returns a tuple, so use only the actual file name
self.media = self.instance.media_new(filename[0])
# Put the media in the media player
self.mediaplayer.set_media(self.media)
# Parse the metadata of the file
self.media.parse()
# Set the title of the track as the window title
self.setWindowTitle("{}".format(self.media.get_meta(0)))
# The media player has to be 'connected' to the QFrame (otherwise the
# video would be displayed in it's own window). This is platform
# specific, so we must give the ID of the QFrame (or similar object) to
# vlc. Different platforms have different functions for this
if platform.system() == "Linux": # for Linux using the X Server
self.mediaplayer.set_xwindow(int(self.videoframe.winId()))
elif platform.system() == "Windows": # for Windows
self.mediaplayer.set_hwnd(int(self.videoframe.winId()))
elif platform.system() == "Darwin": # for MacOS
self.mediaplayer.set_nsobject(int(self.videoframe.winId()))
# Start playing the video as soon as it loads
self.mediaplayer.play()
def update_ui(self):
self.update_statusbar()
try:
val = self.data_queue.get(block=False)
except queue.Empty:
return
if val == '<':
self.mediaplayer.set_rate(self.mediaplayer.get_rate() * 0.5)
return
if val == '>':
self.mediaplayer.set_rate(self.mediaplayer.get_rate() * 2)
return
if val == 'P':
self.mediaplayer.play()
return
if val == 'p':
self.mediaplayer.pause()
return
if val == 'S':
self.mediaplayer.stop()
return
val = int(val)
if val != self.mediaplayer.get_time():
self.mediaplayer.set_time(val)
def update_statusbar(self):
mtime = QtCore.QTime(0, 0, 0, 0)
time = mtime.addMSecs(self.mediaplayer.get_time())
self.statusbar.showMessage(time.toString())
def main():
"""Entry point for our simple vlc player
"""
app = QtWidgets.QApplication(sys.argv)
data_queue = queue.Queue()
player = MiniPlayer(data_queue)
player.show()
player.resize(480, 480)
_ = Client("localhost", 10000, data_queue)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
|