Fix the filmsum script up a little.
[dcpomatic.git] / hacks / filmsum
1 #!/usr/bin/python3
2
3 import sys
4 import bs4
5 import termcolor
6
7 inside = False
8 xml = ''
9 for l in sys.stdin.readlines():
10     if l.startswith('<Metadata>'):
11         inside = True
12     elif l.startswith('</Metadata'):
13         inside = False
14     if inside:
15         xml += l
16
17 def note(k, v, highlight=None):
18     if highlight is not None and highlight(v):
19         print('%20s: %s' % (k, termcolor.colored(v, 'white', 'on_red')));
20     else:
21         print('%20s: %s' % (k, v))
22
23 def bool_note(k, v, highlight=None):
24     v = 'yes' if (v is not None and v.text == "1") else 'no'
25     note(k, v, highlight)
26
27 def dcp_time(s):
28     global dcp_rate
29     raw = int(s.text)
30     f = raw * dcp_rate / 96000.0
31     s = f // dcp_rate
32     f -= s * dcp_rate
33     m = s // 60
34     s -= m * 60
35     h = m // 60
36     m -= h * 60
37     return '%s DCP_%02d:%02d:%02d.%02d' % (str(raw).ljust(8), h, m, s, f)
38
39
40 def content_time_from_frames(s, r):
41     raw = int(s.text)
42     f = raw
43     s = f // r
44     f -= s * r
45     m = s // 60
46     s -= m * 60
47     h = m // 60
48     m -= h * 60
49     return '%s Con_%02d:%02d:%02d.%02d' % (str(raw).ljust(8), h, m, s, f)
50
51 soup = bs4.BeautifulSoup(xml, 'xml')
52 note('Name', soup.Metadata.Name.text)
53 note('Container', soup.Metadata.Container.text)
54 note('J2K bandwidth', soup.Metadata.J2KBandwidth.text, lambda x: int(x) < 20000000 or int(x) > 235000000)
55 note('Video frame rate', soup.Metadata.VideoFrameRate.text, lambda x: int(x) not in [24, 25, 30])
56 dcp_rate = int(soup.Metadata.VideoFrameRate.text)
57 note('Audio channels', soup.Metadata.AudioChannels.text)
58 bool_note('3D', soup.Metadata.ThreeD, lambda x: not x)
59 bool_note('Encrypted', soup.Metadata.ThreeD, lambda x: not x)
60 reel_types = ['single', 'by-video', 'by-length']
61 note('Reel type', reel_types[int(soup.ReelType.text)])
62 for c in soup.Metadata.Playlist.children:
63     if isinstance(c, bs4.element.Tag):
64         print()
65         note('  Type', c.Type.text)
66         note('  Position', dcp_time(c.Position))
67         note('  Trim start', c.TrimStart.text)
68         note('  Trim end', c.TrimEnd.text)
69         if c.VideoFrameRate:
70             note('  Video rate', c.VideoFrameRate.text)
71             note('  Video length', content_time_from_frames(c.VideoLength, float(c.VideoFrameRate.text)))
72         if c.AudioFrameRate:
73             note('  Audio rate', c.AudioFrameRate.text)
74         bool_note('  Reference video', c.ReferenceVideo, lambda x: not x)
75         bool_note('  Reference audio', c.ReferenceAudio, lambda x: not x)
76         bool_note('  Reference subtitle', c.ReferenceSubtitle, lambda x: not x)