vstinner / hachoir

Hachoir is a Python library to view and edit a binary stream field by field
http://hachoir.readthedocs.io/
GNU General Public License v2.0
613 stars 70 forks source link

Add possibility to get metadata within Python #50

Closed benbou8231 closed 3 years ago

benbou8231 commented 4 years ago

I was looking for a tool to get video metadata in Python for a project I work on. I found this thread: https://stackoverflow.com/q/51342429/5806912

So I took a look at the source, and I found an easy way to allow users to retrieve metadata within Python.

Here a snippet of how to call it with the proposed modification (might be useful for the documentation):

from hachoir.metadata.main import main
try:
    d = main(['--get-dict', 'PATH_TO_VIDEO.mkv'])
except SystemExit:
    pass
vstinner commented 4 years ago

Hi, here is an example how to use Hachoir to extract metadata: https://hachoir.readthedocs.io/en/latest/developer.html#hachoir-metadata-example

I don't think that the main() function should return a value different than an exit code, but I'm open for a new API which would simply such code. I suggest to add a new function instead.

benbou8231 commented 4 years ago

Hello,

That makes a lot of sense. I'll write a getMetadata function as soon I have a few minutes. I'll update this PR when it will be done.

Thanks

benbou8231 commented 4 years ago

With that new function we can get a list of dictionaries with the following code:

from hachoir.metadata.main import getMetadata
getMetadata(["--raw", "C:\\myVid.mkv"])
vstinner commented 3 years ago

I don't understand the use case, but I dislike the implementation. You should return a dict, not modify a global variable.