Installing ffmpeg on FreeBSD (or other Unix or Linux distributions)

May 3rd, 2011

Installing ffmpeg on FreeBSD couldn’t be simpler. Just type the following command:

pkg_add -r ffpmpeg

And that should install ffmpeg along with audio and video codecs and other dependencies.

Most likely, other Linux or Unix-based systems provide similar ways to install ffmpeg but unfortunately this is not well documented.


Setting up and using the Auth module in Kohana 3.1

April 16th, 2011

The documentation about the Auth module in Kohana 3.1 is severely lacking, and the module has changed sufficiently that existing tutorials and documentations are no longer relevant.

So below is a quick tutorial on how to setup and use the module:

1) Enable the modules

If not done already, enable the needed modules in bootstrap.php. In particular, auth, database and ORM need to be enabled:

<?php
Kohana::modules(array(
    'auth'       => MODPATH.'auth',       // Basic authentication
    // 'cache'      => MODPATH.'cache',      // Caching with multiple backends
    // 'codebench'  => MODPATH.'codebench',  // Benchmarking tool
    'database'   => MODPATH.'database',   // Database access
    // 'image'      => MODPATH.'image',      // Image manipulation
    'orm'        => MODPATH.'orm',        // Object Relationship Mapping
    // 'unittest'   => MODPATH.'unittest',   // Unit testing
    // 'userguide'  => MODPATH.'userguide',  // User guide and API documentation
    ));
?>

2) Setup the database

– Copy the configuration file from modules/database/config/database.php to application/config/database.php
– Open it and set username, password, etc.
– Create the database schema using the SQL file in modules/orm/auth-schema-mysql.sql

You should now have a working database with all the required tables.

3) Setup the Auth module

– Copy the Auth config file from modules\auth\config\auth.php to your config folder in application/config.
– Open this file and change the driver to “ORM” and set a hash key. It can be any random value such as those generated by WordPress.
– Your file should then look like this:

<?php defined('SYSPATH') or die('No direct access allowed.');

return array(

    'driver'       => 'ORM',
    'hash_method'  => 'sha256',
    'hash_key'     => "4b 8?((~FKnpD))>8kb!B |#-uXIO24G3rc:&MG+FR{x;r#Uq4k{Ef@F4E9^-qS!",
    'lifetime'     => 1209600,
    'session_key'  => 'auth_user',

    // Username/password combinations for the Auth File driver
    'users' => array(
        // 'admin' => 'b3154acf3a344170077d11bdb5fff31532f679a1919e716a02',
    ),

);

In order for the Auth module to work, you must also set the cookie salt variable. So add this at the end of your bootstrap.php file:

Cookie::$salt = 'somerandomstring';

4) How to register a user

Once the database and Auth are setup, it is relatively easy to add a user to the database, although there are some pitfalls (see comments below). Here is the minimum code required to add a user:

<?php
$client = ORM::factory('user');
$client->email = "my@email.com";

/* Note that the username cannot contain
   certain characters such as"." or "@".
   If it does "$client->save()" is going
   to crash, and the error message is not
   helpful */
$client->username = "laurent";

/* Auth is going to automatically hash
   the password when saving the user,
   so don't do it manually */
$client->password = "mypa55word";

$client->save();
?>

After saving the user, assign a role to him (see this post comments).

5) Example login

Once this is all done, login a user is straightforwards:

<?php
$r = Auth::instance()->login("laurent", "mypa55word");
?>

You can also check if a user is currently logged in and, if so, retrieve it from the database:

<?php
$loggedIn = Auth::instance()->logged_in();
if ($loggedIn) {
    $user = Auth::instance()->get_user();
} else {
    echo "User is not logged in";
}
?>

For more information, have a look at the Auth Module API documentation.


Using Gettext with the Qt Framework

June 14th, 2010

The Qt Framework for some reason doesn’t provide any built-in support for Gettext, the quasi-standard for translating software programs. An alternative is provided, Qt Linguist, however there are several reasons why some may still prefer Gettext:

  • If your application is already partially translated using Gettext it’s much easier to keep on using it rather than converting all your mo/po files to Qt’s format.
  • Another significant advantage of Gettext is that it doesn’t have any dependencies (unlike Qt Linguist which creates a dependency to the Qt Framework) and thus allows creating more portable code.

So for these reasons, I’ve created a small library which parses and manages Gettext catalogues in Qt. The library includes a generic parser and a catalogue manager which can be used in any C++ project. On top of that, a Qt wrapper is provided for easy integration with the Qt framework.

The library can be downloaded from the Google Code project:

Download the Gettext library for the Qt Framework

Setting up and using the library is easy:

#include <QtGettext.h>

QtGettext::instance()->setLocale("cn_TW"); // Set the locale
QtGettext::instance()->setCatalogueName("catalogue"); // Set the name of the mo files
QtGettext::instance()->setCatalogueLocation("Data/Locales"); // Set the catalogue folder

QtGettext is now set to use the file in Data/Locales/cn_TW/catalogue.mo.

Also note that if this file is not available, QtGettext will try to load Data/Locales/cn/catalogue.mo (if available) as an alternative.

To mark a string for translation, simply call:

QtGettext::instance()->getTranslation("Some string to be translated");

Since this is rather long, a _() macro is also provided. It shortens the code to just:

_("Some string to be translated");

Get the URL of an iPod or HD quality YouTube video

March 19th, 2009

I’ve described in a previous post how to get the URL of a YouTube video in Flash. However this method only returns the low-resolution quality video at 320×240. Getting the high-res one is easy though – you just need to add a “fmt” parameter to the URL link.

These two values in particular can be interesting:

fmt=22: This is an MP4 video in HD quality. On my tests, the resolution can go up to 1280 x 720.

fmt=18: This is a 480 pixels wide version that can be used in portable devices such as iPods.

In order to use this additional parameter, just add an argument to the constructFLVURL method:

// Set "format" to 22 for the high resolution version, or 18 for the iPod one. Or just
// leave to -1 for the low res version.
private function constructFLVURL (video_id:String, t:String, format:Number = -1):String {
  var str:String = "http://www.youtube.com/get_video.php?";
  str += "video_id=" + video_id;
  str += "&t=" + t;
  if (format >= 0) {
    str += "&fmt=" + format;
  }
  return str;
}

Delete a file to the recycle bin from C++

November 14th, 2008

Today, I was looking for a way to delete a file to the recycle bin using C++, but couldn’t find any simple example, so here it is:

SHFILEOPSTRUCT operation;
operation.wFunc = FO_DELETE;
operation.pFrom = "c:\\file\to\delete.txt";
operation.fFlags = FOF_ALLOWUNDO;

SHFileOperation(&operation);

Copyright © Pogopixels Ltd, 2008-2018