Question

I'm working on a site that users uploads videos and audio files, I when uploaded, some common metadata fields must be populated from the file. I have found Hachoir and it seems good, but with a problem, to create a parser for metadata reading, what is required is a filename, rather than a file-like or stream object.

Right now I'm using Django for web development, and I would like to keep using the FileStorage API, so the files can be uploaded nicely to a CDN.

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

Was it helpful?

Solution

Quick and dirty snippet:

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 better error handling :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top