kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 1 | #!/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 | |
| 10 | import optparse |
| 11 | import os |
| 12 | import shutil |
| 13 | import subprocess |
| 14 | import sys |
| 15 | import tempfile |
| 16 | |
| 17 | |
| 18 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 19 | |
| 20 | # Chrome browsertests will throw away stderr; avoid that output gets lost. |
| 21 | sys.stderr = sys.stdout |
| 22 | |
| 23 | |
| 24 | def _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.') |
| 39 | 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].')) |
| 51 | parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt', |
| 52 | help=('Path to the temporary stats file to be created and ' |
| 53 | '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')) |
| 61 | 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') |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 65 | parser.add_option('--chartjson_result_file', type='str', default=None, |
| 66 | help='Where to store perf results in chartjson format.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 67 | options, _ = parser.parse_args() |
| 68 | |
| 69 | if options.stats_file: |
| 70 | options.stats_file_test = options.stats_file |
| 71 | print ('WARNING: Using deprecated switch --stats_file. ' |
| 72 | 'The new flag is --stats_file_test.') |
| 73 | |
| 74 | if not options.ref_video: |
| 75 | parser.error('You must provide a path to the reference video!') |
| 76 | if not os.path.exists(options.ref_video): |
| 77 | parser.error('Cannot find the reference video at %s' % options.ref_video) |
| 78 | |
| 79 | if not options.test_video: |
| 80 | parser.error('You must provide a path to the test video!') |
| 81 | if not os.path.exists(options.test_video): |
| 82 | parser.error('Cannot find the test video at %s' % options.test_video) |
| 83 | |
| 84 | if not options.frame_analyzer: |
| 85 | parser.error('You must provide the path to the frame analyzer executable!') |
| 86 | if not os.path.exists(options.frame_analyzer): |
| 87 | parser.error('Cannot find frame analyzer executable at %s!' % |
| 88 | options.frame_analyzer) |
| 89 | return options |
| 90 | |
| 91 | def _DevNull(): |
| 92 | """On Windows, sometimes the inherited stdin handle from the parent process |
| 93 | fails. Workaround this by passing null to stdin to the subprocesses commands. |
| 94 | This function can be used to create the null file handler. |
| 95 | """ |
| 96 | return open(os.devnull, 'r') |
| 97 | |
| 98 | def DecodeBarcodesInVideo(options, path_to_decoder, video, stat_file): |
| 99 | # Run barcode decoder on the test video to identify frame numbers. |
| 100 | png_working_directory = tempfile.mkdtemp() |
| 101 | cmd = [ |
| 102 | sys.executable, |
| 103 | path_to_decoder, |
| 104 | '--yuv_file=%s' % video, |
| 105 | '--yuv_frame_width=%d' % options.yuv_frame_width, |
| 106 | '--yuv_frame_height=%d' % options.yuv_frame_height, |
| 107 | '--stats_file=%s' % stat_file, |
| 108 | '--png_working_dir=%s' % png_working_directory, |
| 109 | ] |
| 110 | if options.zxing_path: |
| 111 | cmd.append('--zxing_path=%s' % options.zxing_path) |
| 112 | if options.ffmpeg_path: |
| 113 | cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path) |
| 114 | |
| 115 | |
| 116 | barcode_decoder = subprocess.Popen(cmd, stdin=_DevNull(), |
| 117 | stdout=sys.stdout, stderr=sys.stderr) |
| 118 | barcode_decoder.wait() |
| 119 | |
| 120 | shutil.rmtree(png_working_directory) |
| 121 | if barcode_decoder.returncode != 0: |
| 122 | print 'Failed to run barcode decoder script.' |
| 123 | return 1 |
| 124 | return 0 |
| 125 | |
| 126 | def main(): |
| 127 | """The main function. |
| 128 | |
| 129 | A simple invocation is: |
| 130 | ./webrtc/rtc_tools/barcode_tools/compare_videos.py |
| 131 | --ref_video=<path_and_name_of_reference_video> |
| 132 | --test_video=<path_and_name_of_test_video> |
| 133 | --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable> |
| 134 | |
| 135 | Notice that the prerequisites for barcode_decoder.py also applies to this |
| 136 | script. The means the following executables have to be available in the PATH: |
| 137 | * zxing |
| 138 | * ffmpeg |
| 139 | """ |
| 140 | options = _ParseArgs() |
| 141 | |
| 142 | if options.barcode_decoder: |
| 143 | path_to_decoder = options.barcode_decoder |
| 144 | else: |
| 145 | path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools', |
| 146 | 'barcode_decoder.py') |
| 147 | |
| 148 | if DecodeBarcodesInVideo(options, path_to_decoder, |
| 149 | options.ref_video, options.stats_file_ref) != 0: |
| 150 | return 1 |
| 151 | if DecodeBarcodesInVideo(options, path_to_decoder, |
| 152 | options.test_video, options.stats_file_test) != 0: |
| 153 | return 1 |
| 154 | |
| 155 | # Run frame analyzer to compare the videos and print output. |
| 156 | cmd = [ |
| 157 | options.frame_analyzer, |
| 158 | '--label=%s' % options.label, |
| 159 | '--reference_file=%s' % options.ref_video, |
| 160 | '--test_file=%s' % options.test_video, |
| 161 | '--stats_file_ref=%s' % options.stats_file_ref, |
| 162 | '--stats_file_test=%s' % options.stats_file_test, |
| 163 | '--width=%d' % options.yuv_frame_width, |
| 164 | '--height=%d' % options.yuv_frame_height, |
| 165 | ] |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 166 | if options.chartjson_result_file: |
| 167 | cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 168 | frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(), |
| 169 | stdout=sys.stdout, stderr=sys.stderr) |
| 170 | frame_analyzer.wait() |
| 171 | if frame_analyzer.returncode != 0: |
| 172 | print 'Failed to run frame analyzer.' |
| 173 | return 1 |
| 174 | |
| 175 | return 0 |
| 176 | |
| 177 | if __name__ == '__main__': |
| 178 | sys.exit(main()) |