flac2mp3

I was going through Fedora Planet stream when I noticed this blog post about converting Flac files to mp3 on Linux.   That reminded me of a couple of folders with files that I can’t listen to in my car.  So I thought I’d convert them now, especially that I have the guide handy it only consists of a couple of steps.  I tried and it worked, except that default options used resulted in the mp3 file of 128 kbps, which is a rather low quality.  So I played a bit with it and ended up with the following shell flac2mp3.sh script:

#!/bin/bash

# Convert flac file to mp3 with best quality VBR

for file in *.flac
do
    base=`basename "$file" .flac`
    # Convert flat to wav
    flac -d "$file"
    # Convert wav to mp3 with variable bit rate, best quality
    lame -V 0 "$base".wav "$base".mp3
done

As mentioned in the comments, it creates variable bit rate (VBR) mp3 files with maximum quality.

Update: The moment I finished this blog post I felt weird. It was almost like I did it before. And indeed. I should have searched my own blog before posting. Here is the post I did five years ago which has pretty much the same script in it.

The saga of a lost connection

I came across an interesting problem at work a couple of days ago.  One of our CakePHP-based applications has a scheduled task (cronjob) that runs every minute and imports mail from a number of IMAP mailboxes into the database.  All of a sudden the script broke.  In a very particular way.  Not a single line of code was change in the script itself, and there were no significant changes in the rest of the application that the script is using.  But one day, for some reason, it started to import emails from only half of the mailboxes, completely ignoring the rest.

A lengthy midnight drunk debugging session helped to realize the problem.  Apparently, in the list of mailboxes to check there were a few old mailboxes, which were not used anymore for any email.  The script was checking them but they were always empty.  Server administrator removed those mailboxes during a scheduled maintenance window.  Now, the script couldn’t connect to the mailbox anymore.  No problem, there was error handling for this case.  But was error handling didn’t take into account is the combination of time it takes timeout on the non-existing mailbox check and the database connection timeout.

It so happened, that these few old mailboxes were right in the middle of the list of all mailboxes.  The script was importing mail from the first few mailboxes just fine.  But then while it was taking a long time to timeout on a few non-existing mailboxes, the database connection got closed due to the inactivity timeout.  This wasn’t handled properly.  The script was only checking for the existing database connection at the beginning, when it was opening the connection – not later on.  As the result, nothing could have been imported from the rest of the mailboxes.

As always, once you know the problem, you also know how to fix it or work around it.  But troubleshooting a problem like this is tricky.  Seeing the queries working for one mailbox and not working for another is disorienting at least.  Having no recent changes to the application doesn’t help either.  And the problem being not in the code itself, but in the resources is also not very obvious.

So, here you go.  If you ever have a weird problem like that – check that your resources (database connection, network connection, file handler) are still available to you.

Missing main menu bar in Gnome applications

I had this problem for quite some time now.  It’s been haunting me from computer to computer and from account to account.   I went through all configuration options I could find.  I started my Gnome and GTK profiles from scratch a few times.  And nothing seemed to help.  I even abandoned Gnome over this and switched to KDE and other desktop managers for some time – that’s how annoying it was.  And the worst thing, whenever I tried to Google for a solution – a totally different problem was coming up in the search results.

The problem was that in all of my Gnome applications main menu was missing.  The menu bar, which has File, Edit, View, Help, and such – never showed up.  Be that a video player or GnuCash or anything else.   Today I finally found a solution to this major annoyance.  Just in case the original will disappear, I quote for generations to come:

The problem was not in the .gnome or .gtk sub-directories, as I suspected.  Instead two packages installed were the problem.  gnome-globalmenu-common    and   gnome-applet-globalmenu were the culprits. There are two options if these are installed.  The first is to use the applet on one of the Gnome panels either top or bottom of hte screen.  This interesting applet will insert the ‘missing’ menu bar from the currently focused application into the bar the applet is a part of.  This will be VERY handy on the netbook, where real estate is an issue. The other option is to remove them.

Thank you, Daniel and mutk from #fedora IRC channel.  You sirs have saved my sanity.

Copy SSH key to remote machine

Those of us who use secure shell (SSH) for logging in to remote machines, already know about key authentication, which is so much easier and sometimes more secure than password authentication.  We also know that in order to make it work you need to:

  • generate a pair of keys with ssh-keygen command
  • copy public key from the local machine to authorized_keys file on the remote machine
  • fix the permissions of the .ssh/ folder and authorized_keys file on the remote machine

And that’s just what we have been doing.  Or at least me.  Today, after approximately 10 years of using secure shell, I’ve learned that there is a ssh-copy-id command, which will automatically add your current public key to a remote machine’s authorized_keys file and arrange for correct permissions.   Wow!

Thanks to @commandlinefu and top 10 one-liners blog post.

PHP date() and 53 weeks

Let’s say you have a bunch of statistical data.  And all that data is date-related.  And let’s say want to display that data on a chart, a weekly average or something along those lines.  One of the ways for you to place the value into the proper week would be something like this:

$week = date('W', strtotime($stats_date));
$values[$week][] = $stats_value;

And if you did it this way, sooner or later, you’d notice that something is not quite working right at the edges of your chart.  With code as simple and straight-forward as this, you’d probably look for the problem elsewhere.  Maybe it’s your statistical data which is wrong, or the graph is not generated properly.  But the problem is here.

How many weeks do you think there are in a year?  A common knowledge says 52.  However, if you think for a moment about how the weeks are related to the year, you’ll realize that the first and last weeks don’t necessary start and end at the edge of the year.  If you play around with 1st of January and 31st of December across several years, you’ll notice that sometimes they fall into the 53rd week.  (As do a few more days, not just these two).

And here, the problem with the “W” date() format starts to emerge.   When scattering your data across a single year, you’d most often expect January to start with the first week of the year.  But it doesn’t. Sometimes the start of it falls into the 53rd week of the previous year.  And date(‘W’, $your_time) will happily return 53.  What will this do to your chart?  Two things are most likely.  The first week’s values would get reduced, and the last week’s values would get increased.  Or those values of the first week would altogether vanish from the graph.  That is unless you are careful.  Which I hope you’ll now be.

See comments to PHP date() manual for several solutions of this problem.