blob: c4a70c20fed23085c86a1344d4b8ddde95f1af63 [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
phoglund@webrtc.org3260f102013-11-25 14:10:20 +000012import shutil
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000013import subprocess
14import sys
phoglund@webrtc.org3260f102013-11-25 14:10:20 +000015import tempfile
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000016
17
18SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
19
phoglund@webrtc.org84002462013-07-29 11:01:03 +000020# Chrome browsertests will throw away stderr; avoid that output gets lost.
21sys.stderr = sys.stdout
22
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000023
24def _ParseArgs():
25 """Registers the command-line options."""
26 usage = 'usage: %prog [options]'
27 parser = optparse.OptionParser(usage=usage)
28
phoglund@webrtc.org3260f102013-11-25 14:10:20 +000029 parser.add_option('--label', type='string', default='MY_TEST',
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000030 help=('Label of the test, used to identify different '
31 'tests. Default: %default'))
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000032 parser.add_option('--ref_video', type='string',
33 help='Reference video to compare with (YUV).')
34 parser.add_option('--test_video', type='string',
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +000035 help=('Test video to be compared with the reference '
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000036 'video (YUV).'))
37 parser.add_option('--frame_analyzer', type='string',
38 help='Path to the frame analyzer executable.')
phoglund@webrtc.org3260f102013-11-25 14:10:20 +000039 parser.add_option('--barcode_decoder', type='string',
40 help=('Path to the barcode decoder script. By default, we '
41 'will assume we can find it in barcode_tools/'
42 'relative to this directory.'))
43 parser.add_option('--ffmpeg_path', type='string',
44 help=('The path to where the ffmpeg executable is located. '
45 'If omitted, it will be assumed to be present in the '
46 'PATH with the name ffmpeg[.exe].'))
47 parser.add_option('--zxing_path', type='string',
48 help=('The path to where the zxing executable is located. '
49 'If omitted, it will be assumed to be present in the '
50 'PATH with the name zxing[.exe].'))
mandermo74568172017-01-17 03:24:57 -080051 parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt',
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000052 help=('Path to the temporary stats file to be created and '
mandermo74568172017-01-17 03:24:57 -080053 'used for the reference video file. '
54 'Default: %default'))
55 parser.add_option('--stats_file_test', type='string',
56 default='stats_test.txt',
57 help=('Path to the temporary stats file to be created and '
58 'used for the test video file. Default: %default'))
59 parser.add_option('--stats_file', type='string',
60 help=('DEPRECATED'))
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000061 parser.add_option('--yuv_frame_width', type='int', default=640,
62 help='Width of the YUV file\'s frames. Default: %default')
63 parser.add_option('--yuv_frame_height', type='int', default=480,
64 help='Height of the YUV file\'s frames. Default: %default')
Henrik Kjellander57e5fd22015-05-25 12:55:39 +020065 options, _ = parser.parse_args()
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000066
mandermo74568172017-01-17 03:24:57 -080067 if options.stats_file:
68 options.stats_file_test = options.stats_file
69 print ('WARNING: Using deprecated switch --stats_file. '
70 'The new flag is --stats_file_test.')
71
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +000072 if not options.ref_video:
73 parser.error('You must provide a path to the reference video!')
74 if not os.path.exists(options.ref_video):
75 parser.error('Cannot find the reference video at %s' % options.ref_video)
76
77 if not options.test_video:
78 parser.error('You must provide a path to the test video!')
79 if not os.path.exists(options.test_video):
80 parser.error('Cannot find the test video at %s' % options.test_video)
81
82 if not options.frame_analyzer:
83 parser.error('You must provide the path to the frame analyzer executable!')
84 if not os.path.exists(options.frame_analyzer):
85 parser.error('Cannot find frame analyzer executable at %s!' %
86 options.frame_analyzer)
87 return options
88
mandermo74568172017-01-17 03:24:57 -080089def _DevNull():
90 """On Windows, sometimes the inherited stdin handle from the parent process
91 fails. Workaround this by passing null to stdin to the subprocesses commands.
92 This function can be used to create the null file handler.
93 """
94 return open(os.devnull, 'r')
95
96def DecodeBarcodesInVideo(options, path_to_decoder, video, stat_file):
97 # Run barcode decoder on the test video to identify frame numbers.
98 png_working_directory = tempfile.mkdtemp()
99 cmd = [
100 sys.executable,
101 path_to_decoder,
102 '--yuv_file=%s' % video,
103 '--yuv_frame_width=%d' % options.yuv_frame_width,
104 '--yuv_frame_height=%d' % options.yuv_frame_height,
105 '--stats_file=%s' % stat_file,
106 '--png_working_dir=%s' % png_working_directory,
107 ]
108 if options.zxing_path:
109 cmd.append('--zxing_path=%s' % options.zxing_path)
110 if options.ffmpeg_path:
111 cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path)
112
113
114 barcode_decoder = subprocess.Popen(cmd, stdin=_DevNull(),
115 stdout=sys.stdout, stderr=sys.stderr)
116 barcode_decoder.wait()
117
118 shutil.rmtree(png_working_directory)
119 if barcode_decoder.returncode != 0:
120 print 'Failed to run barcode decoder script.'
121 return 1
122 return 0
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000123
124def main():
125 """The main function.
126
127 A simple invocation is:
128 ./webrtc/tools/barcode_tools/compare_videos.py
129 --ref_video=<path_and_name_of_reference_video>
130 --test_video=<path_and_name_of_test_video>
131 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
132
133 Notice that the prerequisites for barcode_decoder.py also applies to this
134 script. The means the following executables have to be available in the PATH:
135 * zxing
136 * ffmpeg
137 """
138 options = _ParseArgs()
139
phoglund@webrtc.org3260f102013-11-25 14:10:20 +0000140 if options.barcode_decoder:
141 path_to_decoder = options.barcode_decoder
142 else:
143 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools',
144 'barcode_decoder.py')
kjellander@webrtc.orgc0b4c4a2013-10-02 15:04:45 +0000145
mandermo74568172017-01-17 03:24:57 -0800146 if DecodeBarcodesInVideo(options, path_to_decoder,
147 options.ref_video, options.stats_file_ref) != 0:
148 return 1
149 if DecodeBarcodesInVideo(options, path_to_decoder,
150 options.test_video, options.stats_file_test) != 0:
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000151 return 1
152
153 # Run frame analyzer to compare the videos and print output.
154 cmd = [
155 options.frame_analyzer,
kjellander@webrtc.orgf880f862013-09-10 12:10:01 +0000156 '--label=%s' % options.label,
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000157 '--reference_file=%s' % options.ref_video,
phoglund@webrtc.org7bcc7e32013-06-27 14:05:26 +0000158 '--test_file=%s' % options.test_video,
mandermo74568172017-01-17 03:24:57 -0800159 '--stats_file_ref=%s' % options.stats_file_ref,
160 '--stats_file_test=%s' % options.stats_file_test,
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000161 '--width=%d' % options.yuv_frame_width,
162 '--height=%d' % options.yuv_frame_height,
163 ]
mandermo74568172017-01-17 03:24:57 -0800164 frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(),
kjellander@webrtc.orgc0b4c4a2013-10-02 15:04:45 +0000165 stdout=sys.stdout, stderr=sys.stderr)
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000166 frame_analyzer.wait()
167 if frame_analyzer.returncode != 0:
phoglund@webrtc.org84002462013-07-29 11:01:03 +0000168 print 'Failed to run frame analyzer.'
kjellander@webrtc.orga6ff8452013-05-14 09:43:04 +0000169 return 1
170
171 return 0
172
173if __name__ == '__main__':
174 sys.exit(main())