]> code.delx.au - bluplayer/blob - bluplayer.py
build-makemkv.sh supports v1.8.7
[bluplayer] / bluplayer.py
1 #!/usr/bin/python
2
3 import csv
4 import errno
5 import logging
6 import os
7 import subprocess
8 import sys
9 import urllib
10
11 from lxml import etree
12 from PyQt4.QtCore import *
13 from PyQt4.QtGui import *
14
15
16 XHTML = "http://www.w3.org/1999/xhtml"
17 NS = {
18 "xhtml": XHTML,
19 }
20 DEFAULT_PATH = "/usr/local/bin:/usr/bin:/bin"
21
22
23 def set_env_config(name, value):
24 value = os.environ.get(name, value)
25 globals()[name] = value
26
27 def find_path(binary):
28 value = ""
29 paths = os.environ.get("PATH", DEFAULT_PATH).split(":")
30 for path in paths:
31 path = os.path.join(path, binary)
32 if os.path.isfile(path):
33 value = path
34 break
35 return value
36
37 set_env_config("MAKEMKVCON_PATH", find_path("makemkvcon"))
38 set_env_config("MPLAYER_PATH", find_path("mplayer"))
39 set_env_config("MPLAYER_OPTS", None)
40
41
42 def grab_xml(url):
43 f = urllib.urlopen(url)
44 doc = etree.parse(f)
45 f.close()
46 return doc
47
48 def parse_doc(doc):
49 d = {}
50 tds = doc.xpath("//xhtml:td", namespaces=NS)
51 i = 0
52 while i < len(tds)-1:
53 key = tds[i].text
54 value = tds[i+1]
55 for v in value:
56 if v.tag == "{%s}a" % XHTML:
57 value = v.get("href")
58 break
59 else:
60 value = value.text
61
62 d[key] = value
63 i += 2
64 return d
65
66 def get_num_cpus():
67 logging.info("Determing number of CPUs")
68 try:
69 return subprocess.check_output("nproc").strip()
70 except Exception, e:
71 logging.warn("Unable to run nproc: %s", fmt_exc(e))
72 return 1
73
74 def grab_dict(url):
75 doc = grab_xml(url)
76 d = parse_doc(doc)
77 return d
78
79 def fmt_exc(e):
80 return "%s: %s" % (e.__class__.__name__, str(e))
81
82
83 class Title(object):
84 def __init__(self, title_page):
85 self.id = title_page["id"]
86 self.duration = title_page["duration"]
87 self.video_url = title_page["file0"]
88
89 def __str__(self):
90 return "Title%s: %s %s" % (self.id, self.duration, self.video_url)
91
92
93 class MakeMkvCon(object):
94 def __init__(self, params):
95 cmd = [MAKEMKVCON_PATH, "--robot"]
96 cmd.extend(params)
97 logging.info("Running makemkvcon: %s", params)
98 self.p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
99 self.fd = self.p.stdout.fileno()
100 self.buf = ""
101 self.finished = False
102 self.status = None
103
104 def decode_line(self, line):
105 logging.debug("makemkvcon output: %s", line)
106 data = csv.reader([line]).next()
107 key, x0 = data[0].split(":", 1)
108 data[0] = x0
109 return key, data
110
111 def __iter__(self):
112 return self
113
114 def next(self):
115 if self.finished:
116 raise StopIteration()
117 while True:
118 pos = self.buf.find("\n")
119 if pos >= 0:
120 result = self.buf[:pos]
121 self.buf = self.buf[pos+1:]
122 return self.decode_line(result)
123
124 try:
125 data = os.read(self.p.stdout.fileno(), 4096)
126 except OSError, e:
127 if e.errno == errno.EINTR:
128 continue
129 if not data:
130 self.status = self.p.wait()
131 logging.info("makemkvcon exited with status %s", self.status)
132 self.finished = True
133 raise StopIteration()
134
135 self.buf += data
136
137
138 class MPlayer(QObject):
139 play_finished = pyqtSignal()
140 fatal_error = pyqtSignal(str, str)
141
142 def run(self):
143 logging.info("mplayer thread started")
144
145 if not os.path.isfile(MPLAYER_PATH):
146 self.fatal_error.emit(
147 "MPlayer was not found.",
148 "Please install MPlayer. If you already have done so you " +
149 "may set the MPLAYER_PATH environment variable to the " +
150 "absolute path to the mplayer executable."
151 )
152 return
153
154 if MPLAYER_OPTS:
155 self.opts = MPLAYER_OPTS.split()
156 else:
157 self.opts = [
158 "-fs",
159 "-nocache",
160 "-lavdopts", "threads=%s" % get_num_cpus(),
161 "-volume", "100",
162 ]
163
164 def play(self, video_url):
165 video_url = str(video_url)
166 logging.info("Running mplayer: %s", video_url)
167 try:
168 subprocess.check_call([MPLAYER_PATH] + self.opts + [video_url])
169 except Exception, e:
170 self.fatal_error.emit("MPlayer failed to play the video.", fmt_exc(e))
171 finally:
172 self.play_finished.emit()
173
174
175 class MakeMkv(QObject):
176 title_loaded = pyqtSignal(Title)
177 title_load_complete = pyqtSignal()
178 status = pyqtSignal(str)
179 fatal_error = pyqtSignal(str, str)
180
181 def find_disc(self):
182 self.url = "http://192.168.1.114:51001/"
183 makemkvcon = MakeMkvCon(["info", "disc:9999"])
184 disc_number = None
185 for key, line in makemkvcon:
186 if key == "MSG" and line[0] != "5010":
187 self.status.emit(line[3])
188
189 if disc_number is None and key == "DRV" and line[5]:
190 disc_number = line[0]
191 disc_name = line[5]
192 self.status.emit("Found disc %s" % disc_name)
193
194 if makemkvcon.status == 0:
195 return disc_number
196
197 def run_stream(self, disc_number):
198 makemkvcon = MakeMkvCon(["stream", "disc:%s" % disc_number])
199 for key, line in makemkvcon:
200 if key == "MSG" and line[0] == "4500":
201 # Sometimes the port in field 6 is wrong
202 port = line[5].split(":")[1]
203 url = "http://localhost:%s/" % port
204 self.load_titles(url)
205 elif key == "MSG":
206 self.status.emit(line[3])
207
208 if makemkvcon.status != 0:
209 self.fatal_error.emit(
210 "MakeMKV quit unexpectedly.",
211 "makemkvcon exited with code %s" % makemkvcon.status
212 )
213
214 def load_titles(self, url):
215 home_page = grab_dict(url)
216 title_list_page = grab_dict(home_page["titles"])
217 title_count = int(title_list_page["titlecount"])
218 for i in xrange(title_count):
219 title_page = grab_dict(title_list_page["title%d" % i])
220 title = Title(title_page)
221 self.title_loaded.emit(title)
222 self.title_load_complete.emit()
223
224 def run(self):
225 logging.info("makemkv thread started")
226
227 if not os.path.isfile(MAKEMKVCON_PATH):
228 self.fatal_error.emit(
229 "MakeMKV was not found.",
230 "Please install MakeMKV. If you already have done so you " +
231 "may set the MAKEMKVCON_PATH environment variable to the " +
232 "absolute path to the makemkvcon executable."
233 )
234 return
235
236 try:
237 disc_number = self.find_disc()
238 except Exception, e:
239 self.fatal_error.emit("Error searching for disc.", fmt_exc(e))
240 raise
241
242 if not disc_number:
243 self.fatal_error.emit(
244 "No disc found.",
245 "Please insert a BluRay disc and try again."
246 )
247 return
248
249 try:
250 self.run_stream(disc_number)
251 except Exception, e:
252 self.fatal_error.emit("Failed to start MakeMKV.", fmt_exc(e))
253 raise
254
255 logging.info("makemkv thread finished")
256
257
258 class PlayerWindow(QWidget):
259 fatal_error = pyqtSignal(str, str)
260 video_selected = pyqtSignal(str)
261
262 def __init__(self):
263 QWidget.__init__(self)
264
265 self.title_map = {}
266 self.is_playing = False
267
268 self.list_widget = QListWidget(self)
269 self.list_widget.itemActivated.connect(self.handle_activated)
270 self.list_widget.setEnabled(False)
271
272 self.log = QTextEdit(self)
273 self.log.setReadOnly(True)
274
275 self.splitter = QSplitter(Qt.Vertical, self)
276 self.splitter.addWidget(self.list_widget)
277 self.splitter.addWidget(self.log)
278 self.splitter.setSizes([900, 200])
279
280 self.layout = QVBoxLayout(self)
281 self.layout.addWidget(self.splitter)
282 self.setWindowTitle("BluPlayer")
283 self.resize(1200, 700)
284
285 def add_title(self, title):
286 name = "Title %s (%s)" % (int(title.id)+1, title.duration)
287 self.list_widget.addItem(name)
288 self.title_map[name] = title
289
290 def select_longest_title(self):
291 longest_title = None
292 longest_item = None
293 for i in xrange(self.list_widget.count()):
294 item = self.list_widget.item(i)
295 name = str(item.text())
296 title = self.title_map[name]
297 if longest_title is None or title.duration > longest_title.duration:
298 longest_title = title
299 longest_item = item
300 self.list_widget.setCurrentItem(longest_item)
301 self.list_widget.setEnabled(True)
302 self.list_widget.setFocus()
303
304 def add_log_entry(self, text):
305 self.log.append(text)
306
307 def handle_activated(self, item):
308 if self.is_playing:
309 return
310 name = str(item.text())
311 title = self.title_map[name]
312 self.is_playing = True
313 self.list_widget.setEnabled(False)
314 self.video_selected.emit(title.video_url)
315
316 def set_play_finished(self):
317 self.is_playing = False
318 self.list_widget.setEnabled(True)
319 self.list_widget.setFocus()
320
321 def keyPressEvent(self, e):
322 if e.key() in (Qt.Key_Escape, Qt.Key_Backspace):
323 self.close()
324 return
325 QWidget.keyPressEvent(self, e)
326
327 class LoadingDialog(QProgressDialog):
328 def __init__(self, parent):
329 QProgressDialog.__init__(self, parent)
330 self.setWindowModality(Qt.WindowModal);
331 self.setWindowTitle("Loading disc")
332 self.setLabelText("Loading BluRay disc. Please wait...")
333 self.setCancelButtonText("Exit")
334 self.setMinimum(0)
335 self.setMaximum(0)
336
337 class ErrorDialog(QMessageBox):
338 fatal_error = pyqtSignal(str, str)
339
340 def __init__(self, parent):
341 QMessageBox.__init__(self, parent)
342 self.setStandardButtons(QMessageBox.Ok)
343 self.setDefaultButton(QMessageBox.Ok)
344 self.fatal_error.connect(self.configure_popup)
345 self.setWindowTitle("Fatal error")
346 self.has_run = False
347
348 def configure_popup(self, text, detail):
349 if self.has_run:
350 return
351 self.has_run = True
352 self.setText(text)
353 self.setInformativeText(detail)
354 QTimer.singleShot(0, self.show_and_exit)
355
356 def show_and_exit(self):
357 logging.info("showing and exiting")
358 self.exec_()
359 qApp.quit()
360
361 def killall_makemkvcon():
362 logging.info("Stopping any makemkvcon processes")
363 subprocess.Popen(["killall", "--quiet", "makemkvcon"]).wait()
364
365 def main():
366 logging.basicConfig(format="%(levelname)s: %(message)s")
367 logging.getLogger().setLevel(logging.DEBUG)
368 logging.info("Configuring application")
369
370 app = QApplication(sys.argv)
371 app.setQuitOnLastWindowClosed(True)
372
373 default_font = app.font()
374 default_font.setPointSize(16)
375 app.setFont(default_font)
376
377 player_window = PlayerWindow()
378 player_window.show()
379
380 loading_dialog = LoadingDialog(player_window)
381 loading_dialog.show()
382
383 error_dialog = ErrorDialog(player_window)
384
385 makemkv = MakeMkv()
386 makemkv_thread = QThread()
387 makemkv.moveToThread(makemkv_thread)
388 makemkv_thread.started.connect(makemkv.run)
389
390 mplayer = MPlayer()
391 mplayer_thread = QThread()
392 mplayer.moveToThread(mplayer_thread)
393 mplayer_thread.started.connect(mplayer.run)
394
395 makemkv.title_loaded.connect(player_window.add_title)
396 makemkv.title_load_complete.connect(player_window.select_longest_title)
397 makemkv.status.connect(player_window.add_log_entry)
398 makemkv.fatal_error.connect(error_dialog.fatal_error)
399 makemkv.title_load_complete.connect(loading_dialog.reset)
400
401 player_window.video_selected.connect(mplayer.play)
402 mplayer.play_finished.connect(player_window.set_play_finished)
403 mplayer.fatal_error.connect(error_dialog.fatal_error)
404
405 player_window.fatal_error.connect(error_dialog.fatal_error)
406 error_dialog.fatal_error.connect(loading_dialog.reset)
407 loading_dialog.canceled.connect(qApp.quit)
408
409 logging.info("Starting application")
410 makemkv_thread.start()
411 mplayer_thread.start()
412 result = app.exec_()
413
414 logging.info("Shutting down")
415 makemkv_thread.quit()
416 mplayer_thread.quit()
417 killall_makemkvcon()
418 logging.info("Waiting for makemkv thread")
419 makemkv_thread.wait(2000)
420 logging.info("Waiting for mplayer thread")
421 mplayer_thread.wait(2000)
422 logging.info("Exiting...")
423 sys.exit(result)
424
425 if __name__ == "__main__":
426 main()
427