11

I am looking for a simple library, compatible with Python 3.7, which can extract metadata of video files, specifically the capture/recording datetime; the date and time when the video was shot. I am mainly looking to do this on .mov files. hachoir-metadata has no Python library as far as I'm aware; only a command-line interface, and enzyme works only on .mkv files, though this isn't clearly stated in the description. The reason I want to retrieve the recording/capture datatime as a string is that I want to put this in the filename.

Before this question is marked as duplicate: similar questions are either unanswered or outdated. I am honestly puzzled as to why there isn't a proper way to retrieve video metadata in a Python script yet.

  • 2
    Do you have any links to those outdated questions? We need to clean those up... – Aran-Fey Jul 14 '18 at 18:51
  • 2
    This user asks for a library, yet the answer only provides the command-line tool Hachoir: https://stackoverflow.com/questions/4969497/video-meta-data-using-python. The provided script to this questions yielded an error for me, and I simply copy-pasted it with slight adaptations to fit my script (specifically, I got "struct.error: unpack requires a buffer of 4 bytes". Not sure what this means, but the error isn't mentioned in the thread). The answer to this question is also useless: https://stackoverflow.com/questions/39743494/reading-and-parsing-windows-video-files-metadata-in-python. –  Jul 14 '18 at 19:16
  • I wrongly recall being directed to expired Bitbucket documentation, but I found those URLs on sites other than StackOverflow, so I was wrong about other questions being "outdated". In my search for those questions, though, I did find this question with an expired Bitbucket link: https://stackoverflow.com/questions/30386343/how-to-extract-encoded-by-from-mp3-metadata-using-python. Actually, this might be the question I remember having an expired link: https://stackoverflow.com/questions/3742583/read-exe-msi-and-zip-file-metadata-in-python-in-linux/4944285. –  Jul 14 '18 at 19:16
  • 1
    Thanks. I'm afraid most of those are off-topic. We don't do recommendations here on SO. – Aran-Fey Jul 14 '18 at 19:20

2 Answers2

12

FFMPEG is a suitable library for this.

Installation: pip3 install ffmpeg-python

How to use:

import ffmpeg
vid = ffmpeg.probe(your_video_address)
print(vid['streams'])
Arashsyh
  • 609
  • 1
  • 10
  • 16
2

I have not found a nice library for Python but using hachoir with subprocess is a dirty workaround. You can grab the library itself from pip, the instructions for Python 3 are here: https://hachoir.readthedocs.io/en/latest/install.html

def get_media_properties(filename):

    result = subprocess.Popen(['hachoir-metadata', filename, '--raw', '--level=3'],
        stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

    results = result.stdout.read().decode('utf-8').split('\r\n')

    properties = {}

    for item in results:

        if item.startswith('- duration: '):
            duration = item.lstrip('- duration: ')
            if '.' in duration:
                t = datetime.datetime.strptime(item.lstrip('- duration: '), '%H:%M:%S.%f')
            else:
                t = datetime.datetime.strptime(item.lstrip('- duration: '), '%H:%M:%S')
            seconds = (t.microsecond / 1e6) + t.second + (t.minute * 60) + (t.hour * 3600)
            properties['duration'] = round(seconds)

        if item.startswith('- width: '):
            properties['width'] = int(item.lstrip('- width: '))

        if item.startswith('- height: '):
            properties['height'] = int(item.lstrip('- height: '))

    return properties

hachoir supports other properties as well but I am looking for just those three. For the mov files I tested it also appears to print out the creation date and modification dates. I am using a priority level of 3 so you can try playing with that to see more stuff.

Roman
  • 774
  • 7
  • 12