Archive

Archive for the ‘I write code’ Category

Turning mkv into mp4 for the PS3 [UPDATED]

February 21st, 2010 Fighter Hayabusa 1 comment

Quite a lot of acronyms there don’t ya think? ;-)

Anyway… The PS3 is fantastic. I think most people with half a brain will agree on that. It does however have it’s shortcomings and one of them is that it doesn’t play mkv-files. So whenever I stumble upon something in that format I have to perform some sort of black magic on it in order to turn it into an mp4 that the PS3 agrees with.

The easy way to go is to just flush it through HandBrake. That works but then it will be decoded and re-encoded which potentially causes quality degradation and takes foreeeeeever. So that’s not really an option I’m happy with.

However, decoding and re-encoding is actually unnecessary since what’s wrong is the container and not the contents. Luckily unpacking and repackaging the mkv as mp4 can all be done with a bunch of free and open source tools. It’s still a bit of a hassle though so today I spent a couple of hours putting together a script that makes it semi-automatic and a lot easier.

One thing that proved to be a bit more work than I expected was turning DTS 5.1 audio into AAC 5.1. What I came up with should work, but I haven’t been able to verify it since my receiver doesn’t support AAC 5.1. If yours does, please let me know if it works or not. For this reason in my personal version of the script I turn DTS 5.1 into AAC stereo instead (using the last part of the if-elif-else statement), which is OK even if it’s not quite as nice as 5.1 sound would have been.

All of the tools I used are open source software and available for most Linux-flavors (I use Fedora 12), except Nero AAC Encoder which is just free as in beer.

So that nobody else has to suffer through the process of putting a script like this together; here’s the script. It’s fairly self-explanatory and I even commented the code a bit so it shouldn’t be that hard to understand what it does and why. Enjoy!

Download the script here!

While putting together the script I found this article very useful for deciphering the mess that is audio codecs. I would also like to give a shout-out to one of my co-workers who introduced me to several of the tools and how they work.

UPDATE:
In further experimenting I found that my original script contained a couple of bugs. I believe I’ve sorted those out now and I’ve updated the code above. I’m still not claiming this script to be bug-free so if you find anything that’s not working, please let me know.

Finding what’s after a specific string in a text-file

November 7th, 2009 Fighter Hayabusa No comments

Have you ever wanted to find a specific string in a log-file and then output what comes after that line in the file? I do this every single day at work and can’t be the only one. Somewhere in a huge log-file, XML-file or other type of text-file there’s a line that equals the beginning of some specific type of information that I’m in need of so I need to find that line of text and output a bunch of the lines following it.

I might for example have a backup cron-job that when it starts outputs “Backup started” to /var/log/messages and then follows that with output concerning how the backup went. Sure, theoretically I could open the file in vim and simply search for the line in question but sometimes text-files are frikkin’ huge and opening them up in a text-editor is just not a viable option. For example, I handle XML-files that are several gigabytes in size daily and loading one of those into an editor will mean nothing but pain and suffering I assure you ;-)

So to facilitate this procedure I slapped together this script, which I’m giving the easy-to-remember name fsagsl.sh:

#!/bin/bash
# fsagsl.sh Find String And Grab Some Lines
# 2009 (c) IDontGiveASmegWhatYouDoWithIt License
# by FighterHayabusa <fighterhayabusa@barbedwirebytecodebaconburger.com>
# Finds a string in a text-file and then outputs
# a chosen number of lines from that position onwards.

function is_int()
{
[ "$1" -eq "$1" ] > /dev/null 2>&1
return $?
}

PRG=fsagsl.sh

if [ -f $3 ] && is_int $2; then
LINESTART=`grep -n $1 $3 | sed -e 's/\([0-9]*\):.*/\1/g'`
for L in $LINESTART; do
let LINESTOP=$L+$2
sed -n "${L},${LINESTOP}p" $3
done
exit 0
else
echo "ERROR: Invalid parameters."
echo "Usage: ./$PRG string_to_find number_of_lines filename"
echo "Example: ./$PRG \"Backup started\" 5 /var/log/messages"
exit 1
fi

As you may notice the script also has some simple error-handling and  handles if there are multiple instances of the string that is being looked for.

So there you go. I hope somebody finds it useful :-)

Migrating an old MS Access database to MySQL [UPDATED]

August 3rd, 2009 Fighter Hayabusa No comments

I’m in the process of migrating an old website I made almost 8 years ago from it’s current unbearable platform of Windows, ASP and MS Access to some sort of *nix, PHP and MySQL. Tonight I decided to combat that MS Access database and transform it into a real database. To help me accomplish this I used the excellent tools provided by the mdbtools project.

First I had to export the schema of the database to a format readable by MySQL:

mdb-schema -S mydb.mdb mysql > mydb.sql

This creates an SQL-file containing the schema in plain readable SQL. Very nice.

Next I had to export the actual data from the tables in the MS Access database, like this:

TABLES=`mdb-tables mydb.mdb`
for T in $TABLES; do
mdb-export -I -R';\n' mydb.mdb $T >> mydb.sql
done

The first line stores the names of all the tables in the variable TABLES. Then I simply loop through all of them and execute the mdb-export command on all of them, writing the output to the end of the schema-file.

In case I need to do this again in the future I put it all together in a script like this:

#!/bin/bash
DBFILE=$1
OUTFILE=$2

#Check for correct number of arguments
if [ ! $# -eq 2 ]; then
echo "Usage: access2mysql.sh DBFILE OUTPUTFILE"
echo "Example: access2mysql.sh msaccess.mdb mysql.sql"
exit 1
fi
#Check that DBFILE really exists
if [ ! -f $DBFILE ]; then
echo "$DBFILE does not exist."
exit 1
fi

#All is good, here we go!

#Create schema
mdb-schema -S $DBFILE mysql > $OUTFILE

#Export table data
TABLES=`mdb-tables $DBFILE`
for T in $TABLES; do
mdb-export -I -R';\n' $DBFILE $T >> $OUTFILE
done
#Clean up some Windows-character stuff
dos2unix $OUTFILE
exit 0

Now all I have to do is create a MySQL-database and write my newly created SQL-script into it and I’m good to go!

UPDATE:
Before I could get the SQL-script to work I had to fire off a couple of sed-statements to fix the fact that I had used some illegal words as names for some of the table-columns, “type” and “condition” to be specific. No biggie though. Also, mdb-schema didn’t add “AUTO_INCREMENT” to the fields that were auto incrementing in the MS Access database so I had to do that manually.

Crowdsourcing your iTunes music with PHP, AppleScript and Twitter

February 20th, 2009 Fighter Hayabusa No comments

The other day I was catching up on the last few episodes of Hak5, one of my favorite internet-TV shows, and in one episode Darren Kitchen was doing some neat tricks with PHP, Twitter and VLC. This got me thinking about what other kinds of fun projects Twitter could be used for.

Twitter is a great service, not very reliable however (fail whale anyone?), and there are already plenty of mash-ups that uses it for various things. My idea was to use Twitter to select what plays in iTunes. People could send me replies with songs in them and if the song was in my iTunes-library it would be played, as simple as that. Not very useful, but a fun experiment if nothing else. So I started coding.

In order to control iTunes I had to delve into AppleScript for the first time. I find it to be an odd language, but whatever, for this project it’s definitely the most appropriate choice for getting quick results. One of my favorite languages is PHP so that’s what I chose for the control structure of the whole thing.

A couple of hours later I had my first prototype and now an additional few hours of tweaks I give you… TwitTunes!

This is how it works:
Person #1 starts TwitTunes on his Mac. TwitTunes sends a tweet - “#TwitTunes starting” - using Person #1s Twitter-account to let the world know that it is running.
Person #2 sends a reply on Twitter containing search words. These search words are then used to search through Person #1s iTunes-library and the first matching song found is played.

Not very fancy and far from perfect, but admit it, crowdsourcing your jukebox via Twitter is a pretty nifty idea and this is my very first proof of concept ;-)

If you wanna try out TwitTunes yourself you can download it here (sloppy source code included of course), and if you have any feedback I’ll be happy to hear it.

Updated my Twitter-widget

September 4th, 2007 Fighter Hayabusa No comments

If anybody gives a shit I updated my Twitter-widget tonight. It now parses URLs in the text and turns them into links, and I also tweaked the CSS a bit.

You can download it from the same ‘ole place. Enjoy!

Twitterer and widget maker

July 17th, 2007 Fighter Hayabusa No comments

The past couple of days I’ve been playing around with Twitter and it’s API. It’s pretty simple to use, hardly astrophysics or anything, and with the widget for Blogger looking like ass I decided to hack my own. And so I did.

The fruits of my labour are plainly visible to the right in this blog. It’s nothing fancy, just some basic HTML, CSS and Javascript, but I think it looks pretty good. And if anybody gives a damn the code can be downloaded from here.

In the days ahead I’m gonna pleasure myself (oooh…) by writing a Twitter-applet for the Gnome desktop as well. Now don’t misinterpret the sudden focus on Twitter. I don’t think I’ll ever be an avid twitterer (is that a word? probably not) and I’m not exactly falling head over heels with excitement over the thing. I just wanna play around with some free API:s, and for that purpose this one is as good as any. Also, anything that allows me to interact using the command line gets a “yay!” from me.

curl -u someaccount@somedomain.com:somepassword \
-d status=Posting+to+Twitter+using+curl+is+teh+shit%21 \
http://twitter.com/statuses/update.json

Spicing up your e-mail signature for fun and… well… fun!

March 21st, 2007 Fighter Hayabusa 1 comment

Recently I got tired of my e-mail signature always looking the same. It bored me but I’m a bit too lazy to be changing it myself very often. So I figured out a way to spice it up a little and make it update itself automatically.

The tools I used for this tiny little project was the neat little application fortune and cron.

If you’re using any sort of real operating system you already have cron installed. Chances are that you have fortune as well, but if you don’t it shouldn’t be very hard to find from some repository or other download source. If you are, like me, a Fedora Core user you will find it in the extras repository.

What fortune does is that it outputs a random quote taken from an installed database of quotes. There are plenty of funny and interesting quotes available for inclusion in this database, such as quotes taken from movies, books and TV-shows.

So I simply wrote a bash-script that takes a base signature, which in my case contains some contact information and the URL to my GnuPG public key, adds this to the output from fortune and stores it all in a simple text file. The resulting file is what I then tell my e-mail client to use for a signature.

The script looks like this:

#!/bin/bash
SIGN_DIR=/home/hayabusa/documents
rm -f $SIGN_FILE/mailsignature
cat $SIGN_DIR/basesignature > $SIGN_DIR/mailsignature
fortune >> $SIGN_DIR/mailsignature
exit 0

So as you can see, nothing fancy at all.

Then to make the signature change once every fifteen minutes I added this row to my /etc/crontab:

*/15 * * * * hayabusa /home/hayabusa/bin/mailsign_maker.sh >/dev/null

Now let’s see for how long this keeps me “not bored” with my e-mail signature ;-)
Oh, and by the way, I realize I am far from the first that’s done something like this. I just figured I might not be the last wanting to do it either.