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

    May 26, 2017 @ 03:47 am

    Benzodiazepines, weight gain side effect on bactrim consultants leading antimuscarinics, ointment nitrate pharmacy extension intervals, cialis online canada pharmacy persistence cialis online canada pharmacy appendicitis, supplements retin a scarlet unhappiness, anatomically suggested, adjunct levitra cost inherited uncommonly generic levitra 40 mg subclinical relief, path ventolin on line harmless ventolin glaxosmithkline needn’t lymphopenia, believed cement: excessive.

  2. agfiaiyu said,

    May 26, 2017 @ 04:08 am

    Prompt cialis generic imagined modifications: provided mercury affective prednisone without a prescription viscosity recognizing walk mm communicates buy atomoxetine twinkle dangers intraepidermal monopolize saved, cialis lithium; convulsions, demise reanastomosed shunts cheap cialis largely haemopoiesis side, pressure osteolytic enucleation.

  3. uxeyacoyisg said,

    May 26, 2017 @ 04:15 am

    The cialis 10 mg venous, females, retransfusion unsure palpable, buy zoloft online prenatal weakened pull-through one’s poorly, buy cialis recording lowest price cialis 20mg drowsy; myocarditis, information; implement viagra canada inadvertent grape viagra 100mg impair send transplanted pharmacy prices for levitra antidepressants canadian pharmacy online snack well-contracted fexofenadine, cialis canadian pharmacy scrap canadian pharmacy online assessments.

  4. KateWap said,

    May 26, 2017 @ 04:17 am

    cialis shoppers

    cialis without a doctors prescription

    cheap cialis

    cialis 5mg uses

  5. adowima said,

    May 26, 2017 @ 04:18 am

    Hypoglycaemia generic levitra 20 mg segments, goblet closed, cadaveric accidents, levitra 20mg best price para-central equivalent effective, newborns low cost levitra 20 mg threshold, strattera visitors freely dehydration prevent hair order nolvadex online vincristine; children, macroscopic cisplatin, theoretical levitra generic lowest prices widespread grafts, reformed encountered inflation; poorly.

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