blob: ee8e6d51220725acfdda56f1c9303ca1e396fa45 [file] [log] [blame]
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +00001#!/usr/bin/env python
2# Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
10import optparse
11import os
12import subprocess
13import sys
14
15
16SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
17
phoglund@webrtc.org84002462013-07-29 11:01:03 +000018# Chrome browsertests will throw away stderr; avoid that output gets lost.
19sys.stderr = sys.stdout
20
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000021
22def _ParseArgs():
23 """Registers the command-line options."""
24 usage = 'usage: %prog [options]'
25 parser = optparse.OptionParser(usage=usage)
26
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000027 parser.add_option('--label', type='string', default="MY_TEST",
28 help=('Label of the test, used to identify different '
29 'tests. Default: %default'))
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000030 parser.add_option('--ref_video', type='string',
31 help='Reference video to compare with (YUV).')
32 parser.add_option('--test_video', type='string',
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000033 help=('Test video to be compared with the reference '
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000034 'video (YUV).'))
35 parser.add_option('--frame_analyzer', type='string',
36 help='Path to the frame analyzer executable.')
37 parser.add_option('--stats_file', type='string', default='stats.txt',
38 help=('Path to the temporary stats file to be created and '
39 'used. Default: %default'))
40 parser.add_option('--yuv_frame_width', type='int', default=640,
41 help='Width of the YUV file\'s frames. Default: %default')
42 parser.add_option('--yuv_frame_height', type='int', default=480,
43 help='Height of the YUV file\'s frames. Default: %default')
44 options, _args = parser.parse_args()
45
46 if not options.ref_video:
47 parser.error('You must provide a path to the reference video!')
48 if not os.path.exists(options.ref_video):
49 parser.error('Cannot find the reference video at %s' % options.ref_video)
50
51 if not options.test_video:
52 parser.error('You must provide a path to the test video!')
53 if not os.path.exists(options.test_video):
54 parser.error('Cannot find the test video at %s' % options.test_video)
55
56 if not options.frame_analyzer:
57 parser.error('You must provide the path to the frame analyzer executable!')
58 if not os.path.exists(options.frame_analyzer):
59 parser.error('Cannot find frame analyzer executable at %s!' %
60 options.frame_analyzer)
61 return options
62
63
64def main():
65 """The main function.
66
67 A simple invocation is:
68 ./webrtc/tools/barcode_tools/compare_videos.py
69 --ref_video=<path_and_name_of_reference_video>
70 --test_video=<path_and_name_of_test_video>
71 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
72
73 Notice that the prerequisites for barcode_decoder.py also applies to this
74 script. The means the following executables have to be available in the PATH:
75 * zxing
76 * ffmpeg
77 """
78 options = _ParseArgs()
79
80 # Run barcode decoder on the test video to identify frame numbers.
81 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools',
82 'barcode_decoder.py')
kjellander@webrtc.orgc0b4c4a2013-10-02 15:04:45 +000083
84 # On Windows, sometimes the inherited stdin handle from the parent process
85 # fails. Work around this by passing null to stdin to the subprocesses.
86 null_filehandle = open(os.devnull, 'r')
87
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000088 cmd = [
89 sys.executable,
90 path_to_decoder,
91 '--yuv_file=%s' % options.test_video,
92 '--yuv_frame_width=%d' % options.yuv_frame_width,
93 '--yuv_frame_height=%d' % options.yuv_frame_height,
94 '--stats_file=%s' % options.stats_file,
95 ]
kjellander@webrtc.orgc0b4c4a2013-10-02 15:04:45 +000096 barcode_decoder = subprocess.Popen(cmd, stdin=null_filehandle,
97 stdout=sys.stdout, stderr=sys.stderr)
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000098 barcode_decoder.wait()
99 if barcode_decoder.returncode != 0:
phoglund@webrtc.org84002462013-07-29 11:01:03 +0000100 print 'Failed to run barcode decoder script.'
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000101 return 1
102
103 # Run frame analyzer to compare the videos and print output.
104 cmd = [
105 options.frame_analyzer,
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000106 '--label=%s' % options.label,
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000107 '--reference_file=%s' % options.ref_video,
phoglund@webrtc.org7bcc7e32013-06-27 14:05:26 +0000108 '--test_file=%s' % options.test_video,
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000109 '--stats_file=%s' % options.stats_file,
110 '--width=%d' % options.yuv_frame_width,
111 '--height=%d' % options.yuv_frame_height,
112 ]
kjellander@webrtc.orgc0b4c4a2013-10-02 15:04:45 +0000113 frame_analyzer = subprocess.Popen(cmd, stdin=null_filehandle,
114 stdout=sys.stdout, stderr=sys.stderr)
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000115 frame_analyzer.wait()
116 if frame_analyzer.returncode != 0:
phoglund@webrtc.org84002462013-07-29 11:01:03 +0000117 print 'Failed to run frame analyzer.'
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000118 return 1
119
120 return 0
121
122if __name__ == '__main__':
123 sys.exit(main())