Wednesday, July 27, 2011

New Location

I've decided to move all my blogging activities to This will allow me to not only continue to write blog posts, but also to actually *build* the blog. It'll be a fun experiment with Google App Engine.

Tuesday, May 31, 2011

Nested Go Templates with Dtemplate

Lately I have been playing around with Google's relatively new programming language, Go. So far, I am fairly impressed with the language. It has been pretty easy to pick up. I started playing with it mostly because Google App Engine recently added support. The idea of a compiled language to run a web app seemed appealing. Especially in an environment like App Engine where you pay for CPU time.

While Go as a language is quite capable (and really easy to learn), the template package is somewhat lacking (imo). Specifically, there's no way to nest templates without resorting to a somewhat convoluted method outlined here:

In a web application, it is pretty vital to be able to nest templates. So I created dtemplate. A simple package to allow a user of Go to nest templates in a way that is very similar to how Django handles nested templates. Check out dtemplate on github: You'll find basic documentation on how to use it there as well.

I haven't yet specified a license, but feel free to use or modify as you please.

Tuesday, October 5, 2010

Automatic Video Conversion in Ubuntu

A friend of mine recently created an automated video conversion system using OS X’s Automator. It’s a simple system. He drops a video file in a folder, Compressor (via the Automator script) grabs the file which then converts it and drops the output somewhere predefined. For good measure, he set it up so that the folder being watched by the Automator script is also being synced by Dropbox. That way, he can add video files to be converted regardless of where he is or what computer he is on.

Being the true linux user that I am, upon hearing about this system, I immediately set out to replicate the functionality using Ubuntu. It turned out to be relatively simple. From a functional standpoint, it is identical. Here I am going to outline the steps required to set up a similar system. If everything is followed exactly, you’ll end with two folders for converting video files from just about any format into webm or ogv video files (depending on which folder you drop the source in).

This was all done on the Ubuntu 10.10 beta, but I don’t expect it to be much different on just about any version of Ubuntu or Debian.

The video conversion uses ffmpeg so here’s a quick note on ffmpeg: Ubuntu 10.10 has ffmpeg 0.6 in the repositories. Older version should be fine except that they may not have the same video codec support. The script below to convert video files to the webm format won’t work with anything less than 0.6, but the ogg script will probably work fine for any version of ffmpeg.

We will need something to watch the folder and respond when changes occur. For this I use Watcher. Watcher is a simple python daemon I wrote that watches folders for changes and responds accordingly. It is configured via a simple yaml file.

Here are the commands to get all files and folders in place:

mkdir ~/.watcher # used by watcher for config files
touch ~/.watcher/jobs.yml # main (only) config file for watcher
mkdir ~/Videos/to_ogv # folder for video files that will be converted to ogv
mkdir ~/Videos/to_webm # folder for video files that will be converted to webm
mkdir ~/Videos/processed # folder to store the original source video file
mkdir ~/Videos/converted # folder to store the output video files in webm or ogv format
mkdir ~/bin # this is where I keep all my home brewed scripts
touch ~/bin/to_ogv # the script to convert video files to ogv
touch ~/bin/to_webm # the script to convert video files to webm
Ok, now that all the files and folders are set up, let’s get to configuration.

First off, we’ll create the scripts to handle the actual conversion. Open up ~/bin/to_ogv in your favorite editor. Like this for gedit:

gedit ~/bin/to_ogv

Paste this as the contents of the file and save it (change /home/gregg to your home directory):

ffmpeg -i $1 -acodec libvorbis -ab 128000 -vcodec libtheora -b 761000 /home/gregg/Videos/converted/`date +%F--%k:%M:%S`.ogv
mv $1 /home/gregg/Videos/processed/

Do the same for ~/bin/to_webm, but paste this instead (change /home/gregg to your home directory):

ffmpeg -i $1 -acodec libvorbis -ab 128000 -vcodec libvpx -b 761000 /home/gregg/Videos/converted/`date +%F--%k:%M:%S`.webm
mv $1 /home/gregg/Videos/processed/

You can change the quality of the audio and video by adjusting the -ab and -b options in the ffmpeg lines. -ab is the audio bitrate and -b is the video bitrate. A higher value means higher quality and larger filesize.

Now let’s set up watcher.

gedit ~/.watcher/jobs.yml

Paste this (change /home/gregg to your home directory):

label: Convert to ogv
watch: /home/gregg/Videos/to_ogv
events: ['create', 'move_to']
recursive: false
command: /home/gregg/bin/to_ogv $filename

label: Convert to webm
watch: /home/gregg/Videos/to_webm
events: ['create', 'move_to']
recursive: false
command: /home/gregg/bin/to_webm $filename

Make sure all indentation is exactly two spaces.

Alright, all the configuration should be ready to go. Let’s start the daemon

./ start

If there were no errors, everything should be good. You can view the output with this:

tail -f /tmp/watcher_out

At this point, any video files dropped in ~/Videos/to_ogv or ~/Videos/to_webm will be automatically converted to the respective video type. All your source files will be droped in ~/Videos/processed (careful, it will overwrite filenames) and your converted video files will be in ~/Videos/converted.

If you're interested in the watcher script (it is extremely flexible), you can get the source and skim some documentation here:

It is also possible to replace watcher with incron. I consider it slightly more difficult to configure, but I originally had setup this system using incron.

Saturday, January 16, 2010

First Chrome Extension: Password Generator

I've created my first extension for Chrome. You can check it out here.

It was fun to build and I'm always taking suggestions :D

Thursday, January 7, 2010

Current Directory PHP

I always need this. Here's how to get the current directory of a file in php:

echo dirname(__FILE__);

Friday, December 11, 2009

Nvidia Updater in Python

I rewrote this script in python because I figured it'd be more cross platform. It require python 2.6.x and the python-beautifulsoup package in Ubuntu.

Everything runs pretty much identically in this python version as it does in the php version. I've supplied this second version mostly to help myself become more proficient in python.

You still have to install the driver manually because they require user input. This just makes it much easier to find out if a new version exists and then download the new version.

I'm just giving a link to this one because indentation matters in python, and as you can see in the php version below, blogspot isn't too kind to indentation.

and here's also a direct link to an updated php version

Wednesday, December 9, 2009

Nvidia Driver Updater for Linux

I've created a php script to automatically check the nvidia website and download a new driver if available. It's pretty simple how it works. Just run the script and it will do it's magic. The first time it is run, it will create a file called "current" in the same directory. Also, on the first run, it will always download the latest version of the driver in the same directory. After the first run, every other run will only download the driver if a new version is available.

This is mainly for those who are using nvidia drivers from outside the Ubuntu repository. If you're using the (often slightly old) driver in the repository, there is no need to use this script because Ubuntu will update it when ever the Ubuntu devs update the repo (I think each Ubuntu release comes with a new version of the driver). This is only for those who want the bleeding edge nvidia drivers and know how to install them manually.

I should note that you have to actually install the driver yourself as usual, this script just automates the download process and lets you avoid nvidia's click-fest of a website.

Just copy the code below into a text file and save it in your desired location as updater.php. From the command line, cd into the directory you save it and run 'php updater.php'. Better yet, set up a cron to run every few days or few hours to automatically download the updates.

function curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
return $result;

$site = curl('');

$doc = @DOMDocument::loadHTML($site);
$p = $doc->getElementsByTagName('p');
$node = $p->item(3)->nodeValue;
//$node = $p->item(5)->nodeValue; // uncomment this line for the 64-bit version

$del = explode("\n", trim($node));
$latest = trim(substr($del[0], strpos($del[0], ':')+2));
$current = (is_file('current')) ? trim(file_get_contents('current')) : 0;
if ($current != $latest) {
$url = "$latest/NVIDIA-Linux-x86-$";
system("wget -p $url");
file_put_contents('current', $latest);

I've only actually tested this in Ubuntu, but in theory, it should work elsewhere. You just need to have php5-cli installed. The code above is for the 32-bit version of the nvidia driver. Uncomment "$node = $p->item(5)->nodeValue;" and it will download the 64-bit version.