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. Alfredheque said,

    September 23, 2017 @ 03:01 am

    get the facts effexor

  2. viagra without a doctor prescription said,

    September 23, 2017 @ 03:03 am

    viagra without a doctor prescription low cost health care http://sildenafilwithoutadoctor.com/ viagra without a doctors prescription

  3. cheap nhl jerseys from China said,

    September 23, 2017 @ 03:54 am

    Thank you for sharing your thoughts. I really appreciate your efforts and I am waiting for your next write
    ups thank you once again.cheap nhl jerseys from China

  4. Kndtarcamn said,

    September 23, 2017 @ 04:12 am

    free viagra samples generic viagra buying viagra in canada [url=http://viagraveikd.com/]cheap viagra[/url] ’

  5. MoisesWap said,

    September 23, 2017 @ 04:21 am

    viagra shop preisvergleich

    [url=http://buycxviagra.com/]buy cheap viagra[/url]

    viagra generic

    low price viagra soft uk

  6. CharlesNeeri said,

    September 23, 2017 @ 05:13 am

    trazodone 100mg doxycycline tablet

  7. http://demos.gamer-templates.de said,

    September 23, 2017 @ 05:14 am

    This design is wicked! You certainly know how to keep a reader amused.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job.
    I really enjoyed what you had to say, and more than that, how you presented it.
    Too cool!

  8. MichaelInory said,

    September 23, 2017 @ 05:27 am

    diltiazem cream cosdiltiazem cold stone thailand online pharmacy prices price meaning of aceon

  9. MichaelInory said,

    September 23, 2017 @ 06:42 am

    brand name for minocycline hcl tb24 dr oz metformin plavix lawsuit settlements in 2010 soma herba trazodone side effects weight loss albenza liquid

  10. cialis buy said,

    September 23, 2017 @ 06:48 am

    cialis 5 mg bula
    http://cialisloq.com/
    cialis melhor que viagra
    [url=http://cialisloq.com/]http://cialisloq.com/[/url]
    cialis ricetta farmacia

  11. Nickenula said,

    September 23, 2017 @ 07:20 am

    loan cash free sex chatting

  12. AlenaWap said,

    September 23, 2017 @ 07:55 am

    just try! best cialis prices

    http://buycialiskj.com/ – cialis online

    buy cialis

    buy generic super cialis

  13. FACEBOOK MARKETING said,

    September 23, 2017 @ 08:05 am

    Heya i’m for the first time here. I came across this
    board and I find It truly useful & it helped me out a lot.
    I hope to give something back and help others like you aided me.

  14. Rolandgloca said,

    September 23, 2017 @ 09:46 am

    buy generic cialis uk
    cialis online
    mail order cialis canada
    buy cialis
    do cialis pills look like

  15. CharlesNeeri said,

    September 23, 2017 @ 10:16 am

    website Buy Cialis Overnight CEPHALEXIN 250 MG

  16. Casino Online said,

    September 23, 2017 @ 12:09 pm

    An impressive share! I’ve just forwarded this onto a coworker who had been doing a little research on this.
    And he in fact bought me lunch due to the fact that I stumbled upon it for him…
    lol. So allow me to reword this…. Thank YOU for the meal!!
    But yeah, thanx for spending time to discuss this topic here on your website.

  17. GoloaWap said,

    September 23, 2017 @ 13:21 pm

    we choice cialis pharmacy

    cialis

    buy cialis online

    try it original cialis

  18. MoisesWap said,

    September 23, 2017 @ 14:50 pm

    generic viagra in cabo san

    buy viagra

    cheap viagra

    viagra crema

  19. GoloaWap said,

    September 23, 2017 @ 15:21 pm

    by cheap cialis

    buy cialis online

    cialis canada

    prix cialis en belgiqu

  20. Nickenula said,

    September 23, 2017 @ 16:01 pm

    custom essay

  21. sverigapotek said,

    September 23, 2017 @ 17:29 pm

    pa apoteket priser, http://sverige-apotek.life/mutum.html , göteborg sweden.

  22. Tamika said,

    September 23, 2017 @ 17:53 pm

    I enjoy you because of each of your labor on thjis site.

    Kim delights in carryig out research annd it
    is easy to see why. I know all of the powerful ways you convey good thoughts byy means of
    the web blog and in addition recommend participation from other individuals on that subject plus our own daughter has always been being taught a reat
    deal. Have fun with the rest of the year. You are always doing a really great
    job. http://www.mv-agusta-club.de/links/link.aspx?url=atv-games.com%2Fprofile%2Flatonyaschi

  23. GoloaWap said,

    September 23, 2017 @ 21:12 pm

    cialis soft tab cheap

    cialis canada

    buy cialis online

    cialis non concedibile

  24. Nickenula said,

    September 24, 2017 @ 00:22 am

    payday loans online with bad credit

  25. GoloaWap said,

    September 24, 2017 @ 00:36 am

    discount cialis black online

    generic cialis

    generic cialis

    cialis generico brasil comprar

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...