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

    February 21, 2018 @ 05:07 am

    generic sildenafil citrate canada
    viagra prices
    buy discount viagra online
    [url=http://fvviagrayrtuedf.com/#]viagra price[/url]
    can you buy viagra in canada over the counter
    viagra generic
    safe to order generic viagra online
    [url=http://dmviagratruykfj.com/#]generic viagra online[/url]
    generic viagra online fast delivery

  2. Neitunny said,

    February 21, 2018 @ 05:14 am

    http://paydaysonlinemoney.com/
    payday loans without credit check
    online loans
    cash loans near me
    payday loans online

  3. 8 vw autoradio dvd gps navigation said,

    February 21, 2018 @ 05:18 am

    I visit every day a few web sites and blogs to read articles
    or reviews, but this weblog offers feature based writing.

  4. carinsurance said,

    February 21, 2018 @ 05:20 am

    cheapest car insurance top rated auto insurance companies 2017 cheapest car insurance [url=https://cheapestcarinsurance.us.org]top rated auto insurance companies 2017[/url]

  5. Dog Ate Homework said,

    February 21, 2018 @ 05:23 am

    cover letters professional cover letter cover letter for employment [url=https://coverletters.us.com]make a cover letter[/url]

  6. Android Phones said,

    February 21, 2018 @ 05:26 am

    Hi there, You’ve done a great job. I’ll definitely digg it and personally suggest to my friends.

    I am confident they will be benefited from this site.

  7. PhillipNap said,

    February 21, 2018 @ 05:28 am

    viagra sale lloyds pharmacy
    viagra on line
    viagra 50 mg compresse
    [url=http://fvviagrayrtuedf.com/#]buy viagra online[/url]
    can you buy viagra in tijuana
    generic viagra online
    do need 50mg 100mg viagra
    [url=http://dmviagratruykfj.com/#]generic viagra[/url]
    viagra for men online india

  8. Loans Online said,

    February 21, 2018 @ 05:32 am

    installment loans online loans online installment loans online [url=https://loansonline.store]loans online[/url]

  9. Write Essays For Me said,

    February 21, 2018 @ 05:34 am

    dissertation writer paper writer writing expository essays [url=https://writers.us.com]essay writers[/url]

  10. Undits said,

    February 21, 2018 @ 05:39 am

    http://paydaysonlinemoney.com/
    personal loans las vegas
    payday loans
    secured loan rates
    online loans

  11. Easy Payday Loan said,

    February 21, 2018 @ 05:45 am

    installment loans online direct lender installment loans installment loans online [url=https://installmentloansonline.us.com]installment loans no credit check[/url]

  12. Lelandked said,

    February 21, 2018 @ 05:54 am

    how hard is it to get viagra in canada
    viagra on line
    ordering viagra online forum
    [url=http://fvviagrayrtuedf.com/#]online viagra[/url]
    pfizer brand viagra online
    generic viagra online
    viagra buy online no prescription uk
    [url=http://dmviagratruykfj.com/#]generic viagra online[/url]
    how to get viagra from dr

  13. Essay Writer Online said,

    February 21, 2018 @ 05:57 am

    essay writers essay writers writers [url=https://writers.us.com]essay writers[/url]

  14. vehicle insurance said,

    February 21, 2018 @ 06:02 am

    cheap car insurance thegeneral.com auto insurance cheap auto insurance [url=https://cheapcarinsurance.us.org]online car insurance quotes[/url]

  15. usaa auto insurance said,

    February 21, 2018 @ 06:22 am

    cheap auto insurance quotes online auto insurance quotes michigan the general car insurance florida [url=https://autoinsurancequotesonline.us.com]low car insurance[/url]

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