Skip to main content

Building HandBrake on OS X and linking it from Qt

·2 mins

I’ve spent hours today on this.

Firstly, building libhandbrake from source on OS X. Here’s the steps that finally worked:

# get the source
git clone [email protected]:HandBrake/HandBrake.git
cd HomeBrew

# install some dependencies with Homebrew. There may be others, but these are the ones I didn't already have
brew install libtool cmake automake yasm

# configure, without xcode and using libtool installed above (renamed to glibtool to avoid a clash)
./configure --disable-xcode --libtool=/usr/local/bin/glibtool

cd build

# run make. This will fail...
make

# edit contrib/fontconfig/fontconfig-2.8.0/install-sh, which tries to install itself, and add `exit;` somewhere near the top to prevent it from running
vi contrib/fontconfig/fontconfig-2.8.0/install-sh

# make again, which will hopefully succeed!
make

Then, linking it and all its dependencies from my Qt project.:

# from the root of my Qt project, bring in the libs
mkdir external
cp -R ../HandBrake/build/{libhb,contrib} external/

Then add the following to your .pro file:

macx {
    # Add libhandbrake
    LIBS += -L$$PWD/external/libhb/ -lhandbrake
    PRE_TARGETDEPS += $$PWD/external/libhb/libhandbrake.a

    INCLUDEPATH += \
        $$PWD/external/libhb \
        $$PWD/external/contrib/include
    DEPENDPATH += \
        $$PWD/external/libhb \
        $$PWD/external/contrib/include

    # Add everything from ./external/contrib/lib/
    LIBS += -L$$PWD/external/contrib/lib/
    SHARED_LIB_FILES = $$files($$PWD/external/contrib/lib/*.a)
    for(FILE, SHARED_LIB_FILES) {
        BASENAME = $$basename(FILE)
        FILENAME = $$replace(BASENAME,lib,)
        LIBS += -l$$replace(FILENAME,\.a,)
        PRE_TARGETDEPS += $$FILE
    }

    # Add OSX frameworks and libraries
    LIBS += \
        -framework CoreAudio \
        -framework AudioUnit \
        -framework AudioToolbox \
        -framework IOKit \
        -framework CoreFoundation \
        -liconv \
        -lbz2 \
        -lz
}

A lot of trial and error went in to populating the list of system frameworks and libraries it depends on…

If all goes well, you should now be able to use libhandbrake from your project:

#import "hb.h"

void hb_error_handler( const char *errmsg )
{
    qCritical("HB ERROR : %s", errmsg);
}

int main(int argc, char *argv[])
{
    hb_global_init();
    hb_register_error_handler(&hb_error_handler);
}

Phew! Now I’m hoping I can use libhandbrake to make it easier to import my DVDs into iTunes, so I can view them on my iPad and Apple TV. But its pushing midnight, so thats for another day…