]>
code.delx.au - bluplayer/blob - bluplayer.py
11 from lxml
import etree
12 from PyQt4
.QtCore
import *
13 from PyQt4
.QtGui
import *
16 XHTML
= "http://www.w3.org/1999/xhtml"
21 MAKEMKV_DIR
= os
.environ
.get("MAKEMKV_DIR", os
.path
.expanduser("~/.makemkv_install"))
22 MAKEMKVCON_PATH
= os
.environ
.get("MAKEMKVCON_PATH", os
.path
.join(MAKEMKV_DIR
, "current", "makemkvcon"))
23 MPLAYER_PATH
= os
.environ
.get("MPLAYER_PATH", "mplayer")
27 f
= urllib
.urlopen(url
)
34 tds
= doc
.xpath("//xhtml:td", namespaces
=NS
)
40 if v
.tag
== "{%s}a" % XHTML
:
55 def format_exception(msg
, e
=None):
56 msg
= "%s\n%s: %s" % (msg
, e
.__class
__.__name
__, str(e
))
62 def __init__(self
, title_page
):
63 self
.id = title_page
["id"]
64 self
.duration
= title_page
["duration"]
65 self
.video_url
= title_page
["file0"]
68 return "Title%s: %s %s" % (self
.id, self
.duration
, self
.video_url
)
74 "-lavdopts", "threads=%s" % subprocess
.check_output("nproc").strip(),
78 logging
.info("Running mplayer: %s", self
.video_url
)
79 subprocess
.check_call(cmd
)
82 class MakeMkvCon(object):
83 def __init__(self
, params
):
84 cmd
= [MAKEMKVCON_PATH
, "--robot"]
86 logging
.info("Running makemkvcon: %s", params
)
87 self
.p
= subprocess
.Popen(cmd
, stdout
=subprocess
.PIPE
)
88 self
.fd
= self
.p
.stdout
.fileno()
93 def decode_line(self
, line
):
94 logging
.debug("makemkvcon output: %s", line
)
95 data
= csv
.reader([line
]).next()
96 key
, x0
= data
[0].split(":", 1)
105 raise StopIteration()
107 pos
= self
.buf
.find("\n")
109 result
= self
.buf
[:pos
]
110 self
.buf
= self
.buf
[pos
+1:]
111 return self
.decode_line(result
)
114 data
= os
.read(self
.p
.stdout
.fileno(), 4096)
116 if e
.errno
== errno
.EINTR
:
119 self
.status
= self
.p
.wait()
120 logging
.info("makemkvcon exited with status %s", self
.status
)
122 raise StopIteration()
127 class MakeMkv(QObject
):
128 title_loaded
= pyqtSignal(Title
)
129 title_load_complete
= pyqtSignal()
130 status
= pyqtSignal(str)
131 fatal_error
= pyqtSignal(str)
134 raise NotImplementedError("auto-install not implemented")
137 self
.url
= "http://192.168.1.114:51001/"
138 makemkvcon
= MakeMkvCon(["info", "disc:9999"])
140 for key
, line
in makemkvcon
:
141 if key
== "MSG" and line
[0] != "5010":
142 self
.status
.emit(line
[3])
144 if disc_number
is None and key
== "DRV" and line
[5]:
145 disc_number
= line
[0]
147 self
.status
.emit("Found disc %s" % disc_name
)
149 if makemkvcon
.status
== 0:
152 def run_stream(self
, disc_number
):
153 makemkvcon
= MakeMkvCon(["stream", "disc:%s" % disc_number
])
154 for key
, line
in makemkvcon
:
155 if key
== "MSG" and line
[0] == "4500":
156 # Sometimes the port in field 6 is wrong
157 port
= line
[5].split(":")[1]
158 url
= "http://localhost:%s/" % port
159 self
.load_titles(url
)
161 self
.status
.emit(line
[3])
163 if makemkvcon
.status
!= 0:
164 self
.fatal_error
.emit("MakeMKV exited with error status: %s" % makemkvcon
.status
)
166 def load_titles(self
, url
):
167 home_page
= grab_dict(url
)
168 title_list_page
= grab_dict(home_page
["titles"])
169 title_count
= int(title_list_page
["titlecount"])
170 for i
in xrange(title_count
):
171 title_page
= grab_dict(title_list_page
["title%d" % i
])
172 title
= Title(title_page
)
173 self
.title_loaded
.emit(title
)
174 self
.title_load_complete
.emit()
177 logging
.info("MakeMKV thread started")
179 if not os
.path
.isfile(MAKEMKVCON_PATH
):
183 self
.fatal_error
.emit(format_exception("Failed to install MakeMKV", e
))
187 disc_number
= self
.find_disc()
189 self
.fatal_error
.emit(format_exception("Error searching for disc", e
))
193 self
.fatal_error
.emit("No disc found, please insert a disc and try again.")
197 self
.run_stream(disc_number
)
199 self
.fatal_error
.emit(format_exception("Failed to start MakeMKV", e
))
202 logging
.info("MakeMKV thread finished")
205 class PlayerWindow(QWidget
):
207 QWidget
.__init
__(self
)
210 self
.list_widget
= QListWidget(self
)
211 self
.list_widget
.itemActivated
.connect(self
.handle_activated
)
212 self
.list_widget
.setFocus()
214 self
.log
= QTextEdit(self
)
215 self
.log
.setReadOnly(True)
217 self
.splitter
= QSplitter(Qt
.Vertical
, self
)
218 self
.splitter
.addWidget(self
.list_widget
)
219 self
.splitter
.addWidget(self
.log
)
220 self
.splitter
.setSizes([900, 200])
222 self
.layout
= QVBoxLayout(self
)
223 self
.layout
.addWidget(self
.splitter
)
224 self
.setWindowTitle("BluPlayer")
226 def add_title(self
, title
):
227 name
= "Title %s (%s)" % (int(title
.id)+1, title
.duration
)
228 self
.list_widget
.addItem(name
)
229 self
.title_map
[name
] = title
231 def select_longest_title(self
):
234 for i
in xrange(self
.list_widget
.count()):
235 item
= self
.list_widget
.item(i
)
236 name
= str(item
.text())
237 title
= self
.title_map
[name
]
238 if longest_title
is None or title
.duration
> longest_title
.duration
:
239 longest_title
= title
241 self
.list_widget
.setCurrentItem(longest_item
)
243 def add_log_entry(self
, text
):
244 self
.log
.append(text
)
246 def popup_fatal_error(self
, text
):
247 QMessageBox
.critical(None, "Fatal error", text
)
250 def handle_activated(self
, item
):
251 name
= str(item
.text())
252 title
= self
.title_map
[name
]
256 popup_error_exit("MPlayer failed to play the video", e
)
258 def keyPressEvent(self
, e
):
259 if e
.key() in (Qt
.Key_Escape
, Qt
.Key_Backspace
):
262 QWidget
.keyPressEvent(self
, e
)
265 def killall_makemkvcon():
266 logging
.info("killing makemkvcon")
267 subprocess
.Popen(["killall", "makemkvcon"]).wait()
270 logging
.basicConfig(format
="%(levelname)s: %(message)s")
271 logging
.getLogger().setLevel(logging
.DEBUG
)
273 app
= QApplication(sys
.argv
)
274 app
.setQuitOnLastWindowClosed(True)
276 default_font
= app
.font()
277 default_font
.setPointSize(16)
278 app
.setFont(default_font
)
280 player_window
= PlayerWindow()
281 player_window
.resize(1200, 700)
285 worker_thread
= QThread()
286 makemkv
.moveToThread(worker_thread
)
288 worker_thread
.started
.connect(makemkv
.run
)
289 makemkv
.title_loaded
.connect(player_window
.add_title
)
290 makemkv
.title_load_complete
.connect(player_window
.select_longest_title
)
291 makemkv
.status
.connect(player_window
.add_log_entry
)
292 makemkv
.fatal_error
.connect(player_window
.popup_fatal_error
)
294 logging
.info("Starting worker thread")
295 worker_thread
.start()
298 logging
.info("Shutting down")
301 logging
.info("Waiting for worker thread")
302 worker_thread
.wait(2000)
303 logging
.info("Exiting...")
306 if __name__
== "__main__":