Posts Tagged ‘snippets’

simple is beautiful - command line id3 tagging

Sunday, March 16th, 2008

generally speaking, my set of mp3s is very well tagged. for my personal mp3s, i used to exclusively use easytag to tag them, and now i use a combination of easytag and amarok (which is totally awesome by the way!)

but sometimes, i have to mass edit id3tags for mp3s on the server, and i don't have the luxury of using such gui tools for the editing. as thus, i've been mainly using id3v2 within some perl scripts to tag mp3s. this turns out to work great, but i also wanted to be able to add album art to the mp3s from the command line.

i couldn't figure out how to do it using id3v2 (perhaps using the custom frames, there's a way, but nothing extremely simple and obvious from what i was looking at). then i found the solution in the form of a id3lib-ruby, a ruby wrapper for id3lib, the same library that id3v2 is based on.

with this, everything turns out to be extremely easy -

 
require 'id3lib'
 
tag = ID3Lib::Tag.new('myfile.mp3')
cover = {
   :id => :APIC,
   :mimetype => 'image/jpeg',
   :picturetype => 3,
   :data => File.read('cover.jpg')
}
 
tag << cover
tag.update!

and that's it. nice and simple. by the way, a picturetype of 3 denotes a front cover and is the default value (just learned that from a quick search). oh and the output mp3 image cover shows up fine in both linux and on itunes. beautiful!

dealing with timezones in php

Thursday, March 13th, 2008

so i was working on some code in which i needed to know whether or not it was dst for a given country and/or timezone or not. luckily, with php5.2, some sparsely documented (yet very useful) classes were introduced - a more thorough documentation can be found here.

so let's say i want to know whether or not egypt is in dst right now or not... so first i need to know what zoneinfo file egypt uses (for egypt, it's simple, but this trick is useful for more obscure places, like "isle of man," for example):

 
cd /usr/share/zoneinfo
grep -i egypt iso*.tab        # get the iso country code for egypt
 
# the above command returns 'EG' - so...
grep EG zone.tab
# returns 'Africa/Cairo'

in many cases, there are many timezones that exist for a given country. in many cases, it's obvious which file you need, but in some cases, it's not very obvious. in those cases, i found it helpful to open the binary files and look at the very last line, in which some hint about the offset of the timezone is given.

anyway... once you have the zoneinfo file that you would use, it's very easy to find whether or not you are in dst (well, assuming that you know what the standard, non-dst offset from utc is). for example:

 
$tz = new DateTimeZone('America/New_York');
$date = new DateTime();
$date->setTimezone($tz);
echo $date->format(DATE_RFC3339) . "\n";
echo $date->getOffset()/3600 . "\n";

running this returns the time in new york, and the offset (-4). since the standard est offset is -5 hours, -4 means we're +1 which means we are currently on dst.

so if you don't know the standard offset, another trick that you could do is pass some parameters to the new DateTime() constructor - so for example...

 
$tz = new DateTimeZone('America/New_York');
$date = new DateTime('2008-12-31');
$date->setTimezone($tz);
echo $date->getOffset()/3600 . "\n";

this returns -5, which is out of dst. anyhow, you could use the above if you don't know the default offset for a timezone for dst by passing in 2 dates - something towards the middle of the year (july-ish) and something towards the end of the year (december-ish). if the offsets are different, the place probably has dst.

also, do note that some places have things a little differently - so dst in windhoek, namibia, for example, ends in april and starts in september.

come again?

Sunday, November 27th, 2005

a long time ago, when i still used to use xmms, there was this nice plugin called Repeat It! that let you say, "repeat this song between this and this times forever."

i found myself missing that feature today... unfortunately, i don't have xmms installed and don't want it installed because i now like the gtk2 based mp3 players, like beep for example.

but because beep is being rewritten as bmpx, i figure rather than porting the plugin, i can just write a much simpler command line version based on the existing plugin that works with beep.

note however that this is really bad... it does very little error checking, and the sleep solution isn't really the best solution... but its a pretty good solution for my limited uses :)

and in 25 lines, here it is:

 
#include <stdio.h>
#include <signal.h>
#include <bmp/beepctrl.h>
 
void got_signal(int x){
	printf("Exiting!\n"); exit(1);
}
 
int main(int argc, char** argv){
 
   int startpos, endpos;
   if (!xmms_remote_is_running(0))
      return (0*printf("\033[1mbmp is not running.\033[0m\n"))-1;
 
   if (argc != 3)
      return (0*printf("usage: %s start end\n", argv[0]));
 
   startpos = atoi(argv[1]);
 
   endpos = atoi(argv[2]);
 
   signal(SIGINT, got_signal);
 
   if (!xmms_remote_is_playing(0)){ xmms_remote_play(0); }
 
   while (1){
      xmms_remote_jump_to_time(0, startpos * 1000);
      sleep(endpos - startpos);
   }
}

to compile it, just do:

 
gcc `beep-config --cflags --libs` loopsegment.c -o loop

then just run beep, load up the file you want to play, and run the script passing in start and end seconds.

hitchhiker’s guide, ruby mozembed

Friday, September 9th, 2005

"Space is big. You just won't believe how vastly, hugely, mind- bogglingly big it is. I mean, you may think it's a long way down the road to the chemist's, but that's just peanuts to space."

i finally got around to reading the hitchhiker's guide to the galaxy. really funny book. maybe i will try to read the other books at some point.

i played around some today with ruby-gnome2 bindings. all i can say is wow. the website documentation is incredible, and you can do so much in so little code. i played around some with ruby-gtkmozembed, and in a few lines of code, you basically have a fully functional webbrowser.

 
require 'gtkmozembed'
 
class Main
def initialize
Gtk.init
window = Gtk::Window.new
window.resize(800, 600)
window.title = "ahmed's google test"
 
browser = Gtk::MozEmbed.new
browser.chrome_mask = Gtk::MozEmbed::ALLCHROME
browser.location = "http://www.google.com"
browser.signal_connect("title"){
window.title = browser.title;
}
 
window < < browser
window.show_all
window.signal_connect("destroy"){ Gtk::main_quit }
end
end
 
Main.new
Gtk.mai

that's all for now.