Code Snippets Syndicate content

  • » 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 using mplayer and lame instead of ffmpeg. 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:

    1. void CoverDisplay::wheelEvent(QWheelEvent *event)
    2. {
    3. if (areControlsShown()) {
    4. if (event->orientation() == Qt::Vertical) {
    5. PlayerInformation *infos = PlayerInformation::instance();
    6. if (infos->canSeek()) {
    7. int deltaSeconds = 10 * (event->delta() > 0 ? 1 : -1);
    8. m_infos->seekRelative(deltaSeconds);
    9. }
    10. } else {
    11. if (event->delta() > 0)
    12. AmarokApi::volumeUp();
    13. else
    14. AmarokApi::volumeDown();
    15. }
    16. }
    17. }

    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:

    1. void CoverDisplay::wheelEvent(QWheelEvent *event)
    2. {
    3. if (areControlsShown()) {
    4. if (event->orientation() == Qt::Vertical) {
    5. if (event->delta() > 0)
    6. AmarokApi::volumeUp();
    7. else
    8. AmarokApi::volumeDown();
    9. } else {
    10. PlayerInformation *infos = PlayerInformation::instance();
    11. if (infos->canSeek()) {
    12. int deltaSeconds = 10 * (event->delta() > 0 ? 1 : -1);
    13. m_infos->seekRelative(deltaSeconds);
    14. }
    15. }
    16. }
    17. }

    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:

    1. Get the *.flv, e.g. visit youtube with Konqueror and check /tmp for a file like FlashV5SQLt - this is your FLV.
    2. Install some dependencies: ffmpeg is required, mp3info and ecasound are optional but strongly recommended.
    3. Download the script below (it’s attached!) and save it inside your $PATH, remember how you’ve called it!
    4. 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 named Foo Bar - All Your Base Are Belong To Us.mp3 - with emulated stereo sound and basic MP3 tags (artist and title). Coolio!

  • » 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.

    1. #!/bin/bash
    2. sudo /etc/init.d/kdm stop
    3. sudo chvt 1
    4. sudo sh ~/Downloads/NVIDIA-Linux-x86-*.run --ui='none' -aNq
    5. sudo /etc/init.d/kdm start
    6. 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:

    1. ## syntax highlighting for /etc/apt/sources.list
    2.  
    3. syntax "apt/sources.list" "sources\.list(\.old|~)?$"
    4. # component
    5. color brightmagenta "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+ .+$"
    6. # distribution
    7. color brightred "^deb(-src)? ((http|file|ftp):/[^ ]+|cdrom:\[[^\]]+\]/|cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/) [^ ]+"
    8. # URI
    9. color brightgreen "(http|file|ftp):/[^ ]+"
    10. # cdroms
    11. # [^\]] does not work…
    12. color brightgreen "cdrom:\[[a-zA-Z0-9\._-\(\) ]+\]/"
    13. # deb / deb-src
    14. color cyan "^deb"
    15. color brightblue "^deb-src"
    16. # comments
    17. 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 like php.ini in Nano.

    1. # ini highlighting
    2. syntax "ini" "\.ini(\.old|~)?$"
    3.  
    4. # values
    5. color brightred "=.*$"
    6. # equal sign
    7. color green "="
    8. # numbers
    9. color brightblue "-?[0-9\.]+\s*($|;)"
    10. # ON/OFF
    11. color brightmagenta "ON|OFF|On|Off|on|off\s*($|;)"
    12. # sections
    13. color brightcyan "^\s*\[.*\]"
    14. # keys
    15. color cyan "^\s*[a-zA-Z0-9_\.]+"
    16. # comments
    17. 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 via kcontrol 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.