Thu, 29 Mar 2007
Flash, ming, Python and ctypes...
You didn't think I was going to leave you without a dope beat to step to... I mean, having to use ming from C, did you? Of course not, I wouldn't be so cruel...
So, ming has a Python binding available as a separate download or from CVS but it's based on SWIG and not exactly actively maintained (for example no SWFVideoStream support). In fact, I did try to bring the SWIG binding up to date but it wasn't even clear what parameters were used to produce it in the first case...
Since any potential speed benefit from having a compiled C extension is probably not a major factor for ming and because I've had experience with ctypes before I decided I'd try using ctypes to create a Python binding for ming.
The cool thing about ctypes is that in the simplest cases once you've compiled the target library in the usual way you're ready to go. In addition, it's relatively straightforward to convert a chunk of C to the equivalent Python—it won't be pretty or Pythonic but will probably be functional.
A basic example
By way of a simple example based on test/Movie/new/test01.c: (Note: Change library name as appropriate.)
#!/usr/bin/python
#
# Requires `libming.0.4.0.dylib` in the current directory
# or in the search path.
#
from ctypes import *
libming = cdll.load("libming.0.4.0.dylib")
if __name__ == "__main__":
if libming.Ming_init() != 0:
raise Exception("Ming_init failed.");
movie = libming.newSWFMovie();
bytesout = libming.SWFMovie_save(movie, "test01.swf");
print "Bytes written:", bytesout
The result will be a valid, but boring .swf file.
More complicated stuff
Once we get much deeper into ming usage we start needing to do things like casting values and it gets somewhat unpleasant. Fortunately, there's a way of making that easier too—my intention is to return to that topic in a later post.
Posted at: 06:10 | category: / | | Comments ()
Compile ming Flash generation library on OS X (ming 0.4.0 beta 4)
libming is "a library for generating Macromedia Flash files (.swf), written in C". As of the current beta (0.4.0.beta4) it does not compile out of the box for me (OS X 10.4 PPC) so I thought I'd write up what worked for me.
For future reference, when trying to compile out of the box the following error occurs when trying to generate libming.0.4.0.dylib:
The problem seems to occur when linking against libfl, I'm not entirely clear why it happens but if you remove the library from the link process the problem "goes away"—seemingly with no ill effects—the small patch below implements this work around.ld: Undefined symbols: _yylex /usr/bin/libtool: internal link edit command failed
How to compile
It seems I had the necessary dependencies (such as libpng) already installed but if you don't you might want to follow the existing ming OS X installation instructions for that.
Okay, here's what you need to do:
Now we need to apply our patch osx_ming_0.4.0.b4.patch to workaround the error:wget http://dl.sourceforge.net/sourceforge/ming/ming-0.4.0.beta4.tar.bz2 tar xjvf ming-0.4.0.beta4.tar.bz2 cd ming-0.4.0.beta4
Then continue:wget http://words.rancidbacon.com/static/osx_ming_0.4.0.b4.patch patch < osx_ming_0.4.0.b4.patch
We then need to work around a permissions isssue:./configure
Finally, we get on with:chmod u+x config/install-sh
And that should be it...make
You can install the library now or run a quick test:
The last command should result in no output—if there are any differences things didn't go as planned...cd test/Movie/new/ make test01 ./test01 ../../../util/listswf test01.swf | diff -u test01.ref -
Hopefully things did go as planned however and you can now start playing with ming on OS X...
The future
I've been working with the ming people on #gnash and hopefully the fixes will be included before the next release. It appears this can be applied to the version in CVS to fix the flex library issue in the proper place:
--- configure.in~ 2007-02-22 07:32:11.000000000 +1300
+++ configure.in 2007-03-29 04:33:23.000000000 +1200
@@ -198,7 +198,6 @@
dnl --------------------------------------------
AC_PROG_YACC
-AM_PROG_LEX
AC_PROG_LIBTOOL
if test x"$LIBTOOL" = x; then
AC_MSG_ERROR([could not detect libtool, bailing out])
Posted at: 05:35 | category: / | | Comments ()