Archive for February, 2008

mp3 tag editor using Ruby

Friday, February 8th, 2008

Was listening to music and thought of changing the mp3 tags. Quickly i opened the file and right clicked to change it, but unfortunately i couldn’t. So i thought of building a small application which will help to me to play with these tags.

I searched for some helpers in ruby and came to know about “RUBY-MP3INFO” which will help you to read, write, remove id3v1 and id3v2 tags.You can easily do this in few steps.
Installation
$ ruby install.rb config
$ ruby install.rb setup
# ruby install.rb install

Or simply
gem install ruby-mp3info
Documentation is available at http://ruby-mp3info.rubyforge.org/

We knows the tags now with the help of documentation . Using some simple coding we can access this tags , the following code fragments will help to do the rest.

Dispaly all tags

Show_tags

run as ./mp3tags.rb -tags <mp3filename>; , which will give u that tags of that mp3 file

Now we can expand this code to insert our own tags.

chaging_tag

run ./change_tag.rb -s <field> <fieldname> <mp3 filename>; it will help u to change the <field> of <filename> with <fieldname>

After doing these i thought of giving a directory as input and want to change the field of all the files insidfe that directory.For doing that FileTest.directory?(arg) in ruby helped to find out whether the passed argument is a file or directory.

directory

run ./directory.rb -dir <field> <fieldname> <directoryname>; which will change the field of all files inside the directory name with given fieldname

Atlast I integerated all these together which which perfectly working and giving you access to read, write, remove id3v1 and id3v2 tags

change_tags.rb

run /change_tags.rb with respective field

changing tags

Fianlly we can play with tags of mp3 files.
Also we can apply “Tk” tool of our Ruby and we can have a GUI for dispalying the tags and editing fields.If you installed Ruby yourself, possibly the Tk bindings do not work , so make sure that by requiring Tk on irb

irb>>require("tk")
true
irb>>

Or u can easily install this by

sudo apt-get install libtcltk-ruby

Gui form

gui

Hope this will help u and make a try out of this :)

Print

IPC with D-Bus in Linux

Friday, February 8th, 2008

D-bus is an interprocess communication mechanism using message buses. It is a way for running applications to communicate with one another.

Eg: We can write a program that communicates with Pidgin the IM program to get the buddy list.

Developers can enable D-bus support in their applications and expose a set of functionalities to be accessed via D-bus. Quite a few applications already have D-bus support.

Communicating to an D-Bus enabled Application

We can use a D-Bus API to communicate to a running application, that supports D-Bus. D-Bus APIs are available in many languages , starting from a very low level C API. Ruby D-Bus API is called Rbus.

Installing Rbus

sudo apt-get install dbus
sudo apt-get install libdbus-1-3
sudo apt-get install libdbus-glib-1-dev
sudo gem install rbus

Using Rbus to retrieve the ‘now playing’ songfrom Rhythmbox

Rhythmbox is a music player inspired by Apple ITunes, and is D-Bus enabled.

sudo apt-get install rhythmbox

The following code fragment establishes a D-bus connection with Rhythmbox and gets the currently playing song filename path from it.

session_bus = RBus.session_bus
rhythmbox = session_bus.get_object('org.gnome.Rhythmbox', '/org/gnome/Rhythmbox/Player')
p rhythmbox.getPlayingUri()

Refer this for more examples on Rbus

Using Rbus to set status message in Pidgin

Pidgin is an opensource Instan messenger program. A detailed document on Pidgin’s D-Bus support is available here.

session_bus = RBus.session_bus
pidgin = session_bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")

status = pidgin.PurpleSavedstatusNew(test,2)
pidgin.PurpleSavedstatusSetMessage(status, "hello world")
pidgin.PurpleSavedstatusActivate(status)

  1. Connects to Pidgin via D-bus
  2. Creates a new status message with name “test” and type 2 (2 stands for ‘available’)
  3. Saves it with status message as “Hello World”
  4. Activates it.

Making Rhythmbox’s ‘now playing’ song the status message of Pidgin

Idea is to write a ruby program that communicates with Rhythmbox and pidgin

  1. to get the current song from pidgin

2. get the song’s details from its mp3 tags (assuming its an mp3 file)

3. set pidgin’s status message with the extracted details, like song title, artist, album etc.

Extracting details from mp3 tags is extremely simple using the mp3info library

Mp3Info.open("myfile.mp3") do |mp3|
   puts mp3.tag.title
   puts mp3.tag.artist
   puts mp3.tag.album
   puts mp3.tag.tracknum

   mp3.tag.title = "track title"
   mp3.tag.artist = "artist name"
end

Complete program is available for download here

After starting pidgin and rhythmbox (play some mp3file with non-empty tags !), run the program as “ruby now_playing.rb”. Pidgin’s status message will now start showing the title of the playing song in Rhythm box :)

Print