Python: Using Hachoir, how to extract metadata for file objects?

I am working on a site on which users upload video and audio files; when uploading, some common metadata fields must be filled out from the file. I found Hachoir and it seems good, but with a problem, to create a parser for reading metadata, you need a file name, not a file or stream object.

Right now I am using Django for web development, and I would like to continue using the FileStorage API, so files can be easily added to CDN.

How to use Hachoir with files like objects? sample code they provide work, but only for "real" files.

+5
source share
1 answer

Quick and dirty fragment:

from hachoir_core.error import HachoirError
from hachoir_core.stream import InputIOStream
from hachoir_parser import guessParser
from hachoir_metadata import extractMetadata


def metadata_for_filelike(filelike):
    try:
        filelike.seek(0)
    except (AttributeError, IOError):
        return None

    stream = InputIOStream(filelike, None, tags=[])
    parser = guessParser(stream)

    if not parser:
        return None

    try:
        metadata = extractMetadata(parser)
    except HachoirError:
        return None

    return metadata

Just need to improve error handling :)

+4
source

All Articles