-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmini_player.py
169 lines (131 loc) · 5.92 KB
/
mini_player.py
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
#! /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, [email protected]
Date: 25 January 2019
"""
import os
import sys
import platform
from PyQt5 import QtWidgets, QtGui, QtCore
import vlc
class MiniPlayer(QtWidgets.QMainWindow):
"""Stripped-down PyQt5-based media player class to sync with "master" video.
"""
def __init__(self, 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.video_media = None
self.audio_media = None
# Create an empty vlc media player for video
self.video_mediaplayer = self.instance.media_player_new()
# Create an empty vlc media player for audio
self.audio_mediaplayer = self.instance.media_player_new()
self.init_ui()
self.open_files()
self.timer = QtCore.QTimer(self)
self.timer.setInterval(10)
self.timer.timeout.connect(self.update_ui)
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)
self.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint) #플레이어윈도우 맨위로
def open_files(self):
"""Open media files in media players
"""
dialog_txt = "Choose Video File"
filename_video = QtWidgets.QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
if not filename_video[0]:
return
dialog_txt = "Choose Audio File"
filename_audio = QtWidgets.QFileDialog.getOpenFileName(self, dialog_txt, os.path.expanduser('~'))
if not filename_audio[0]:
return
# getOpenFileName returns a tuple, so use only the actual file name
self.video_media = self.instance.media_new(filename_video[0])
self.audio_media = self.instance.media_new(filename_audio[0])
# Put the media in the media players
self.video_mediaplayer.set_media(self.video_media)
self.audio_mediaplayer.set_media(self.audio_media)
# Parse the metadata of the files
self.video_media.parse()
self.audio_media.parse()
# Set the title of the track as the window title
self.setWindowTitle("{}".format(self.video_media.get_meta(0)))
# The media players have to be 'connected' to the respective QFrames
# This is platform specific, so we must give the IDs of the QFrames (or similar objects) to vlc
if platform.system() == "Linux": # for Linux using the X Server
self.video_mediaplayer.set_xwindow(int(self.videoframe.winId()))
self.audio_mediaplayer.set_xwindow(int(self.videoframe.winId()))
elif platform.system() == "Windows": # for Windows
self.video_mediaplayer.set_hwnd(int(self.videoframe.winId()))
self.audio_mediaplayer.set_hwnd(int(self.videoframe.winId()))
elif platform.system() == "Darwin": # for MacOS
self.video_mediaplayer.set_nsobject(int(self.videoframe.winId()))
self.audio_mediaplayer.set_nsobject(int(self.videoframe.winId()))
# Start playing the video and audio as soon as they load
self.video_mediaplayer.play()
self.audio_mediaplayer.play()
def update_ui(self):
self.update_statusbar()
if self.video_mediaplayer.get_state() == vlc.State.Ended:
self.video_mediaplayer.stop()
self.video_mediaplayer.play()
if self.audio_mediaplayer.get_state() == vlc.State.Ended:
self.audio_mediaplayer.stop()
self.audio_mediaplayer.play()
def update_statusbar(self):
mtime = QtCore.QTime(0, 0, 0, 0)
time_video = mtime.addMSecs(self.video_mediaplayer.get_time())
time_audio = mtime.addMSecs(self.audio_mediaplayer.get_time())
if self.video_mediaplayer.is_playing():
self.statusbar.showMessage("Video: " + time_video.toString())
elif self.audio_mediaplayer.is_playing():
self.statusbar.showMessage("Audio: " + time_audio.toString())
def main():
"""Entry point for our simple vlc player
"""
app = QtWidgets.QApplication(sys.argv)
player = MiniPlayer()
player.show()
player.resize(480, 480)
sys.exit(app.exec_())
if __name__ == "__main__":
main()