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 |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 12 | import shutil |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 13 | import subprocess |
| 14 | import sys |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 15 | import tempfile |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 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.') |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 39 | parser.add_option('--aligned_output_file', type='string', |
| 40 | help='Path for output aligned YUV or Y4M file.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 41 | parser.add_option('--barcode_decoder', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 42 | 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.')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 45 | parser.add_option('--ffmpeg_path', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 46 | 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].')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 49 | parser.add_option('--zxing_path', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 50 | 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].')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 53 | parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 54 | help=('Path to the temporary stats file to be created and ' |
| 55 | 'used for the reference video file. ' |
| 56 | 'Default: %default')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 57 | parser.add_option('--stats_file_test', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 58 | 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')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 61 | parser.add_option('--stats_file', type='string', |
| 62 | help=('DEPRECATED')) |
| 63 | parser.add_option('--yuv_frame_width', type='int', default=640, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 64 | help='Width of the YUV file\'s frames. Default: %default') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 65 | parser.add_option('--yuv_frame_height', type='int', default=480, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 66 | help='Height of the YUV file\'s frames. Default: %default') |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 67 | parser.add_option('--chartjson_result_file', type='str', default=None, |
| 68 | help='Where to store perf results in chartjson format.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 69 | options, _ = parser.parse_args() |
| 70 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 71 | 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 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 76 | 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 | |
| 93 | def _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 Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 100 | def 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 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 128 | def main(): |
| 129 | """The main function. |
| 130 | |
| 131 | A simple invocation is: |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 132 | ./webrtc/rtc_tools/compare_videos.py |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 133 | --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 Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 136 | |
| 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 |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 141 | """ |
| 142 | options = _ParseArgs() |
| 143 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 144 | 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 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 157 | # 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 Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 163 | '--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, |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 167 | ] |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 168 | if options.chartjson_result_file: |
| 169 | cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file) |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 170 | if options.aligned_output_file: |
| 171 | cmd.append('--aligned_output_file=%s' % options.aligned_output_file) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 172 | 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 | |
| 181 | if __name__ == '__main__': |
| 182 | sys.exit(main()) |