Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: endrift/mplayer-wrapper
base: master
...
head fork: endrift/mplayer-wrapper
compare: pygi
  • 9 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 272 additions and 70 deletions.
  1. +7 −0 COPYING
  2. +265 −70 player.py
7 COPYING
View
@@ -0,0 +1,7 @@
+Copyright (c) 2011 Jeffrey Pfau
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
335 player.py
View
@@ -1,85 +1,176 @@
-#!/usr/bin/env python2
-import pygtk
-pygtk.require('2.0')
-import gtk
-import gobject
+#!/usr/bin/env python
+from gi.repository import Gtk, GObject
+import fcntl
+import functools
import os
-import select
import signal
import subprocess
+# Gleaned from <linux/cdrom.h>
+CDROMEJECT = 0x5309
+CDROMCLOSETRAY = 0x5319
+CDROM_DRIVE_STATUS = 0x5326
+CDROM_LOCKDOOR = 0x5329
+
class RootWindow(object):
- icons = gtk.icon_theme_get_default()
+ icons = Gtk.IconTheme.get_default()
def __init__(self):
self.player = None
- self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
+ self.window = Gtk.Window()
self.window.connect('delete_event', self.quit)
self.window.set_title('Media Player')
- self.window.set_default_size(600, 300)
- hbox = gtk.HBox()
+ self.window.set_default_size(900, 300)
+ hbox = Gtk.HBox()
self.window.add(hbox)
- controls = gtk.VBox()
+ controls = Gtk.VBox()
hbox.pack_start(controls, False, False, 0)
- self.playlistControls = gtk.VBox()
+ self.playlistControls = Gtk.HBox(True)
+ primaryControls = Gtk.VBox()
+ secondaryControls = Gtk.VBox()
+ self.playlistControls.pack_start(primaryControls, True, True, 0)
+ self.playlistControls.pack_start(secondaryControls, True, True, 0)
controls.pack_start(self.playlistControls, True, True, 0)
- extButton = gtk.Button('External Drive')
+ extButton = Gtk.Button('External Drive')
extButton.connect('clicked', lambda w: self.selectFile('/'))
- ytButton = gtk.Button('YouTube Video')
+ folderButton = Gtk.Button('Folder')
+ folderButton.connect('clicked', lambda w: self.selectFolder('/'))
+ ytButton = Gtk.Button('YouTube Video')
ytButton.connect('clicked', lambda w: self.selectYouTube())
- removeButton = gtk.Button('Remove Selected')
+ dvdButton = Gtk.Button('DVD')
+ dvdButton.connect('clicked', lambda w: self.selectDVD())
+ removeButton = Gtk.Button('Remove Selected')
removeButton.connect('clicked', lambda w: self.removeSelected())
- self.playlistControls.pack_start(extButton)
- self.playlistControls.pack_start(ytButton)
- self.playlistControls.pack_start(removeButton)
-
- player = gtk.VBox()
- hbox.pack_start(player)
+ removeAllButton = Gtk.Button('Remove All')
+ removeAllButton.connect('clicked', lambda w: self.removeAll())
+ primaryControls.pack_start(extButton, True, True, 0)
+ primaryControls.pack_start(folderButton, True, True, 0)
+ primaryControls.pack_start(removeButton, True, True, 0)
+ secondaryControls.pack_start(ytButton, True, True, 0)
+ secondaryControls.pack_start(dvdButton, True, True, 0)
+ secondaryControls.pack_start(removeAllButton, True, True, 0)
+
+ player = Gtk.VBox()
+ hbox.pack_start(player, True, True, 0)
self.playlist = PlaylistWidget()
- scroller = gtk.ScrolledWindow()
+ scroller = Gtk.ScrolledWindow()
scroller.add(self.playlist.widget)
- scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
- player.pack_start(scroller)
+ scroller.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
+ player.pack_start(scroller, True, True, 0)
- self.scrubber = gtk.HScale()
+ self.scrubber = Gtk.HScale()
self.scrubber.set_draw_value(False)
self.scrubber.connect('adjust-bounds', lambda w, v: self.seek(v))
player.pack_end(self.scrubber, False, False, 0)
- playButton = gtk.Button();
+ leftControls = Gtk.VBox(True)
+ rightControls = Gtk.VBox(True)
+ bigButtons = Gtk.HBox(True)
+ controls.pack_start(bigButtons, True, True, 0)
+ bigButtons.pack_start(leftControls, True, True, 0)
+ bigButtons.pack_start(rightControls, True, True, 0)
+ playButton = Gtk.Button();
playButton.add(self._loadIcon('player_play'))
playButton.connect('clicked', self.play)
- pauseButton = gtk.Button()
+ pauseButton = Gtk.Button()
pauseButton.add(self._loadIcon('player_pause'))
pauseButton.connect('clicked', self.pause)
- stopButton = gtk.Button()
+ stopButton = Gtk.Button()
stopButton.add(self._loadIcon('player_stop'))
stopButton.connect('clicked', self.stop)
- controls.pack_start(playButton)
- controls.pack_start(pauseButton)
- controls.pack_start(stopButton)
- prevButton = gtk.Button()
- prevButton.add(self._loadIconSmall('player_rew'))
+ leftControls.pack_start(playButton, True, True, 0)
+ rightControls.pack_start(pauseButton, True, True, 0)
+ leftControls.pack_start(stopButton, True, True, 0)
+
+ prevButton = Gtk.Button()
+ prevButton.add(self._loadIcon('player_start'))
prevButton.connect('clicked', self.prev)
- nextButton = gtk.Button()
- nextButton.add(self._loadIconSmall('player_fwd'))
+ nextButton = Gtk.Button()
+ nextButton.add(self._loadIcon('player_end'))
nextButton.connect('clicked', self.next)
- skipBox = gtk.HBox()
- skipBox.pack_start(prevButton)
- skipBox.pack_start(nextButton)
- controls.pack_start(skipBox)
+ skipBox = Gtk.HBox()
+ skipBox.pack_start(prevButton, True, True, 0)
+ skipBox.pack_start(nextButton, True, True, 0)
+ controls.pack_start(skipBox, True, True, 0)
+
+ seekBox = Gtk.HBox()
+ seekBackButton = Gtk.Button()
+ seekBackButton.add(self._loadIcon('player_rew'))
+ seekBackButton.connect('clicked', lambda w: self.seekDelta(-10))
+ seekBox.pack_start(seekBackButton, True, True, 0)
+ seekForwardButton = Gtk.Button()
+ seekForwardButton.add(self._loadIcon('player_fwd'))
+ seekForwardButton.connect('clicked', lambda w: self.seekDelta(10))
+ seekBox.pack_start(seekForwardButton, True, True, 0)
+ controls.pack_start(seekBox, True, True, 0)
+
+ dvdbox = Gtk.Table(3, 3, True)
+ upButton = Gtk.Button()
+ upButton.add(self._loadIconTiny('go-up'))
+ upButton.connect('clicked', lambda w: self.dvdControl('up'))
+ dvdbox.attach(upButton, 1, 2, 0, 1)
+ leftButton = Gtk.Button()
+ leftButton.add(self._loadIconTiny('go-previous'))
+ leftButton.connect('clicked', lambda w: self.dvdControl('left'))
+ dvdbox.attach(leftButton, 0, 1, 1, 2)
+ downButton = Gtk.Button()
+ downButton.add(self._loadIconTiny('go-down'))
+ downButton.connect('clicked', lambda w: self.dvdControl('down'))
+ dvdbox.attach(downButton, 1, 2, 2, 3)
+ rightButton = Gtk.Button()
+ rightButton.add(self._loadIconTiny('go-next'))
+ rightButton.connect('clicked', lambda w: self.dvdControl('right'))
+ dvdbox.attach(rightButton, 2, 3, 1, 2)
+
+ selectButton = Gtk.Button('OK')
+ selectButton.connect('clicked', lambda w: self.dvdControl('select'))
+ dvdbox.attach(selectButton, 1, 2, 1, 2)
+
+ lastChapterButton = Gtk.Button()
+ lastChapterButton.add(self._loadIconTiny('go-first'))
+ lastChapterButton.connect('clicked', lambda w: self.seekChapter(-1))
+ dvdbox.attach(lastChapterButton, 0, 1, 0, 1)
+ nextChapterButton = Gtk.Button()
+ nextChapterButton.add(self._loadIconTiny('go-last'))
+ nextChapterButton.connect('clicked', lambda w: self.seekChapter(1))
+ dvdbox.attach(nextChapterButton, 2, 3, 0, 1)
+
+ menuButton = Gtk.Button()
+ menuButton.add(self._loadIconTiny('undo'))
+ menuButton.connect('clicked', lambda w: self.dvdControl('menu'))
+ dvdbox.attach(menuButton, 0, 1, 2, 3)
+ ejectButton = Gtk.Button()
+ ejectButton.add(self._loadIconTiny('player_eject'))
+ ejectButton.connect('clicked', lambda w: self.eject())
+ dvdbox.attach(ejectButton, 2, 3, 2, 3)
+
+ rightControls.pack_start(dvdbox, True, True, 0)
+
+ languageBox = Gtk.HBox(True)
+ subsButton = Gtk.Button('Subtitles')
+ subsButton.connect('clicked', lambda w: self.cycleSubs())
+ languageBox.pack_start(subsButton, True, True, 0)
+ langButton = Gtk.Button('Languages')
+ langButton.connect('clicked', lambda w: self.cycleLanguage())
+ languageBox.pack_start(langButton, True, True, 0)
+ controls.pack_start(languageBox, True, True, 0)
+
self.window.show_all()
@staticmethod
def _loadIcon(iconName):
- return gtk.image_new_from_pixbuf(RootWindow.icons.load_icon(iconName, 96, 0))
+ return Gtk.Image.new_from_pixbuf(RootWindow.icons.load_icon(iconName, 96, 0))
@staticmethod
def _loadIconSmall(iconName):
- return gtk.image_new_from_pixbuf(RootWindow.icons.load_icon(iconName, 48, 0))
+ return Gtk.Image.new_from_pixbuf(RootWindow.icons.load_icon(iconName, 48, 0))
+
+ @staticmethod
+ def _loadIconTiny(iconName):
+ return Gtk.Image.new_from_pixbuf(RootWindow.icons.load_icon(iconName, 32, 0))
def _setScrubberEnabled(self, enabled):
self.scrubber.set_sensitive(enabled)
@@ -94,35 +185,55 @@ def _setPlaylistEnabled(self, enabled):
def removeSelected(self):
self.playlist.removeSelected()
+ def removeAll(self):
+ self.playlist.clear()
+
def selectDrive(self):
pass
def selectFile(self, root):
- f = gtk.FileChooserDialog('Select File', None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
- f.set_default_response(gtk.RESPONSE_CANCEL)
+ f = Gtk.FileChooserDialog('Select File', None, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
+ f.set_default_response(Gtk.ResponseType.CANCEL)
f.set_select_multiple(True)
f.set_current_folder(root)
response = f.run()
- if response == gtk.RESPONSE_OK:
+ if response == Gtk.ResponseType.OK:
self.playlist.addItems([LocalFile(name) for name in f.get_filenames()])
f.destroy()
+ def selectFolder(self, root):
+ f = Gtk.FileChooserDialog('Select Folder', None, Gtk.FileChooserAction.SELECT_FOLDER, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
+ f.set_default_response(Gtk.ResponseType.CANCEL)
+ f.set_select_multiple(True)
+ f.set_create_folders(False)
+ f.set_current_folder(root)
+ response = f.run()
+ if response == Gtk.ResponseType.OK:
+ paths = functools.reduce(list.__add__, [os.walk(name) for name in f.get_filenames()])
+ files = functools.reduce(list.__add__, [[os.path.join(root, file) for file in files] for root, dirs, files in paths])
+ files.sort()
+ self.playlist.addItems([LocalFile(name) for name in files])
+ f.destroy()
+
def selectYouTube(self):
- ytWindow = gtk.MessageDialog(None, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, None)
+ ytWindow = Gtk.MessageDialog(None, 0, Gtk.MessageType.QUESTION, Gtk.ButtonsType.OK_CANCEL, None)
ytWindow.set_markup('Enter URL')
ytWindow.connect('delete_event', lambda w,d: w.destroy())
- ytWindow.set_default_response(gtk.RESPONSE_OK)
- inputBox = gtk.Entry()
- ytWindow.vbox.pack_start(inputBox)
+ ytWindow.set_default_response(Gtk.ResponseType.CANCEL)
+ inputBox = Gtk.Entry()
+ ytWindow.vbox.pack_start(inputBox, False, False, 0)
ytWindow.show_all()
response = ytWindow.run()
- if response == gtk.RESPONSE_OK and inputBox.get_text():
+ if response == Gtk.ResponseType.OK and inputBox.get_text():
try:
self.playlist.addItem(YouTubeMovie(inputBox.get_text()))
except:
pass
ytWindow.destroy()
+ def selectDVD(self):
+ self.playlist.addItem(DVDMovie())
+
def update(self):
if self.player:
if self.player.ended():
@@ -151,9 +262,9 @@ def play(self, widget):
if p:
self.player = p.play()
self._setScrubberEnabled(True)
- self.timer = gobject.timeout_add(500, self.update)
+ self.timer = GObject.timeout_add(500, self.update)
else:
- error = gtk.MessageDialog(None, 0, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, None)
+ error = Gtk.MessageDialog(None, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, None)
error.set_markup('Cannot play empty playlist!')
error.run()
error.destroy()
@@ -179,7 +290,6 @@ def stop(self, widget=None):
self.player.quit()
self._setScrubberEnabled(False)
self.player = None
- pass
def next(self, widget):
if self.player:
@@ -189,8 +299,43 @@ def prev(self, widget):
if self.player:
self.player.prev()
+ def seekDelta(self, delta):
+ if self.player:
+ self.player.seekDelta(delta)
+
+ def cycleSubs(self):
+ if self.player:
+ self.player.cycleSubs()
+
+ def cycleLanguage(self):
+ if self.player:
+ self.player.cycleLanguage()
+
+ def dvdControl(self, control):
+ if self.player:
+ self.player.dvdControl(control)
+
+ def seekChapter(self, direction):
+ if self.player:
+ self.player.seekChapter(direction)
+
+ def eject(self):
+ try:
+ cd = os.open('/dev/sr0', os.O_RDWR | os.O_NONBLOCK)
+ status = fcntl.ioctl(cd, CDROM_DRIVE_STATUS)
+ if status == 2:
+ fcntl.ioctl(cd, CDROMCLOSETRAY)
+ else:
+ if status == 4:
+ fcntl.ioctl(cd, CDROM_LOCKDOOR)
+ fcntl.ioctl(cd, CDROMEJECT)
+ except:
+ print('Could not eject CD')
+ finally:
+ os.close(cd)
+
def quit(self, widget, event, data=None):
- gtk.main_quit()
+ Gtk.main_quit()
return False
class LocalFile(object):
@@ -224,7 +369,6 @@ def _pollProc(self, type):
command.extend(type)
command.append(self.url)
proc = subprocess.Popen(command, stdout=subprocess.PIPE)
- print(command)
(out, err) = proc.communicate()
if proc.returncode:
raise subprocess.CalledProcessError(proc.returncode, ' '.join(command))
@@ -243,25 +387,52 @@ def type():
def name(self):
return self.title
+class DVDMovie(object):
+ def __init__(self, dev='/dev/dvd'):
+ self.device = dev
+
+ command = ['blkid', '-o', 'value', '-s', 'LABEL']
+ command.append(self.device)
+ proc = subprocess.Popen(command, stdout=subprocess.PIPE)
+ (out, err) = proc.communicate()
+ if proc.returncode:
+ self.label = 'Unknown DVD'
+ else:
+ self.label = out.rstrip()
+
+ def __repr__(self):
+ return 'DVD: {0}'.format(self.device)
+
+ def uri(self):
+ return 'dvdnav:///{0}'.format(self.device)
+
+ @staticmethod
+ def type():
+ return 'DVD'
+
+ def name(self):
+ return self.label
+
class PlaylistWidget(object):
def __init__(self):
- def format_name(col, cell, model, iter):
+ def format_name(col, cell, model, iter, func_data):
obj = model.get_value(iter, 0)
cell.set_property('text', obj.name())
- def format_type(col, cell, model, iter):
+ def format_type(col, cell, model, iter, func_data):
obj = model.get_value(iter, 0)
cell.set_property('text', obj.type())
- self.listStore = gtk.ListStore(object)
- nameText = gtk.CellRendererText()
- nameCol = gtk.TreeViewColumn('Name', nameText)
+ self.listStore = Gtk.ListStore(object)
+ nameText = Gtk.CellRendererText()
+ nameCol = Gtk.TreeViewColumn('Name', nameText)
nameCol.set_cell_data_func(nameText, format_name)
- typeText = gtk.CellRendererText()
- typeCol = gtk.TreeViewColumn('Type', typeText)
+ typeText = Gtk.CellRendererText()
+ typeCol = Gtk.TreeViewColumn('Type', typeText)
typeCol.set_cell_data_func(typeText, format_type)
- self.widget = gtk.TreeView(self.listStore)
- self.widget.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
+ self.widget = Gtk.TreeView()
+ self.widget.set_model(self.listStore)
+ self.widget.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
self.widget.set_reorderable(True)
self.widget.append_column(nameCol)
self.widget.append_column(typeCol)
@@ -280,8 +451,11 @@ def removeSelected(self):
for i in iters:
model.remove(i)
+ def clear(self):
+ self.listStore.clear()
+
def compile(self):
- if not self.listStore.get_iter_root():
+ if not self.listStore.get_iter_first():
return None
playlist = Playlist()
self.listStore.foreach(lambda model, path, iter, user_data: playlist.items.append(model.get_value(iter, 0).uri()), None)
@@ -338,7 +512,7 @@ def onTimeout():
def ended(self):
return not self.proc or self.proc.poll() is not None
- def seek(self, delta):
+ def seekDelta(self, delta):
self._write('seek {0}'.format('+{0}'.format(delta) if delta >= 0 else '{0}'.format(delta)))
def next(self):
@@ -378,28 +552,49 @@ def paused(self):
def seek(self, value):
self._write('seek {0} 2'.format(value))
+ def seekChapter(self, direction):
+ self._write('seek_chapter {0}'.format(direction))
+
def getTrack(self):
pass
+ def cycleSubs(self):
+ self._write('sub_select')
+
+ def cycleLanguage(self):
+ self._write('switch_audio')
+
+ def dvdControl(self, control):
+ self._write('dvdnav {0}'.format(control))
+
class Playlist(object):
def __init__(self):
self.items = []
def play(self):
try:
- os.mkfifo('/tmp/mplayer.fifo', 0o0660)
+ os.remove('/tmp/mplayer.fifo')
+ except:
+ pass
+ try:
+ os.mkfifo('/tmp/mplayer.fifo', 0o660)
except:
pass
proc = subprocess.Popen(['mplayer', '-playlist', '-', '-quiet', '-slave', '-input', 'file=/tmp/mplayer.fifo'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=1)
for i in self.items:
- proc.stdin.write(i)
- proc.stdin.write('\n')
+ proc.stdin.write((i + '\n').encode('utf-8'))
proc.stdin.close()
return Control('/tmp/mplayer.fifo', proc)
def main():
- RootWindow()
- gtk.main()
+ try:
+ RootWindow()
+ Gtk.main()
+ finally:
+ try:
+ os.remove('/tmp/mplayer.fifo')
+ except:
+ pass
if __name__ == '__main__':
main()

No commit comments for this range

Something went wrong with that request. Please try again.