Skip to content

Using The mp4analyser Package In Automated Scripting

essential61 edited this page Jan 1, 2021 · 3 revisions

This repo hosts a complete MP4 analyser application. The application is split into 2 parts:

  1. An MP4 file parser, that inspects an MP4 file and produces as its output, a nested Python dictionary that describes the box or atom structure of the file and the data values within those boxes. This is the mp4analyser package.
  2. A TKinter GUI Front-End that allows the end user to navigate round the box structure and view data values as desired.

There is only a loose-coupling between the 2 parts and it is quite possible to consider applications where only the mp4analyser package is needed. For example, an automated script that can check multiple files in a directory and determine whether they are a valid iso bmff.

import glob
# mp4analyser is the package that actually parses the mp4 file
import mp4analyser.iso


mydir = "/run/media/steven_home/elements4TB/"
# get all files with 3 letter suffixes e.g .mp4, .txt, .wmv etc.
file_list = glob.glob(mydir + "*.???")
print('file_list {}'.format(file_list))

for fname in file_list:
    try:
        mp4boxtree = mp4analyser.iso.Mp4File(fname)
    except Exception as e:
        print("type error: " + str(e))
        print("unable to parse {0} ".format(fname))
        continue

    # valid ISO bmff files should contain an ftp or styp box at the top-level of the box structure
    # (of course this doesn't guarantee that the media is actually playable...) 
    if len([box for box in mp4boxtree.child_boxes if box.type == 'ftyp' or box.type == 'styp']):
        print("{0} looks valid iso bmff".format(fname))
        # print box structure
        print_out_boxes(mp4boxtree)
    else:
        print("{0} looks invalid bmff".format(fname))

In the case above, if a file looks like a valid iso bmmf, a function print_out_boxes() is called which outputs the box structure of the file.

# iterate through boxes
def print_out_boxes(mp4filetree):
    for b in mp4filetree.child_boxes:
        print("{0} at position {1:d}".format(b.type, b.start_of_box))
        for c in b.child_boxes:
            print("+ {0} at position {1:d}".format(c.type, c.start_of_box))
            for d in c.child_boxes:
                print("++ {0} at position {1:d}".format(d.type, d.start_of_box))
                for e in d.child_boxes:
                    print("+++ {0} at position {1:d}".format(e.type, e.start_of_box))
                    for f in e.child_boxes:
                        print("++++ {0} at position {1:d}".format(f.type, f.start_of_box))
                        for g in f.child_boxes:
                            print("+++++ {0} at position {1:d}".format(g.type, g.start_of_box))
Clone this wiki locally