-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtvlc.py
346 lines (311 loc) · 12.3 KB
/
qtvlc.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#! /usr/bin/python
#
# Qt 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.
#
import sys
import os.path
import vlc
import time
import threading
import choose_mode
import SmartSub
import share_memory
import mmap
from PyQt4 import QtGui, QtCore
#全局变量
mylist = []
t = None
class Player(QtGui.QMainWindow):
"""A simple Media Player using VLC and Qt
"""
def __init__(self, master=None):
QtGui.QMainWindow.__init__(self, master)
self.setWindowTitle("Media Player")
self.clicknum = 0
# creating a basic vlc instance
self.instance = vlc.Instance(("--freetype-font=Microsoft JhengHei", "--subsdec-encoding=GB18030"))
self.player = vlc.MediaPlayer()
# creating an empty vlc media player
self.mediaplayer = self.instance.media_player_new()
self.path = [0,0]
self.createUI()
self.isPaused = False
#创建播放器UI,pyqt语句
def createUI(self):
"""Set up the user interface, signals & slots
"""
self.widget = QtGui.QWidget(self)
self.setCentralWidget(self.widget)
# In this widget, the video will be drawn
if sys.platform == "darwin": # for MacOS
self.videoframe = QtGui.QMacCocoaViewContainer(0)
else:
self.videoframe = QtGui.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.positionslider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
self.positionslider.setToolTip("Position")
self.positionslider.setMaximum(1000)
self.connect(self.positionslider,
QtCore.SIGNAL("sliderMoved(int)"), self.setPosition)
self.hbuttonbox = QtGui.QHBoxLayout()
self.playbutton = QtGui.QPushButton("Play")
self.hbuttonbox.addWidget(self.playbutton)
self.connect(self.playbutton, QtCore.SIGNAL("clicked()"),
self.PlayPause)
self.stopbutton = QtGui.QPushButton("Stop")
self.hbuttonbox.addWidget(self.stopbutton)
self.connect(self.stopbutton, QtCore.SIGNAL("clicked()"),
self.Stop)
#self.subbutton = QtGui.QPushButton("FS")
#self.hbuttonbox.addWidget(self.subbutton)
#self.connect(self.subbutton, QtCore.SIGNAL("clicked()"),
# self.full_screen)
self.hbuttonbox.addStretch(1)
self.volumeslider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
self.volumeslider.setMaximum(100)
self.volumeslider.setValue(self.mediaplayer.audio_get_volume())
self.volumeslider.setToolTip("Volume")
self.hbuttonbox.addWidget(self.volumeslider)
self.connect(self.volumeslider,
QtCore.SIGNAL("valueChanged(int)"),
self.setVolume)
self.vboxlayout = QtGui.QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.vboxlayout.addWidget(self.positionslider)
self.vboxlayout.addLayout(self.hbuttonbox)
self.widget.setLayout(self.vboxlayout)
#菜单栏
open = QtGui.QAction("&Open", self)
open_sub = QtGui.QAction("&Subtitle",self)
close_sub = QtGui.QAction("&Closesub", self)
self.connect(open, QtCore.SIGNAL("triggered()"), self.OpenFile)
self.connect(open_sub, QtCore.SIGNAL("triggered()"), self.OpenSub)
self.connect(close_sub, QtCore.SIGNAL("triggered()"), self.CloseSub)
exit = QtGui.QAction("&Exit", self)
self.connect(exit, QtCore.SIGNAL("triggered()"), sys.exit)
menubar = self.menuBar()
filemenu = menubar.addMenu("&File")
filemenu.addAction(open)
filemenu.addAction(open_sub)
filemenu.addAction(close_sub)
filemenu.addSeparator()
filemenu.addAction(exit)
self.timer = QtCore.QTimer(self)
self.timer.setInterval(200)
self.connect(self.timer, QtCore.SIGNAL("timeout()"),
self.updateUI)
#播放暂停功能
def PlayPause(self):
"""Toggle play/pause status
"""
if not self.playbutton.isEnabled():
self.playbutton.setDisabled(False)
if self.mediaplayer.is_playing():
self.mediaplayer.pause()
self.playbutton.setText("Play")
self.isPaused = True
else:
if self.mediaplayer.play() == -1:
self.OpenFile()
return
self.mediaplayer.play()
self.playbutton.setText("Pause")
self.timer.start()
self.isPaused = False
#停止播放功能
def Stop(self):
"""Stop player
"""
self.mediaplayer.stop()
self.playbutton.setText("Play")
self.tp.cancel()
#全屏功能
#def full_screen(self):
"""
Full Screen
:return:
"""
#self.mediaplayer.set_fullscreen(1)
#可增加的广告功能
'''def adPlay(self):
adname = 'C:\\Users\\Tim Zhou\\Desktop\\2.mp4'
self.ad = self.instance.media_new(adname)
# put the media in the media player
self.mediaplayer.set_media(self.media)
# parse the metadata of the file
self.ad.parse()
# set the title of the track as window title
self.setWindowTitle(self.ad.get_meta(0))
# the media player has to be 'connected' to the QFrame
# (otherwise a video would be displayed in it's own window)
# this is platform specific!
# you have to give the id of the QFrame (or similar object) to
# vlc, different platforms have different functions for this
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())
self.PlayPause()'''
#打开视频文件
def OpenFile(self, filename=None):
"""Open a media file in a MediaPlayer
"""
self.clicknum += 1
if filename is None:
filename = QtGui.QFileDialog.getOpenFileName(self,"Open File", os.path.expanduser('~'))
if not filename:
return
# create the media
if sys.version < '3':
filename = unicode(filename)
self.path[0] = filename
share_memory.get_mmap_info(self.path[0])
global mylist
global t
self.ex = choose_mode.Example()
if self.ex.exec_():
self.ex.show()
mylist = self.ex.list
t = self.ex.t
print('************',t,'*******')
self.media = self.instance.media_new(filename)
# put the media in the media player
self.mediaplayer.set_media(self.media)
#self.mediaplayer.video_set_subtitle_file('D:/2_en.srt')
# parse the metadata of the file
self.media.parse()
# set the title of the track as window title
self.setWindowTitle(self.media.get_meta(0))
#self.mediaplayer.video_set_subtitle_file('D:/1_en.srt')
# the media player has to be 'connected' to the QFrame
# (otherwise a video would be displayed in it's own window)
# this is platform specific!
# you have to give the id of the QFrame (or similar object) to
# vlc, different platforms have different functions for this
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())
self.playbutton.setDisabled(True)
self.tp = threading.Timer(30,self.PlayPause)
self.tp.start()
self.Sub()
#打开自己的字幕文件
def OpenSub(self, subname=None):
"""Open a subtitle file in a MediaPlayer
"""
#self.PlayPause()
if subname is None:
subname = QtGui.QFileDialog.getOpenFileName(self, "Open Sub", os.path.expanduser('~'))
if not subname:
return
# add subtitle
if sys.version < '3':
subname = unicode(subname)
self.sub = subname.replace('/','\\')
self.path[1] = self.sub
self.mediaplayer.video_set_subtitle_file(self.sub)
# the media player has to be 'connected' to the QFrame
# (otherwise a video would be displayed in it's own window)
# this is platform specific!
# you have to give the id of the QFrame (or similar object) to
# vlc, different platforms have different functions for this
'''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())'''
#读取字幕函数
def Sub(self):
timer_start(self.mediaplayer)
#关闭字幕
def CloseSub(self):
self.mediaplayer.video_set_spu(-1)
print(self.mediaplayer.video_set_spu(-1))
#调节音量
def setVolume(self, Volume):
"""Set the volume
"""
self.mediaplayer.audio_set_volume(Volume)
#调节播放位置
def setPosition(self, position):
"""Set the position
"""
# setting the position to where the slider was dragged
self.mediaplayer.set_position(position / 1000.0)
# the vlc MediaPlayer needs a float value between 0 and 1, Qt
# uses integer variables, so you need a factor; the higher the
# factor, the more precise are the results
# (1000 should be enough)
#刷新UI
def updateUI(self):
"""updates the user interface"""
# setting the slider to the desired position
self.positionslider.setValue(self.mediaplayer.get_position() * 1000)
if not self.mediaplayer.is_playing():
# no need to call this function if nothing is played
self.timer.stop()
if not self.isPaused:
# after the video finished, the play button stills shows
# "Pause", not the desired behavior of a media player
# this will fix it
self.Stop()
#开启线程读取字幕
def timer_start(mediaplayer):
t = threading.Thread(target= Sub,args= (mediaplayer,))
t.start()
#读取字幕
def Sub(player):
global mylist
f = share_memory.read_mmap_info()
file = os.path.basename(f)
pwd = os.path.dirname(f)
temp = file.split('.')
#mylist =
tmp = pwd.replace('/','\\')
if mylist[1] == 0:
pwd = pwd = tmp + '\\' + temp[0] + '.srt'
elif mylist[1] == 2:
pwd = tmp + '\\' + temp[0] + '_'+ mylist[2] +'.srt'
print("hahah: "+ pwd)
while True:
vlc.libvlc_video_set_subtitle_file(player, bytes(pwd, 'utf-8'))
time.sleep(5)
def main():
global t
app = QtGui.QApplication(sys.argv)
player = Player()
player.show()
player.resize(640, 480)
if sys.argv[1:]:
player.OpenFile(sys.argv[1])
player.OpenSub(sys.argv[1])
if app.exec_() == 0:
player.Stop()
sys.exit(0)
if __name__ == '__main__':
main()