Wednesday, July 27, 2011
New Location
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: http://go.hokapoka.com/example/embedding-or-nesting-go-templates/.
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: https://github.com/greggoryhz/dtemplate. 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:
wget http://www.grehz.com/watcher.pyOk, now that all the files and folders are set up, let’s get to configuration.
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
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):
#!/bin/bash
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):
#!/bin/bash
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):
job1:
label: Convert to ogv
watch: /home/gregg/Videos/to_ogv
events: ['create', 'move_to']
recursive: false
command: /home/gregg/bin/to_ogv $filename
job2:
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
./water.py 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: http://github.com/greggoryhz/Watcher
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
Thursday, January 7, 2010
Current Directory PHP
echo dirname(__FILE__);
Friday, December 11, 2009
Nvidia Updater in Python
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.