ctypes

El otro día me encontré con un módulo interesante de python que no conocía: ctypes. Sirve para comunicarse con librerías en C: permite cargar librerías dinámicas y llamar a sus funciones, definir tipos equivalentes a los de C para pasarlos de un lado a otro e incluso especificar funciones python como callbacks de C. Vamos, que se pueden crear bindings a librerías externas escribiendo únicamente código python.

Ahi va un ejemplillo tonto usando la libavformat:

#!/usr/bin/python
import sys
from ctypes import *
import ctypes.util

av = CDLL(ctypes.util.find_library("avformat"))
filename = sys.argv[1]
# pFormatCtx deberia ser un puntero a AVFormatContext, 
# pero esto es solo un ejemplo simple :)
pFormatCtx = c_void_p() 

av.av_register_all()
av.av_open_input_file(byref(pFormatCtx), filename, None, 0, None)
av.dump_format(pFormatCtx, 0, filename, 0)

Por supuesto para hacer algo más serio habría que declarar en python las estructuras como AVFormatContext que se definen en las cabeceras correspondientes y cosas así, pero entonces el programa de ejemplo sería demasiado engorroso como para leerlo en un blog ;). El ejemplo muestra una salida como esta:

$ ./av.py test.avi 
Input #0, avi, from 'test.avi':
  Duration: N/A, bitrate: N/A
  Stream #0.0: Video: mpeg4, 512x420,   inf fps(c)
  Stream #0.1: Audio: vorbis, 22050 Hz, stereo, 24 kb/s

Referencias:

3 Comments »

  1. 123 movies said,

    July 27, 2017 @ 18:28 pm

    Do you have a spam problem on this blog; I also am a blogger,
    and I was wanting to know your situation; we have created some nice practices and we are looking to trade techniques with other folks,
    please shoot me an email if interested.

  2. TelinWap said,

    July 27, 2017 @ 19:12 pm

    only here viagra fast delivery

    online viagra

    viagra

    dove acquistare viagra

  3. TelinWap said,

    July 27, 2017 @ 19:18 pm

    consumo de viagra

    buy viagra

    viagra

    tranny generic viagra

  4. MoisesWap said,

    July 27, 2017 @ 20:12 pm

    half price female cialis

    http://buycialisuonline.com/ – online cialis

    buy cheap cialis

    can you get generic cialis.

  5. BrianSleld said,

    July 27, 2017 @ 21:16 pm

    nifigasebe.net

  6. RobertbuB said,

    July 27, 2017 @ 21:34 pm

    nifigasebe.net

  7. DarrellBit said,

    July 27, 2017 @ 21:36 pm

    nifigasebe.net

  8. MoisesWap said,

    July 27, 2017 @ 21:38 pm

    cialis tadalafil 10 20mg

    buy cheap cialis

    buy cheap cialis online

    cialis venta libr

  9. JoshuaCom said,

    July 27, 2017 @ 21:42 pm

    nifigasebe.net

  10. 123 movies said,

    July 27, 2017 @ 21:59 pm

    Thanks on your marvelous posting! I certainly enjoyed reading it, you’re
    a great author.I will be sure to bookmark your blog
    and will eventually come back later in life. I want to encourage you to ultimately continue your great
    job, have a nice holiday weekend!

  11. DarrellDaymn said,

    July 27, 2017 @ 22:06 pm

    nifigasebe.net

  12. Anthonyjoype said,

    July 27, 2017 @ 22:11 pm

    nifigasebe.net

  13. MoisesWap said,

    July 27, 2017 @ 22:14 pm

    cialis sales 3 days

    http://buycialisuonline.com/ – generic cialis

    cheap cialis online

    buy cialis in 3 days

  14. Michaelinhes said,

    July 27, 2017 @ 23:05 pm

    cheaper car insurance auto insurance

    aaa michigan accc insurance company

    classic auto insurance

  15. KennethMop said,

    July 27, 2017 @ 23:09 pm

    wh0cd227148 fluoxetine 0.05 tretinoin cream tadalafil

  16. TelinWap said,

    July 27, 2017 @ 23:13 pm

    tom kaulitz overdose da viagra

    viagra online

    viagra cheap

    free viagra sample of

  17. MoisesWap said,

    July 27, 2017 @ 23:13 pm

    venta de cialis en canada

    generic cialis online

    buy cheap cialis

    cialis without a script

  18. sildenafil said,

    July 28, 2017 @ 00:26 am

    how to buy cheap viagra online
    sildenafil citrate
    generic viagra cheap no prescription
    [url=http://viagratko.com/]sildenafil online[/url]
    buy viagra at walmart

  19. appnana codes hack 2016 said,

    July 28, 2017 @ 00:38 am

    Czasem wymaga się od nas jedynie pobrania jakiegoś produktu, znacznie częściej bywa jednak tak, że APPNana premiuje nas punktami
    wtedy, gdy dokonamy skromnych zakupów lub zarejestrujemy gdzieś numer telefonu.

  20. TelinWap said,

    July 28, 2017 @ 00:44 am

    herbal viagra retailers

    [url=http://buyfviagraonline.com/]viagra[/url]

    online viagra

    viagra get

  21. LIzaorisa said,

    July 28, 2017 @ 01:03 am

    Buy cheap Mobic online without prescription | Visa

  22. TelinWap said,

    July 28, 2017 @ 01:03 am

    viagra by buy

    [url=http://buyfviagraonline.com/]buy viagra[/url]

    cheap viagra

    comprare viagra in thailandi

  23. MoisesWap said,

    July 28, 2017 @ 01:20 am

    cialis daily 5mg best price uk

    buy cialis

    buy cheap cialis

    cialis sales in canada

RSS feed for comments on this post · TrackBack URI

Leave a Comment

This blog is protected by Dave\'s Spam Karma 2: 17770 Spams eaten and counting...