import re
from sys import argv

print argv[1]
with open(argv[1], 'r') as f:
    # parse summary until 1st AU is found
    summary = {}
    AU = []
    for line in f:
        match = re.search(r'(Summary of Access Unit #\s)(\d*)(\s+)', line)
        if match:
            AU.append({})
            AU[-1]['ID'] = int(match.group(2))
            break;
        match = re.search(r'(.*)=(.*)', re.sub(r'\s+', '', line))
        if match:
            summary[match.group(1)] = match.group(2)

    # parse AUs
    for line in f:
        match = re.search(r'(Summary of Access Unit #\s)(\d*)(\s+)', line)
        if match:
            AU.append({})
            AU[-1]['ID'] = int(match.group(2))
        match = re.search(r'(.*?)=(-*\d+)(.*)', re.sub(r'\s+', '', line))
        if match:
            AU[-1][match.group(1)] = match.group(2)
        match = re.search(r'(PictureType)=(\w+)', re.sub(r'\s+', '', line))
        if match:
            AU[-1][match.group(1)] = match.group(2)

    count = {}
    count['IPicture'] = 0
    count['PPicture'] = 0
    count['BPicture'] = 0
    count['UPicture'] = 0
    size = {}
    size['IPicture'] = 0
    size['PPicture'] = 0
    size['BPicture'] = 0
    size['UPicture'] = 0
    for l in AU:#[1170:1300]:
        count[l['PictureType']] += 1
        size[l['PictureType']] += int(l['PictureSize'])
#        print 'AU', l['ID'], l['PictureType'], l['PictureSize']
    totalCount = 0
    totalSize = 0
    for k in size:
        totalCount += int(count[k])
        totalSize += int(size[k])
    print 'Total', totalCount, totalSize
    print 'I', count['IPicture'], size['IPicture'], int(size['IPicture']) * 100 / totalSize,'%'
    print 'P', count['PPicture'], size['PPicture'], int(size['PPicture']) * 100 / totalSize,'%'
    print 'B', count['BPicture'], size['BPicture'], int(size['BPicture']) * 100 / totalSize,'%'
    print 'U', count['UPicture'], size['UPicture'], int(size['UPicture']) * 100 / totalSize,'%'

    for key in summary.keys():
        print key, ' = ', summary[key]
