forked from devos50/vlc-pyqt5-example
/
Main.py
301 lines (252 loc) · 11.1 KB
/
Main.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
#! /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.
#
#https://github.com/devos50/vlc-pyqt5-example
import os.path
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QMainWindow, QWidget, QFrame, QSlider, QHBoxLayout, QPushButton, \
QVBoxLayout, QAction, QFileDialog, QApplication, QLineEdit, QListWidget
import pafy, vlc, requests, sys
from bs4 import BeautifulSoup as bs
class Player(QMainWindow):
"""A simple Media Player using VLC and Qt
"""
def __init__(self, master=None):
QMainWindow.__init__(self, master)
self.setWindowTitle("Media Player")
self.isAudio = False
# creating a basic vlc instance
self.instance = vlc.Instance()
# creating an empty vlc media player
self.mediaplayer = self.instance.media_player_new()
self.createUI()
self.isPaused = False
def createUI(self):
"""Set up the user interface, signals & slots
"""
self.widget = QWidget(self)
self.setCentralWidget(self.widget)
# In this widget, the video will be drawn
if sys.platform == "darwin": # for MacOS
from PyQt5.QtWidgets import QMacCocoaViewContainer
self.videoframe = QMacCocoaViewContainer(0)
else:
self.videoframe = QFrame()
self.palette = self.videoframe.palette()
self.palette.setColor (QPalette.Window,
QColor(0,0,0))
self.videoframe.setPalette(self.palette)
self.videoframe.setAutoFillBackground(True)
self.positionslider = QSlider(Qt.Horizontal, self)
self.positionslider.setToolTip("Position")
self.positionslider.setMaximum(1000)
self.positionslider.sliderMoved.connect(self.setPosition)
self.hbuttonbox = QHBoxLayout()
self.playbutton = QPushButton("►")
self.hbuttonbox.addWidget(self.playbutton)
self.playbutton.clicked.connect(self.PlayPause)
self.stopbutton = QPushButton("⯀")
self.hbuttonbox.addWidget(self.stopbutton)
self.stopbutton.clicked.connect(self.Stop)
self.audiobutton = QPushButton("𝅘𝅥")
self.hbuttonbox.addWidget(self.audiobutton)
self.audiobutton.setToolTip("Switch to audio only mode\n(must search again)")
self.audiobutton.clicked.connect(self.AudioVideo)
self.hbuttonbox.addStretch(1)
self.volumeslider = QSlider(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.volumeslider.valueChanged.connect(self.setVolume)
self.hbuttonbox2 = QHBoxLayout()
self.searchline = QLineEdit()
self.hbuttonbox2.addWidget(self.searchline)
self.searchline.setToolTip("Enter search term here")
self.searchbutton = QPushButton("Search")
self.hbuttonbox2.addWidget(self.searchbutton)
self.searchbutton.setToolTip("Press to search")
self.searchbutton.clicked.connect(self.searchYouTube)
self.searchresult = QHBoxLayout()
#adding QListWidget to the layout causes the video frame
#to disappear and im not sure why
#self.searchresults = QListWidget(self)
#self.searchresult.addWidget(self.searchresults)
#self.searchresults.addItem("testing1")
#self.searchresults.addItem("testing2")
self.vboxlayout = QVBoxLayout()
self.vboxlayout.addWidget(self.videoframe)
self.vboxlayout.addWidget(self.positionslider)
self.vboxlayout.addLayout(self.hbuttonbox)
self.vboxlayout.addLayout(self.hbuttonbox2)
#self.vboxlayout.addLayout(self.searchresult)
self.widget.setLayout(self.vboxlayout)
open = QAction("&Open", self)
open.triggered.connect(lambda: self.OpenFile())
exit = QAction("&Exit", self)
exit.triggered.connect(sys.exit)
download = QAction("&Download", self)
download.triggered.connect(self.downloadFile)
menubar = self.menuBar()
filemenu = menubar.addMenu("&File")
filemenu.addAction(open)
filemenu.addSeparator()
filemenu.addAction(exit)
filemenu.addSeparator()
filemenu.addAction(download)
self.timer = QTimer(self)
self.timer.setInterval(200)
self.timer.timeout.connect(self.updateUI)
def AudioVideo(self):
"""Toggle whether a video is shown or just audio.
requires another search to take effect"""
if self.isAudio == False:
self.isAudio = True
self.audiobutton.setText("🎥")
self.audiobutton.setToolTip("Switch to video only mode\n(must search again)")
else:
self.isAudio = False
self.audiobutton.setText("𝅘𝅥")
self.audiobutton.setToolTip("Switch to audio only mode\n(must search again)")
def PlayPause(self):
"""Toggle play/pause status
"""
if self.mediaplayer.is_playing():
self.mediaplayer.pause()
self.playbutton.setText("►")
self.isPaused = True
else:
if self.mediaplayer.play() == -1:
self.searchYouTube()
return
self.mediaplayer.play()
self.playbutton.setText("❚❚")
self.timer.start()
self.isPaused = False
def Stop(self):
"""Stop player
"""
self.mediaplayer.stop()
self.playbutton.setText("►")
def OpenFile(self, filename=None):
"""Open a media file in a MediaPlayer
"""
if filename is None:
filename = QFileDialog.getOpenFileName(self, "Open File", os.path.expanduser('~'))[0]
if not filename:
return
# create the media
if sys.version < '3':
filename = unicode(filename)
self.media = self.instance.media_new(filename)
# 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 window title
self.setWindowTitle(self.media.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(int(self.videoframe.winId()))
self.PlayPause()
def searchYouTube(self):
"""Search YouTube with search term and play top video"""
searchTerm = self.searchline.text()
self.searchline.clear()
searchTerm = searchTerm.replace(" ", "+") #For formatting
#request youtube search results and web scrape information
r = requests.get("https://www.youtube.com/results?search_query="+searchTerm)
page = r.text
soup = bs(page, 'html.parser')
vids = soup.findAll('a', attrs={'class':'yt-uix-tile-link'})
self.titlelist = []
self.videolist = []
#create a list with all the links and titles of youtube videos
for v in vids:
tmp = 'https://www.youtube.com' + v['href']
self.videolist.append(tmp)
self.titlelist.append(v['title'])
#currently only the top video will play
url_0 = self.videolist[0]
#use pafy to convert into a VLC playable video
self.video_0 = pafy.new(url_0)
#chooses whether to get video or audio only
if self.isAudio:
self.best = self.video_0.getbestaudio()
else:
self.best = self.video_0.getbest()
#play in VLC
playurl = self.best.url
self.media = self.instance.media_new(playurl)
self.media.get_mrl()
self.mediaplayer.set_media(self.media)
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(int(self.videoframe.winId()))
self.setWindowTitle(self.titlelist[0])
self.PlayPause()
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)
def downloadFile(self):
"""downloads the file currently playing, .mp4 for video or .webm for audio
settings can be altered but are left default for now"""
self.best.download()
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()
if __name__ == "__main__":
app = QApplication(sys.argv)
player = Player()
player.show()
player.resize(640, 480)
if sys.argv[1:]:
player.OpenFile(sys.argv[1])
sys.exit(app.exec_())