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 ()