Missing entry in Makefile.
[dcpomatic.git] / hacks / examine.py
1 #!/usr/bin/python
2
3 import subprocess
4 import shlex
5 import sys
6
7 AUDIO_STREAM = "1"
8 types = ['video']
9 VIDEO_RATE = 29.97
10
11 last_video = None
12 last_video_pts = None
13
14 last_audio_pts = {}
15 last_channels = {}
16
17 video_frame_count = 0
18
19 def check_pts_dts(frame):
20     diff = frame['pkt_pts_time'] - frame['pkt_dts_time']
21     if abs(diff) > 1e-8:
22         print("\tPTS != DTS: %f" % diff)
23
24 def handle(frame):
25     global last_video
26     global last_video_pts
27     global last_audio_pts
28     global video_frame_count
29     if frame['media_type'] == 'video' and 'video' in types:
30         if last_video_pts is not None and frame['pkt_pts_time'] <= last_video_pts:
31             print 'Out of order video frame %f (%d) is same as or behind %f (%d)' % (frame['pkt_pts_time'], frame['pkt_pts'], last_video_pts, last_video)
32         elif last_video_pts is not None:
33             print 'OK V    frame %f %f %f %f %d indices %d/%d' % (frame['pkt_pts_time'], frame['pkt_dts_time'], frame['pkt_pts_time'] - last_video_pts, 1 / (frame['pkt_pts_time'] - last_video_pts), frame['pkt_size'], video_frame_count, frame['pkt_pts_time'] * VIDEO_RATE)
34             check_pts_dts(frame)
35         else:
36             print 'OK V    frame %f counted %d' % (frame['pkt_pts_time'], video_frame_count)
37         last_video = frame['pkt_pts']
38         last_video_pts = frame['pkt_pts_time']
39         video_frame_count += 1
40     elif frame['media_type'] == 'audio' and 'audio' in types:
41         stream_index = frame['stream_index']
42         if stream_index in last_audio_pts and (stream_index == AUDIO_STREAM or AUDIO_STREAM is None):
43             print 'OK A[%s] frame %4.8f %4.8f %4.8f %4.8f %d' % (stream_index, frame['pkt_pts_time'], frame['pkt_dts_time'], frame['pkt_pts_time'] - last_audio_pts[stream_index], 1 / (frame['pkt_pts_time'] - last_audio_pts[stream_index]), frame['pkt_size'])
44             check_pts_dts(frame)
45         if stream_index in last_channels and frame['channels'] != last_channels[stream_index]:
46             print "\t*** unusual channel count"
47         last_audio_pts[stream_index] = frame['pkt_pts_time']
48         last_channels[stream_index] = frame['channels']
49
50
51 p = subprocess.Popen(shlex.split('ffprobe -show_frames "%s"' % sys.argv[1]), stdin=None, stdout=subprocess.PIPE)
52 frame = dict()
53 while True:
54     l = p.stdout.readline()
55     if l == '':
56         break
57
58     l = l.strip()
59
60     if l == '[/FRAME]':
61         handle(frame)
62         frame = dict()
63     elif l != '[FRAME]' and l != '[SIDE_DATA]' and l != '[/SIDE_DATA]':
64         s = l.split('=')
65         if s[0] in ['pkt_pts_time', 'pkt_dts_time', 'pkt_pts', 'pkt_dts']:
66             frame[s[0]] = float(s[1])
67         elif s[0] in ['channels', 'pkt_size', 'nb_samples']:
68             frame[s[0]] = int(s[1])
69         elif len(s) > 1:
70             frame[s[0]] = s[1]