pythonmp3mime-typespython-magic

Get MP3 MIME type using python


I am writing a script to determine if a file is a valid MP3 using python-magic. With some files, the magic.from_file() function returns use count (30) exceeded. Is it possible to raise the limit similar to the command line program: file --parameter name=40? If this is not possible with python-magic, is it possible with filemagic?


Solution

  • After navigating my way through ctypes, I found a solution:

    import magic
    
    MAGIC_PARAM_NAME_MAX = 1                    # definition from magic.h
    name_max = magic.c_void_p(40)               # new use count, can also be c_size_t
    name_max_ref = magic.ctypes.byref(name_max)
    
    s = magic.Magic()
    magic.libmagic.magic_setparam(s.cookie, MAGIC_PARAM_NAME_MAX, name_max_ref)
    print s.from_file('file.jpg')