Friday, July 23, 2010

Mplayer GTK Frontend Code

Download sourceforge code
















Hi every one,i am posting this blog regarding the making of an simple Mplayer GUI using pygtk,and i have made this code on Linux based platform(Ubuntu 10 to be exact).
For those who want to compile Mplayer with hardware acceleration,i suggest that you check out the following blog

mplayer vdpau

The above blog will tell you how to compile mplayer with nvidea hardware acceleration,after that go to the ubuntu software center and install bpython,and make sure that python 2.6 or higher is installed.

I installed bpython to eliminate the need to install certain dependencies one by one,i am using a text editor to make the python codes for the mplayer interface.before continuing with coding,u must understand the basics of pygtk coding language,download the pygtk tutorial from the link below

pygtk tutorial

screenshot:
















Then create an new document and name it like "mplayer-gui" or something with an extension ".py"
Those who are familier or tried to make an mplayer gui must have confused with the exact line of the code where we actually embeds the mplayer into the pygtk or python code,the code below adds an mplayer plugin into your pygtk file

"
Mplayer = Plugins.importAPI('Mplayer')" this line of code should be after all the import headers

example:

import gtk
import sys
import select
import pygtk
import gnome.ui
import time
import os
from subprocess import Popen
import screenlets
from screenlets import DefaultMenuItem
from screenlets.options import BoolOption, StringOption, FontOption, ListOption, IntOption,ColorOption
from screenlets import Plugins
import cairo
import pango
import commands



Mplayer = Plugins.importAPI('Mplayer')

this plugin works only if you have screenlets installed otherwise the compiler will show error in "import screenlet", so install screenlets from your synaptic manager.

now that your code has mplayer plugin added,now you need a set of other lines of code to access the mplayer functions like the play,pause,stop etc
use these following codes wherever you need to access the mplayer funcntionalities

self.mplayer = Mplayer.Mplayer(self)
self.mplayer.play(files)

for example the play button when clicked on the mplayer gui will call a function play,which will have the above codes as well as the code to play the selected file
example :

def play(self,widget):
self.mplayer = Mplayer.Mplayer(self)
global files
self.file_selected
print files
self.mplayer.play(files)
print("Playing Video")

dont get confuse by the "self.file_selected" i am calling an another function to acces the file explorer widget of pygtk,u will learn about it under filechooser in pygtk tutorial,you can alternativley use the files="/home/user/desktop/vedio.avi" and use that path to play the file instead of using

global files
self.file_selected
print files
the "self.mplayer.play(files)" is used to play the file,where "files" contain the path of the vedio file,similary u can create functions for pause,stop,progressbar,volume control etc

example:

def play(self,widget):
self.mplayer = Mplayer.Mplayer(self)
global files
self.file_selected
print files
self.mplayer.play(files)
print("Playing Video")


def stop(self,widget):

self.mplayer.close()
print("Video Stopped")
def pause(self,widget):
self.mplayer.pause()
print("Video Pause")
def volume(self,widget,event):
self.mplayer = Mplayer.Mplayer(self)
volume='mplayer -af volume=70'
self.mplayer.volume(volume)

know this,untill and unless you get a complete or general idea about pygtk and basic python scripting,you are going to keep on scratching your head,so leave the easy going mentality and read the pygtk tutorial,if u wanna get the idea about whats happening in this code :D

and also visit the mplayer website to get the commandline codes for fullscreen,next,previous etc

note that there is a lot of header files,imported tht i havent used yet,this is a work in progress,using this code you will get a start in your first step in makig a gui for mplayer in Linux

here is the full code:

##############################
# #
#author:Bince Mathew #
# #
#e-mail:bincemathew@gmail.com #
# #
##############################
#install bpython via ubuntu software center,to get all the header files working
import gtk
import sys
import select
import pygtk
import gnome.ui
import time
import os
from subprocess import Popen
import screenlets
from screenlets import DefaultMenuItem
from screenlets.options import BoolOption, StringOption, FontOption, ListOption, IntOption,ColorOption
from screenlets import Plugins
import cairo
import pango
import commands

# importing the mplayer plugin into the code,make sure that you have installed screenlets via synaptic manager
Mplayer = Plugins.importAPI('Mplayer')
filechooser= gtk.FileChooserWidget()
window1=gtk.Window()
vediofile = gtk.Button("OK")

files=""
class mplayer:


def play(self,widget):
self.mplayer = Mplayer.Mplayer(self)
global files
self.file_selected
print files
self.mplayer.play(files)
print("Playing Video")


def stop(self,widget):
self.mplayer.close()
print("Video Stopped")
def pause(self,widget):
self.mplayer.pause()
print("Video Pause")
def volume(self,widget,event):
self.mplayer = Mplayer.Mplayer(self)
volume='mplayer -af volume=70'
self.mplayer.volume(volume)
def openfile(self,widget):

window1.show_all()
def file_selected(self, widget):
global files
window1.hide()
filenames = filechooser.get_filenames()
for filename in filenames:
print ("Selected filepath: %s" % filename)

# -fs is for fullscreen mode,delete the '-fs' if you dont wish to get the vedio in full screen mode

files=filename


def __init__(self):

#creating a new window with size 400x200
window1.set_default_size(400,200)

window=gtk.Window()
window.set_default_size(400,40)

#send_command=('mplayer')
hbox=gtk.HBox(False,5)


#file chooser is an inbuilt widget in pygtk,to get the file explorer

self.filechooser = gtk.FileChooserWidget()
self.filechooser.set_select_multiple(True)
button = gtk.Button("OK")
button.set_size_request(80, -1)
window1.connect("destroy", lambda w: gtk.main_quit())
button.connect("clicked", self.file_selected)
vbox = gtk.VBox(False, 5)
window1.add(vbox)
vbox.pack_start(filechooser)
vbox.pack_end(button, False, False, 0)

volumebutton = gtk.VolumeButton()
volumebutton.set_value(0.5)
progressbar = gtk.ProgressBar()
tbtn=gtk.ToggleButton("Mute")
tbtn.set_active(True)
lable=gtk.Label("Enter starting time for playback")
Play=gtk.Button("PLAY")
Pause=gtk.Button("PAUSE")
Stop=gtk.Button("STOP")
Next=gtk.Button("Next")
txtbox=gtk.Entry()
txtbox.set_text("enter time")
Open=gtk.Button("Open")
window.add(hbox)


hbox.pack_start(Play)
hbox.pack_start(Pause)
hbox.pack_start(Stop)
hbox.pack_start(Next)
hbox.pack_start(tbtn)
hbox.pack_start(Open)

hbox.pack_start(progressbar)
hbox.pack_start(volumebutton)



window.connect("destroy",lambda w:gtk.main_quit())
Play.connect("clicked",self.play)
Stop.connect("clicked",self.stop)
Pause.connect("clicked",self.pause)
Open.connect("clicked",self.openfile)

volumebutton.connect("value-changed",self.volume)


#use this code if u want to import the gui design from a glade file
#builder = gtk.Builder()
#builder.add_from_file("mplayer.xml")
#builder.connect_signals({ "on_window_destroy" : gtk.main_quit })
#self.window = builder.get_object("mplayer")
#self.window.show()

window.show_all()

mplayer()
gtk.main()


three things:
1 learn basic python scritping
2 Atleast read the pygtk tutorial pdf
3 get all the dependencies installed

save the above code in .py format and compile it in console via python,make sure that you are in the same folder of your code file to compile it via python in console,example "python mplayer.py" this will compile the code and bring the gui,if all dependencies are installed the mplayer gui will emerge,i will post modified codes of my project as it progress,everyone is welcome in modifiying this code,if possible send me the modified code to bincemathew@gmail.com.
so that we both can benefit from each others knowledge :D