Attachment 'mplayerwidget.py'
Download 1 #!/usr/bin/env python
2 # See http://www.mplayerhq.hu/DOCS/tech/slave.txt
3 # and mplayer -input cmdlist
4 #from qt import *
5 from PyQt4.QtCore import pyqtProperty, pyqtSignature, Qt, QSize, QTimer, SIGNAL
6 from PyQt4.QtGui import qApp, QWidget
7 import commands, popen2, atexit
8
9
10 class Process:
11
12 CMD = ("mplayer -slave -quiet -noconsolecontrols -nomouseinput "
13 "-vo %(VO)s -ao %(AO)s -wid %(WID)s %(FILENAME)r")
14
15 CFG = dict(
16 AO = "alsa",
17 VO = "xv" #VO = "x11"
18 )
19
20 def __init__(self, winId, filename = ""):
21
22 self.CFG["WID"] = winId
23 self.CFG["FILENAME"] = filename
24 self.process_in = None
25 self.process_out = None
26 self.running = False
27
28 def run(self):
29
30 if self.CFG["FILENAME"]:
31 self.process_out, self.process_in = popen2.popen2(self.CMD % self.CFG, 1)
32 self.running = True
33
34 def setFileName(self, filename):
35
36 self.CFG["FILENAME"] = filename
37
38 def __del__(self):
39
40 self.command("pause")
41 self.command("quit")
42 if self.process_in:
43 self.process_in.close()
44 self.process_in = None
45 if self.process_out:
46 self.process_out.close()
47 self.process_out = None
48
49 def command(self, cmd, args = (), result_prefix = "", result_type = None):
50
51 if not self.running:
52 if result_type:
53 return result_type()
54 else:
55 return
56
57 if args:
58 args = map(lambda arg: str(arg), args)
59 self.process_in.write("\n%s %s\n" % (cmd, " ".join(args)))
60 else:
61 self.process_in.write("\n%s\n" % cmd)
62
63 if result_type != None:
64 line = self.process_out.readline()
65 value = line.lstrip(result_prefix).rstrip()
66 try:
67 return result_type(value)
68 except ValueError:
69 return result_type()
70
71
72 class MPlayerWidget(QWidget):
73
74 __pyqtSignals__ = ("muteChanged(bool)", "playing(bool)", "paused(bool)",
75 "fullScreenChanged(bool)", "percentPosition(int)",
76 "timePosition(int)", "volumeChanged(int)")
77
78 def __init__(self, parent = None):
79
80 QWidget.__init__(self, parent)
81 self.timer = QTimer()
82 self.connect(self.timer, SIGNAL("timeout()"), self.updateTime)
83 self._process = Process(self.winId())
84 self._path = u""
85 self._playing = False
86 self._paused = False
87 self._fullscreen = False
88 self._muted = False
89 self._volume = 0
90 self._default_volume = 10
91
92 def sizeHint(self):
93 return QSize(100, 100)
94
95 def osd(self, msg):
96 self._process.command("osd_show_text", msg)
97
98 def updateTime(self):
99
100 percentage = self._process.command("get_percent_pos", (), "ANS_PERCENT_POSITION=", int)
101 self.emit(SIGNAL("percentPosition(int)"), percentage)
102 seconds = int(percentage/100.0 * self.getLength())
103 self.emit(SIGNAL("timePosition(int)"), seconds)
104
105 @pyqtSignature("load(QString)")
106 def load(self, url):
107 self.setFileName(url)
108
109 def fileName(self):
110 return self._path
111
112 def setFileName(self, url):
113
114 url = str(url)
115 self._process.setFileName(url)
116 self._path = url
117
118 fileName = pyqtProperty("QString", fileName, setFileName)
119
120 @pyqtSignature("play()")
121 def play(self):
122 if self._process.running:
123 return
124
125 self.setAttribute(Qt.WA_NoSystemBackground)
126 self._process.run()
127
128 self._playing = True
129 self.emit(SIGNAL("playing(bool)"), True)
130 self.setVolume(self._default_volume)
131 self.setPaused(False)
132 self.update()
133
134 @pyqtSignature("stop()")
135 def stop(self):
136
137 if not self._process.running:
138 return
139
140 self._playing = False
141 self.setPaused(False)
142 self.emit(SIGNAL("playing(bool)"), False)
143
144 self._process = Process(self.winId(), self._path)
145 self.setAttribute(Qt.WA_NoSystemBackground, False)
146
147 def isPlaying(self):
148 return self._playing
149
150 def setPlaying(self, enable):
151
152 if not self._process.running:
153 return
154
155 if self._playing != enable:
156 if enable:
157 self.play()
158 else:
159 self.stop()
160
161 playing = pyqtProperty("bool", isPlaying, setPlaying)
162
163 def isMuted(self):
164 return self._muted
165
166 @pyqtSignature("setMuted(bool)")
167 def setMuted(self, enable):
168
169 if not self._process.running:
170 return
171
172 if enable != self._muted:
173 self._process.command("mute")
174 self._muted = enable
175 self.emit(SIGNAL("muteChanged(bool)"), enable)
176
177 muted = pyqtProperty("bool", isMuted, setMuted)
178
179 def isPaused(self):
180 return self._paused
181
182 @pyqtSignature("setPaused(bool)")
183 def setPaused(self, enable):
184
185 if not self._process.running:
186 return
187
188 if not enable and self._playing:
189 self.timer.start(100)
190 else:
191 self.timer.stop()
192
193 if enable != self._paused:
194 self._process.command("pause")
195 self._paused = enable
196 self.emit(SIGNAL("paused(bool)"), enable)
197
198 paused = pyqtProperty("bool", isPaused, setPaused)
199
200 def isFullScreen(self):
201 return self._fullscreen
202
203 @pyqtSignature("setFullScreen(bool)")
204 def setFullScreen(self, enable):
205
206 if not self._process.running:
207 return
208
209 if enable != self._fullscreen:
210 self._process.command("vo_fullscreen")
211 self._fullscreen = enable
212 self.emit(SIGNAL("fullScreenChanged(bool)"), enable)
213
214 fullScreen = pyqtProperty("bool", isFullScreen, setFullScreen)
215
216 def getLength(self):
217
218 if not self._process.running:
219 return -1
220 else:
221 return self._process.command("get_time_length", (), "ANS_LENGTH=", int)
222
223 def getVolume(self):
224
225 return self._volume
226
227 @pyqtSignature("setVolume(int)")
228 def setVolume(self, value):
229
230 volume = max(0, min(value, 100))
231 if volume != self._volume:
232 self.emit(SIGNAL("volumeChanged(int)"), volume)
233 self._volume = volume
234 self._process.command("volume", (self._volume, 1))
235
236 volume = pyqtProperty("int", getVolume, setVolume)
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.