blob: d9cb670aba2a333fecb44882cc19bd1598838f40 [file] [log] [blame]
kjellanderd2b63cf2017-06-30 03:04:59 -07001#!/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
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000012import shutil
kjellanderd2b63cf2017-06-30 03:04:59 -070013import subprocess
14import sys
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000015import tempfile
kjellanderd2b63cf2017-06-30 03:04:59 -070016
17
18SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
19
20# Chrome browsertests will throw away stderr; avoid that output gets lost.
21sys.stderr = sys.stdout
22
23
24def _ParseArgs():
25 """Registers the command-line options."""
26 usage = 'usage: %prog [options]'
27 parser = optparse.OptionParser(usage=usage)
28
29 parser.add_option('--label', type='string', default='MY_TEST',
30 help=('Label of the test, used to identify different '
31 'tests. Default: %default'))
32 parser.add_option('--ref_video', type='string',
33 help='Reference video to compare with (YUV).')
34 parser.add_option('--test_video', type='string',
35 help=('Test video to be compared with the reference '
36 'video (YUV).'))
37 parser.add_option('--frame_analyzer', type='string',
38 help='Path to the frame analyzer executable.')
Paulina Hensmanb671d462018-09-14 11:32:00 +020039 parser.add_option('--aligned_output_file', type='string',
40 help='Path for output aligned YUV or Y4M file.')
kjellanderd2b63cf2017-06-30 03:04:59 -070041 parser.add_option('--barcode_decoder', type='string',
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000042 help=('Path to the barcode decoder script. By default, we '
43 'will assume we can find it in barcode_tools/'
44 'relative to this directory.'))
kjellanderd2b63cf2017-06-30 03:04:59 -070045 parser.add_option('--ffmpeg_path', type='string',
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000046 help=('The path to where the ffmpeg executable is located. '
47 'If omitted, it will be assumed to be present in the '
48 'PATH with the name ffmpeg[.exe].'))
kjellanderd2b63cf2017-06-30 03:04:59 -070049 parser.add_option('--zxing_path', type='string',
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000050 help=('The path to where the zxing executable is located. '
51 'If omitted, it will be assumed to be present in the '
52 'PATH with the name zxing[.exe].'))
kjellanderd2b63cf2017-06-30 03:04:59 -070053 parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt',
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000054 help=('Path to the temporary stats file to be created and '
55 'used for the reference video file. '
56 'Default: %default'))
kjellanderd2b63cf2017-06-30 03:04:59 -070057 parser.add_option('--stats_file_test', type='string',
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000058 default='stats_test.txt',
59 help=('Path to the temporary stats file to be created and '
60 'used for the test video file. Default: %default'))
kjellanderd2b63cf2017-06-30 03:04:59 -070061 parser.add_option('--stats_file', type='string',
62 help=('DEPRECATED'))
63 parser.add_option('--yuv_frame_width', type='int', default=640,
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000064 help='Width of the YUV file\'s frames. Default: %default')
kjellanderd2b63cf2017-06-30 03:04:59 -070065 parser.add_option('--yuv_frame_height', type='int', default=480,
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000066 help='Height of the YUV file\'s frames. Default: %default')
Edward Lemur2e5966b2018-01-30 15:33:02 +010067 parser.add_option('--chartjson_result_file', type='str', default=None,
68 help='Where to store perf results in chartjson format.')
kjellanderd2b63cf2017-06-30 03:04:59 -070069 options, _ = parser.parse_args()
70
Magnus Jedvert3e169ac2018-08-24 12:44:59 +000071 if options.stats_file:
72 options.stats_file_test = options.stats_file
73 print ('WARNING: Using deprecated switch --stats_file. '
74 'The new flag is --stats_file_test.')
75
kjellanderd2b63cf2017-06-30 03:04:59 -070076 if not options.ref_video:
77 parser.error('You must provide a path to the reference video!')
78 if not os.path.exists(options.ref_video):
79 parser.error('Cannot find the reference video at %s' % options.ref_video)
80
81 if not options.test_video:
82 parser.error('You must provide a path to the test video!')
83 if not os.path.exists(options.test_video):
84 parser.error('Cannot find the test video at %s' % options.test_video)
85
86 if not options.frame_analyzer:
87 parser.error('You must provide the path to the frame analyzer executable!')
88 if not os.path.exists(options.frame_analyzer):
89 parser.error('Cannot find frame analyzer executable at %s!' %
90 options.frame_analyzer)
91 return options
92
93def _DevNull():
94 """On Windows, sometimes the inherited stdin handle from the parent process
95 fails. Workaround this by passing null to stdin to the subprocesses commands.
96 This function can be used to create the null file handler.
97 """
98 return open(os.devnull, 'r')
99
Magnus Jedvert3e169ac2018-08-24 12:44:59 +0000100def DecodeBarcodesInVideo(options, path_to_decoder, video, stat_file):
101 # Run barcode decoder on the test video to identify frame numbers.
102 png_working_directory = tempfile.mkdtemp()
103 cmd = [
104 sys.executable,
105 path_to_decoder,
106 '--yuv_file=%s' % video,
107 '--yuv_frame_width=%d' % options.yuv_frame_width,
108 '--yuv_frame_height=%d' % options.yuv_frame_height,
109 '--stats_file=%s' % stat_file,
110 '--png_working_dir=%s' % png_working_directory,
111 ]
112 if options.zxing_path:
113 cmd.append('--zxing_path=%s' % options.zxing_path)
114 if options.ffmpeg_path:
115 cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path)
116
117
118 barcode_decoder = subprocess.Popen(cmd, stdin=_DevNull(),
119 stdout=sys.stdout, stderr=sys.stderr)
120 barcode_decoder.wait()
121
122 shutil.rmtree(png_working_directory)
123 if barcode_decoder.returncode != 0:
124 print 'Failed to run barcode decoder script.'
125 return 1
126 return 0
127
kjellanderd2b63cf2017-06-30 03:04:59 -0700128def main():
129 """The main function.
130
131 A simple invocation is:
Paulina Hensmanb671d462018-09-14 11:32:00 +0200132 ./webrtc/rtc_tools/compare_videos.py
kjellanderd2b63cf2017-06-30 03:04:59 -0700133 --ref_video=<path_and_name_of_reference_video>
134 --test_video=<path_and_name_of_test_video>
135 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
Magnus Jedvert3e169ac2018-08-24 12:44:59 +0000136
137 Notice that the prerequisites for barcode_decoder.py also applies to this
138 script. The means the following executables have to be available in the PATH:
139 * zxing
140 * ffmpeg
kjellanderd2b63cf2017-06-30 03:04:59 -0700141 """
142 options = _ParseArgs()
143
Magnus Jedvert3e169ac2018-08-24 12:44:59 +0000144 if options.barcode_decoder:
145 path_to_decoder = options.barcode_decoder
146 else:
147 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools',
148 'barcode_decoder.py')
149
150 if DecodeBarcodesInVideo(options, path_to_decoder,
151 options.ref_video, options.stats_file_ref) != 0:
152 return 1
153 if DecodeBarcodesInVideo(options, path_to_decoder,
154 options.test_video, options.stats_file_test) != 0:
155 return 1
156
kjellanderd2b63cf2017-06-30 03:04:59 -0700157 # Run frame analyzer to compare the videos and print output.
158 cmd = [
159 options.frame_analyzer,
160 '--label=%s' % options.label,
161 '--reference_file=%s' % options.ref_video,
162 '--test_file=%s' % options.test_video,
Magnus Jedvert3e169ac2018-08-24 12:44:59 +0000163 '--stats_file_ref=%s' % options.stats_file_ref,
164 '--stats_file_test=%s' % options.stats_file_test,
165 '--width=%d' % options.yuv_frame_width,
166 '--height=%d' % options.yuv_frame_height,
kjellanderd2b63cf2017-06-30 03:04:59 -0700167 ]
Edward Lemur2e5966b2018-01-30 15:33:02 +0100168 if options.chartjson_result_file:
169 cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file)
Paulina Hensmanb671d462018-09-14 11:32:00 +0200170 if options.aligned_output_file:
171 cmd.append('--aligned_output_file=%s' % options.aligned_output_file)
kjellanderd2b63cf2017-06-30 03:04:59 -0700172 frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(),
173 stdout=sys.stdout, stderr=sys.stderr)
174 frame_analyzer.wait()
175 if frame_analyzer.returncode != 0:
176 print 'Failed to run frame analyzer.'
177 return 1
178
179 return 0
180
181if __name__ == '__main__':
182 sys.exit(main())