Search code examples
pythonstreammetadatahachoir-parser

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


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.

How to use Hachoir with file like objects?


Solution

  • Using Hachoir v3.2.1:

    import hachoir.metadata
    import hachoir.parser
    import hachoir.stream
    
    parser = hachoir.parser.guessParser(hachoir.stream.InputIOStream(file_handle, None, tags=[]))
    if parser:
        hachoir_metadata = hachoir.metadata.extractMetadata(parser)
        if hachoir_metadata:
            metadata: Dict[str, str] = hachoir_metadata.exportDictionary()['Metadata']
            print(metadata)