› Code Snippets 
» profile.class.php
Tue, 06/24/2008 - 18:29
Every now and then I want to profile a given part of PHP code. For example I want to quickly check wether my current changeset to GeSHi works faster or is horribly slower. For a big change I’ll stick to Xdebug and KCachegrind. But for a quick overview? Overkill in my eyes.
Say hello to
profile.class.php
, a simple timer class for PHP5 which you can use to get an overview about where how much time is spent. This is in no way a scientific method nor should you take the results of a single run as a basis for you decisions.I’ve set an emphasize on an easy API so you don’t have to pollute your code with arbitrary hoops and whistles.
UPDATE: You can find the current updated source in the SVN repo of GeSHi.
» mp3dump take two - better audio quality
Sat, 03/29/2008 - 16:29
So just yesterday I’ve published a bash script which rips the audio stream of Flash Videos (
*.flv
) to mp3 format. It’s nice, fast and imo all-purpose. But I didn’t like the audio quality. Thus below you can find a second version of the script which is usingmplayer
andlame
instead offfmpeg
. Usage and behaviour should be pretty much the same. Only the audio quality should be better on cost of a bit more computing.Since it relies on the fact that the input
*.flv
video already uses MP3 encoding for its audio stream this might not work for every flash file! Youtube works just fine though. You can find the script after the break, it’s also attached below. For usage / installation / more information read the old article.If you wonder why I reencode the audiodump with lame: The dumped mp3 file is considered by most (all?) audio players to be ~10x the length. A five minute video gets a 45 minute audio dumpfile. It plays fine but seeking is a mess. Reencoding fixes this. If you know an alternative which does not require reencoding or is generally faster - please drop a comment!
» Patching Kirocker Music Display for volume control via mouse scrolling
Fri, 03/28/2008 - 16:32
Kirocker is such a great application, it’s a pity the developer Sébastien Laoût has given up on it and is using Windows now!
Usually I only use the kicker applet, the full screen window is nice for parties though. But I didn’t like the way the applet handles scroll events: It seeks (if possible). Since I rarely seek when listening to music I’d rather have the behaviour of the Amarok tray icon: volume control!
I had a look inside the sources and found this part in
src/coverdisplay.cpp
:- {
- if (areControlsShown()) {
- if (event->orientation() == Qt::Vertical) {
- PlayerInformation *infos = PlayerInformation::instance();
- if (infos->canSeek()) {
- int deltaSeconds = 10 * (event->delta() > 0 ? 1 : -1);
- m_infos->seekRelative(deltaSeconds);
- }
- } else {
- if (event->delta() > 0)
- AmarokApi::volumeUp();
- else
- AmarokApi::volumeDown();
- }
- }
- }
As you can see it already supports volume control via scrolling! The thing is I’ve disable four-way scrolling for my MX1000. Thus I’ve simply swapped the
if
statements and now I’m really happy with Kirocker. Here’s the updated snippet:- {
- if (areControlsShown()) {
- if (event->orientation() == Qt::Vertical) {
- if (event->delta() > 0)
- AmarokApi::volumeUp();
- else
- AmarokApi::volumeDown();
- } else {
- PlayerInformation *infos = PlayerInformation::instance();
- if (infos->canSeek()) {
- int deltaSeconds = 10 * (event->delta() > 0 ? 1 : -1);
- m_infos->seekRelative(deltaSeconds);
- }
- }
- }
- }
Replace the old code, compile Kirocker, install it. Then restart kicker (
killall kicker; sleep 1; kicker;
) and Kirocker should accept scroll events in the way the Amarok tray icon does!» mp3dump: Rip the audio stream of a Flash video to MP3
Fri, 03/28/2008 - 00:03
UPDATE: Also check out mp3dump part 2
On some undefined occasion you might want to dump the audio stream of a flash file. This is how you do it:
- Get the
*.flv
, e.g. visit youtube with Konqueror and check/tmp
for a file likeFlashV5SQLt
- this is your FLV. - Install some dependencies:
ffmpeg
is required,mp3info
andecasound
are optional but strongly recommended. - Download the script below (it’s attached!) and save it inside your
$PATH
, remember how you’ve called it! I’ve saved the script inside
~/.bin
which is inside my$PATH
so all I have to do now is running it:mp3dump FlashV5SQLt "Foo Bar" "All Your Base Are Belong To Us"
This would rip the audio stream of
FlashV5SQLt
to a file namedFoo Bar - All Your Base Are Belong To Us.mp3
- with emulated stereo sound and basic MP3 tags (artist and title). Coolio!
- Get the
» Reinstall Nvidia Driver
Sat, 02/09/2008 - 03:41
For those of you, who use the original NVIDIA display driver and have to reinstall it with every kernel update: Here is a little bashscript for your convenience.
- #!/bin/bash
- sudo /etc/init.d/kdm stop
- sudo chvt 1
- sudo sh ~/Downloads/NVIDIA-Linux-x86-*.run --ui='none' -aNq
- sudo /etc/init.d/kdm start
- sudo chvt 7
Save it e.g. as
~/.bin/nv_reinst.sh
, make it executable (chmod +x ~/.bin/nv_reinst.sh
). Then put your NVIDIA driver (only the newest version please) into~/Downloads/
(or change the path above). Now run your script.Attention: Save all your work, since kdm will be stopped! You’ll lose all unsaved work!
» highlighted APT sources.list in nano
Tue, 04/10/2007 - 18:17
And here another syntax file for Nano. This time it highlights the
/etc/apt/sources.list
:- ## syntax highlighting for /etc/apt/sources.list
- syntax "apt/sources.list" "sources\.list(\.old|~)?$"
- # component
- color brightmagenta "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+ .+$"
- # distribution
- color brightred "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+"
- # URI
- color brightgreen "(http|file|ftp):/[^ ]+"
- # cdroms
- # [^\]] does not work…
- color brightgreen "cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/"
- # deb / deb-src
- color cyan "^deb"
- color brightblue "^deb-src"
- # comments
- color brightyellow "#.*"
» syntax highlighting for *.ini files in nano
Tue, 04/10/2007 - 17:01
Use the snippet below in your
~/.nanorc
or/etc/nanorc
file to highlight*.ini
files likephp.ini
in Nano.- # ini highlighting
- syntax "ini" "\.ini(\.old|~)?$"
- # values
- color brightred "=.*$"
- # equal sign
- color green "="
- # numbers
- color brightblue "-?[0-9\.]+\s*($|;)"
- # ON/OFF
- color brightmagenta "ON|OFF|On|Off|on|off\s*($|;)"
- # sections
- color brightcyan "^\s*\[.*\]"
- # keys
- color cyan "^\s*[a-zA-Z0-9_\.]+"
- # comments
- color brightyellow ";.*$"
» open URL a in variable browser
Mon, 03/12/2007 - 20:44
I was recently annoyed by how URLs were started in KDE: I used an antiquated firefox command to run an URL in a new tab (
firefox-remote "OpenURL(%u, new-window)"
) which does the job quite well. But: If you have no running firefox instance this command will simply do nothing. Right - nothing! You’ll have to start firefox manually and click once again on the link (in konversation, kopete or wherever).But the newer firefox versions (I don’t know when this feature was added) support the
firefox -newtab %u
command. This is all you need if you only use firefox. Configure KDE viakcontrol
to use this command as a standard webbrowser.This was still not enough for me though, as I regularly switch to konqueror or opera, because firefox is sometimes to slow for a quick browse. This is what I came up with:
» Styling Apache Indexes
Thu, 01/25/2007 - 21:27
The default output of Apaches
Options +Indexes
doesn’t look good at all. Very 1990 something… But - as with pretty much any open source open source software - there are tons of options, which enable you to change everything to your likings!» Recursive Wordcount
Sat, 07/22/2006 - 03:39
Yes, I am a Linux user and I really appreciate the freedom I get by using either an awesome desktop environment or the command line - or both!
The function I’m going to present you gives you a good overview of how many words, lines and bytes files in a given folder have. I’m speaking about
wc
.