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 | |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 10 | import json |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 11 | import optparse |
| 12 | import os |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 13 | import shutil |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 14 | import subprocess |
| 15 | import sys |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 16 | import tempfile |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 17 | |
| 18 | |
| 19 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 20 | |
| 21 | # Chrome browsertests will throw away stderr; avoid that output gets lost. |
| 22 | sys.stderr = sys.stdout |
| 23 | |
| 24 | |
| 25 | def _ParseArgs(): |
| 26 | """Registers the command-line options.""" |
| 27 | usage = 'usage: %prog [options]' |
| 28 | parser = optparse.OptionParser(usage=usage) |
| 29 | |
| 30 | parser.add_option('--label', type='string', default='MY_TEST', |
| 31 | help=('Label of the test, used to identify different ' |
| 32 | 'tests. Default: %default')) |
| 33 | parser.add_option('--ref_video', type='string', |
| 34 | help='Reference video to compare with (YUV).') |
| 35 | parser.add_option('--test_video', type='string', |
| 36 | help=('Test video to be compared with the reference ' |
| 37 | 'video (YUV).')) |
| 38 | parser.add_option('--frame_analyzer', type='string', |
| 39 | help='Path to the frame analyzer executable.') |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 40 | parser.add_option('--aligned_output_file', type='string', |
| 41 | help='Path for output aligned YUV or Y4M file.') |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 42 | parser.add_option('--vmaf', type='string', |
| 43 | help='Path to VMAF executable.') |
| 44 | parser.add_option('--vmaf_model', type='string', |
| 45 | help='Path to VMAF model.') |
| 46 | parser.add_option('--vmaf_phone_model', action='store_true', |
| 47 | help='Whether to use phone model in VMAF.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 48 | parser.add_option('--barcode_decoder', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 49 | help=('Path to the barcode decoder script. By default, we ' |
| 50 | 'will assume we can find it in barcode_tools/' |
| 51 | 'relative to this directory.')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 52 | parser.add_option('--ffmpeg_path', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 53 | help=('The path to where the ffmpeg executable is located. ' |
| 54 | 'If omitted, it will be assumed to be present in the ' |
| 55 | 'PATH with the name ffmpeg[.exe].')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 56 | parser.add_option('--zxing_path', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 57 | help=('The path to where the zxing executable is located. ' |
| 58 | 'If omitted, it will be assumed to be present in the ' |
| 59 | 'PATH with the name zxing[.exe].')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 60 | parser.add_option('--stats_file_ref', type='string', default='stats_ref.txt', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 61 | help=('Path to the temporary stats file to be created and ' |
| 62 | 'used for the reference video file. ' |
| 63 | 'Default: %default')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 64 | parser.add_option('--stats_file_test', type='string', |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 65 | default='stats_test.txt', |
| 66 | help=('Path to the temporary stats file to be created and ' |
| 67 | 'used for the test video file. Default: %default')) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 68 | parser.add_option('--stats_file', type='string', |
| 69 | help=('DEPRECATED')) |
| 70 | parser.add_option('--yuv_frame_width', type='int', default=640, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 71 | help='Width of the YUV file\'s frames. Default: %default') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 72 | parser.add_option('--yuv_frame_height', type='int', default=480, |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 73 | help='Height of the YUV file\'s frames. Default: %default') |
Edward Lemur | 2e5966b | 2018-01-30 15:33:02 +0100 | [diff] [blame] | 74 | parser.add_option('--chartjson_result_file', type='str', default=None, |
| 75 | help='Where to store perf results in chartjson format.') |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 76 | options, _ = parser.parse_args() |
| 77 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 78 | if options.stats_file: |
| 79 | options.stats_file_test = options.stats_file |
| 80 | print ('WARNING: Using deprecated switch --stats_file. ' |
| 81 | 'The new flag is --stats_file_test.') |
| 82 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 83 | if not options.ref_video: |
| 84 | parser.error('You must provide a path to the reference video!') |
| 85 | if not os.path.exists(options.ref_video): |
| 86 | parser.error('Cannot find the reference video at %s' % options.ref_video) |
| 87 | |
| 88 | if not options.test_video: |
| 89 | parser.error('You must provide a path to the test video!') |
| 90 | if not os.path.exists(options.test_video): |
| 91 | parser.error('Cannot find the test video at %s' % options.test_video) |
| 92 | |
| 93 | if not options.frame_analyzer: |
| 94 | parser.error('You must provide the path to the frame analyzer executable!') |
| 95 | if not os.path.exists(options.frame_analyzer): |
| 96 | parser.error('Cannot find frame analyzer executable at %s!' % |
| 97 | options.frame_analyzer) |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 98 | |
| 99 | if options.vmaf and not options.vmaf_model: |
| 100 | parser.error('You must provide a path to a VMAF model to use VMAF.') |
| 101 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 102 | return options |
| 103 | |
| 104 | def _DevNull(): |
| 105 | """On Windows, sometimes the inherited stdin handle from the parent process |
| 106 | fails. Workaround this by passing null to stdin to the subprocesses commands. |
| 107 | This function can be used to create the null file handler. |
| 108 | """ |
| 109 | return open(os.devnull, 'r') |
| 110 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 111 | def DecodeBarcodesInVideo(options, path_to_decoder, video, stat_file): |
| 112 | # Run barcode decoder on the test video to identify frame numbers. |
| 113 | png_working_directory = tempfile.mkdtemp() |
| 114 | cmd = [ |
| 115 | sys.executable, |
| 116 | path_to_decoder, |
| 117 | '--yuv_file=%s' % video, |
| 118 | '--yuv_frame_width=%d' % options.yuv_frame_width, |
| 119 | '--yuv_frame_height=%d' % options.yuv_frame_height, |
| 120 | '--stats_file=%s' % stat_file, |
| 121 | '--png_working_dir=%s' % png_working_directory, |
| 122 | ] |
| 123 | if options.zxing_path: |
| 124 | cmd.append('--zxing_path=%s' % options.zxing_path) |
| 125 | if options.ffmpeg_path: |
| 126 | cmd.append('--ffmpeg_path=%s' % options.ffmpeg_path) |
| 127 | |
| 128 | |
| 129 | barcode_decoder = subprocess.Popen(cmd, stdin=_DevNull(), |
| 130 | stdout=sys.stdout, stderr=sys.stderr) |
| 131 | barcode_decoder.wait() |
| 132 | |
| 133 | shutil.rmtree(png_working_directory) |
| 134 | if barcode_decoder.returncode != 0: |
| 135 | print 'Failed to run barcode decoder script.' |
| 136 | return 1 |
| 137 | return 0 |
| 138 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 139 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 140 | def _RunFrameAnalyzer(options, yuv_directory=None): |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 141 | """Run frame analyzer to compare the videos and print output.""" |
| 142 | cmd = [ |
| 143 | options.frame_analyzer, |
| 144 | '--label=%s' % options.label, |
| 145 | '--reference_file=%s' % options.ref_video, |
| 146 | '--test_file=%s' % options.test_video, |
| 147 | '--stats_file_ref=%s' % options.stats_file_ref, |
| 148 | '--stats_file_test=%s' % options.stats_file_test, |
| 149 | '--width=%d' % options.yuv_frame_width, |
| 150 | '--height=%d' % options.yuv_frame_height, |
| 151 | ] |
| 152 | if options.chartjson_result_file: |
| 153 | cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file) |
| 154 | if options.aligned_output_file: |
| 155 | cmd.append('--aligned_output_file=%s' % options.aligned_output_file) |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 156 | if yuv_directory: |
| 157 | cmd.append('--yuv_directory=%s' % yuv_directory) |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 158 | frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(), |
| 159 | stdout=sys.stdout, stderr=sys.stderr) |
| 160 | frame_analyzer.wait() |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 161 | if frame_analyzer.returncode != 0: |
| 162 | print 'Failed to run frame analyzer.' |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 163 | return frame_analyzer.returncode |
| 164 | |
| 165 | |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 166 | def _RunVmaf(options, yuv_directory, logfile): |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 167 | """ Run VMAF to compare videos and print output. |
| 168 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 169 | The yuv_directory is assumed to have been populated with a reference and test |
| 170 | video in .yuv format, with names according to the label. |
| 171 | """ |
| 172 | cmd = [ |
| 173 | options.vmaf, |
| 174 | 'yuv420p', |
| 175 | str(options.yuv_frame_width), |
| 176 | str(options.yuv_frame_height), |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 177 | os.path.join(yuv_directory, "ref.yuv"), |
| 178 | os.path.join(yuv_directory, "test.yuv"), |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 179 | options.vmaf_model, |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 180 | '--log', |
| 181 | logfile, |
| 182 | '--log-fmt', |
| 183 | 'json', |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 184 | ] |
| 185 | if options.vmaf_phone_model: |
| 186 | cmd.append('--phone-model') |
| 187 | |
| 188 | vmaf = subprocess.Popen(cmd, stdin=_DevNull(), |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 189 | stdout=sys.stdout, stderr=sys.stderr) |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 190 | vmaf.wait() |
| 191 | if vmaf.returncode != 0: |
| 192 | print 'Failed to run VMAF.' |
| 193 | return 1 |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 194 | |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 195 | # Read per-frame scores from VMAF output and print. |
| 196 | with open(logfile) as f: |
| 197 | vmaf_data = json.load(f) |
| 198 | vmaf_scores = [] |
| 199 | for frame in vmaf_data['frames']: |
| 200 | vmaf_scores.append(frame['metrics']['vmaf']) |
| 201 | print 'RESULT VMAF: %s=' % options.label, vmaf_scores |
| 202 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 203 | return 0 |
| 204 | |
| 205 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 206 | def main(): |
| 207 | """The main function. |
| 208 | |
| 209 | A simple invocation is: |
Paulina Hensman | b671d46 | 2018-09-14 11:32:00 +0200 | [diff] [blame] | 210 | ./webrtc/rtc_tools/compare_videos.py |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 211 | --ref_video=<path_and_name_of_reference_video> |
| 212 | --test_video=<path_and_name_of_test_video> |
| 213 | --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable> |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 214 | |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 215 | Running vmaf requires the following arguments: |
| 216 | --vmaf, --vmaf_model, --yuv_frame_width, --yuv_frame_height |
| 217 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 218 | Notice that the prerequisites for barcode_decoder.py also applies to this |
| 219 | script. The means the following executables have to be available in the PATH: |
| 220 | * zxing |
| 221 | * ffmpeg |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 222 | """ |
| 223 | options = _ParseArgs() |
| 224 | |
Magnus Jedvert | 3e169ac | 2018-08-24 12:44:59 +0000 | [diff] [blame] | 225 | if options.barcode_decoder: |
| 226 | path_to_decoder = options.barcode_decoder |
| 227 | else: |
| 228 | path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools', |
| 229 | 'barcode_decoder.py') |
| 230 | |
| 231 | if DecodeBarcodesInVideo(options, path_to_decoder, |
| 232 | options.ref_video, options.stats_file_ref) != 0: |
| 233 | return 1 |
| 234 | if DecodeBarcodesInVideo(options, path_to_decoder, |
| 235 | options.test_video, options.stats_file_test) != 0: |
| 236 | return 1 |
| 237 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 238 | if options.vmaf: |
| 239 | try: |
| 240 | # Directory to save temporary YUV files for VMAF in frame_analyzer. |
| 241 | yuv_directory = tempfile.mkdtemp() |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 242 | _, vmaf_logfile = tempfile.mkstemp() |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 243 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 244 | # Run frame analyzer to compare the videos and print output. |
| 245 | if _RunFrameAnalyzer(options, yuv_directory=yuv_directory) != 0: |
| 246 | return 1 |
Paulina Hensman | 12c62b9 | 2018-09-28 15:14:07 +0200 | [diff] [blame] | 247 | |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 248 | # Run VMAF for further video comparison and print output. |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 249 | return _RunVmaf(options, yuv_directory, vmaf_logfile) |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 250 | finally: |
| 251 | shutil.rmtree(yuv_directory) |
Paulina Hensman | ede8796 | 2018-10-10 15:48:30 +0200 | [diff] [blame^] | 252 | os.remove(vmaf_logfile) |
Paulina Hensman | a6471eb | 2018-10-05 14:34:33 +0200 | [diff] [blame] | 253 | else: |
| 254 | return _RunFrameAnalyzer(options) |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 255 | |
kjellander | d2b63cf | 2017-06-30 03:04:59 -0700 | [diff] [blame] | 256 | return 0 |
| 257 | |
| 258 | if __name__ == '__main__': |
| 259 | sys.exit(main()) |