Compiling zlib.lib on Windows

August 26th, 2010

zlib is the standard for lossless data compression. The DEFLATE compression algorithm is the basis for just about every lossless compression format out there, including “zip” and “gzip”, which is itself part of zlib.

There are two ways that it can be used from C/C++ projects in Windows.

Firstly, it can be used by dynamic linking (dll). This means using zdll.lib and shipping the appropriate version of zlib1.dll with your project. This is not a problem, as Windows versions of both of these files are provided.

The second way is to use static linking. That is, having all of the code in one .lib file and compiling it into your exe so that you do not have to distribute zlib1.dll. This means compiling zlib.lib.

In version 1.2.4 of zlib, a “projects” directory was provided, with a Microsoft Visual C++ 6 project. However, it seems that version 1.2.5 has not included this project. This means that the best solution is to go and get the 1.2.4 source and compile it yourself. However, the zlib project seems to be kept inside the libpng project on sourceforge.net, so it is not immediately obvious where to find older versions of the zlib source code.

zlib 1.2.4 source (zip)

Extract the zip, open projects\visualc6\zlib.dsp in Visual Studio (I used 2005) and compile “LIB Release” (and optionally “LIB Debug”)

Copy zlib.h and zconf.h from “include” to your Visual Studio “include” directory, and zlib.lib (and zlibd.lib if you made it) to your Visual Studio “lib” directory.

On 64 bit Windows, with Visual Studio 2005, this is “C:\Program Files (x86)\Microsoft Visual Studio 8\VC\” so adjust for your version of Visual Studio.

You now just need to add “zlib.lib” to your “Linker -> Input -> Additional Dependencies” line in your C++ project configuration to use it (and optionally zlibd.lib for the debug version).

Fix Windows 7 MBR after GRUB

August 24th, 2010

During a Linux install to my 2nd hard drive, GRUB was automatically installed to the wrong MBR (master boot record). The files were on the /boot of the Linux drive, but the Windows’ drive’s MBR was used. This meant without both drives, I could not boot either operating system.

So I installed GRUB manually with grub-install /dev/sdb. Now I needed to restore the original Windows 7 MBR so that it didn’t require GRUB to boot it.

The Windows 7 “startup repair” from the installation disc detected the OS but didn’t find any boot problems with it, so I went to the command prompt on the disc.

There are several commands for this. A lot of pages suggest using “bootsect”. However, this did not fix anything. The fact that it was for use on individual partitions was probably a clue on that. Some pages even used “bootcfg” which is for older Windows installs such as XP (it affects your boot.ini), but is still included on the Windows 7 disc to add to the confusion.

The command that did work was:

bootrec /fixmbr

Afterwards, for good measure, I also ran:

bootrec /fixboot

How to compile ffmpeg from source

June 9th, 2010

ffmpeg is a free and very powerful video encoding tool that is the basis of many other popular video encoding tools, with a command line interface.

Because of the frequent updates to ffmpeg and the codecs it uses, it is often best to download the latest source and compile it yourself. These updates usually include new features, bug fixes, support for new codecs and noticeable speed improvements. Most Linux distributions have outdated packages in their repositories so being able to compile the latest code from source is very useful

I use ffmpeg to output H.264 video with AAC audio in a MP4 container. I also occasionally convert audio to mp3. This means I will be compiling the following:

Preparing to compile on Linux

(If you already compile things on Linux, skip to the next section.)
First make sure that you have all of the tools and headers required to compile on Linux. On Debian based systems (including Ubuntu) you can usually run the following as root to get everything you need. First update your system. It is important that you get the latest kernel and the correct sources for it.

apt-get update
apt-get dist-upgrade

A reboot may be required here if your kernel updated.

Now get the compilers, tools and headers needed to compile software on Linux:

apt-get install build-essential linux-headers-`uname -r` git subversion yasm

NB: your “git” package may be called something like “git-core”

Compile ffmpeg (empty)

Because some libraries require ffmpeg to be installed, we will first install a blank copy with no additional codecs specified.

When I update, I often find that I need to recompile a blank version of ffmpeg before I can compile x264.

Download the latest ffmpeg source with subversion, configure, compile:

svn checkout svn://svn.ffmpeg.org/ffmpeg/trunk ffmpeg
cd ffmpeg
./configure
make

If this all worked with no errors (you can check with echo $?), run

make install

as root. Use su or sudo if you need to. I use su.

Compile faac and faad

Download and extract faac and faad from audiocoding.com.

As above, run

./configure
make
su -c 'make install'

on both libraries.

You can optionally do the same thing with LAME if you want to be able to encode mp3 audio.

Compile x264

Download and compile the latest x264 source with git:

git clone git://git.videolan.org/x264.git
cd x264
./configure
make
su -c 'make install'

Compile ffmpeg with the above codecs

Now that we have all of the codecs (x264, aac, mp3) successfully installed as libraries on the system, we can reinstall ffmpeg to incorporate these libraries. First go back to the ffmpeg source directory and clean off the previous compile.

cd ffmpeg
make clean

Now configure to enable all of the installed libraries and recompile:

./configure --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
make

(if it tells you that –enable-libfaad is not an option, just remove it. It’s probably either been rolled into faac or ffmpeg has it built in)

If it all worked, run make install as root.

su -c 'make install'

Testing

As long as /usr/local/bin/ is on your $PATH environment variable, you should be able to just type ffmpeg to check that it is all installed. It should look something like this:

$ ffmpeg
FFmpeg version SVN-r23548, Copyright (c) 2000-2010 the FFmpeg developers
  built on Jun  9 2010 13:35:17 with gcc 4.4.4
  configuration: --enable-gpl --enable-nonfree --enable-pthreads --enable-libx264 --enable-libfaac --enable-libfaad --enable-libmp3lame
  libavutil     50.18. 0 / 50.18. 0
  libavcodec    52.75. 1 / 52.75. 1
  libavformat   52.68. 0 / 52.68. 0
  libavdevice   52. 2. 0 / 52. 2. 0
  libavfilter    1.20. 0 /  1.20. 0
  libswscale     0.11. 0 /  0.11. 0
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
 
Use -h to get full help or, even better, run 'man ffmpeg'

Using ffmpeg

I have written a small Python video encoding script to make encoding as easy as

enc -t cd file.vob

If anyone has any suggestions to improve this, such as any advantages of using shared libraries, please feel free to post these suggestions in the comments.

Remote recursive sha1sum with php

June 2nd, 2010

To calculate the SHA-1 sums, display them and make them available for download in a sums.gz file:

<?php echo(`find ./some_directory/ -type f | grep -v sums.gz | xargs sha1sum | gzip -c | tee sums.gz | zcat`); ?>

To check the sums:

<?php echo(`zcat sums.gz | sha1sum -c -`); ?>

Get external IP address with Python

May 26th, 2010

Here’s a quick snippet of Python code (tested in 3.0) to quickly look up your external IP address over HTTP and display it:

import urllib.request
print(str(urllib.request.urlopen("http://www.whatismyip.com/automation/n09230945.asp").read(), "utf8"))

Sign language scammers

May 21st, 2010

Sometimes I think these people are just taking the piss to see who will notice.

Courier notifications

March 26th, 2010

Most couriers know where their drivers are at all times. They should update their tracking shortly before delivery, so that people are ready to collect at the door. Email or SMS notification 30 minutes before delivery would be very useful. Some couriers currently phone before they get there to make sure that you are in before they even bother delivering, but that just doubles the chance for you to miss them because you have to be there for both the phone call and the delivery.

Websites that already offer SMS notifications and links to tracking websites could easily be notified by a courier’s tracking API and send out a SMS accordingly.

This would probably save a fortune in redelivery costs to couriers too.

Just a thought.

Xbox 360 – false economy

March 23rd, 2010

The main thing that caused me to buy an Xbox 360 rather than a PlayStation 3 was the price, as it probably is with a lot of people. However, now I am starting to regret that decision.

To buy a 360 with HDMI, you need to get the most expensive version, the Elite, which is £200. On top of that, it doesn’t play Blu-ray so if you want that (and you will, eventually) add another £130 for a LG BD370, then you later find out how bad the media streaming is on the 360, something which you will probably also want eventually. Things like a complete lack of subtitles and surround sound audio causing the media to error completely rather than even play without sound or down-convert to stereo (on the same codec, AAC, which does work). This means you end up needing an additional piece streaming hardware. I chose the xtreamer for another €99 (£89).

Thats without even having any wireless options.

Total price comes out at about £420, compared to the 120GB PlayStation 3 Slim at £250. That’s almost double the price for the same, or fewer, features.

wp-syntax to look like Visual Studio

March 23rd, 2010

wp-syntax is a nice plugin for WordPress using GeSHi to produce syntax highlighted blocks of code.

The default colours aren’t very nice though, and there is no way to easily change them. wp-syntax-colorizer (horrible name) makes it easier to set the colours, but defaults to even worse colours.

Most of us want readable colours that we are used to from IDEs, and most of us will be using either Visual Studio or Eclipse. I mainly use the former. To get it to use the visual studio colours I edit wp-syntax-colorizer as follows:

function my_custom_geshi_styles(&$geshi)
{
  $overall = "black";
  $keyword = "blue";
  $literal = "maroon";
  $comment = "green";
 
  $geshi->set_overall_style("color: $overall;", true);
 
  $geshi->set_keyword_group_style(1, "color: $keyword;", true);
  $geshi->set_keyword_group_style(2, "color: $keyword;", true);
  $geshi->set_keyword_group_style(3, "color: $keyword;", true);
  $geshi->set_keyword_group_style(4, "color: $keyword;", true);
 
  $geshi->set_symbols_style("color: $overall;", true);
  $geshi->set_methods_style(1, "color: $overall;", true);
  $geshi->set_regexps_style(1, "color: $overall;", true);
 
  $geshi->set_strings_style("color: $literal;", true);
  $geshi->set_numbers_style("color: $literal;", true);
 
  $geshi->set_comments_style(1, "color: $comment;", true);
  $geshi->set_comments_style('MULTI',"color: $comment;", true);
}

Remove Size bbcode from phpbb

March 4th, 2010

One of the most annoying things about phpBB is the ability for people to randomly use [size="200"]Huge[/size] BB code, with no easy way to remove it and every time you update between versions it comes back.

To disable it:
in /include/bbcode.php remove the whole

case 5:
...
break;

section.

To remove it from the posting page:
in /styles/prosilver/template/posting_buttons.html remove the whole

<select>
...
</select>

section.

After that, delete your “cache” directory so that it uses the new copy.

Remember that you have to repeat this process after every update to phpBB3.