kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [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 subprocess |
| 13 | import sys |
| 14 | |
| 15 | |
| 16 | SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) |
| 17 | |
phoglund@webrtc.org | 8400246 | 2013-07-29 11:01:03 +0000 | [diff] [blame] | 18 | # Chrome browsertests will throw away stderr; avoid that output gets lost. |
| 19 | sys.stderr = sys.stdout |
| 20 | |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 21 | |
| 22 | def _ParseArgs(): |
| 23 | """Registers the command-line options.""" |
| 24 | usage = 'usage: %prog [options]' |
| 25 | parser = optparse.OptionParser(usage=usage) |
| 26 | |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 27 | parser.add_option('--label', type='string', default="MY_TEST", |
| 28 | help=('Label of the test, used to identify different ' |
| 29 | 'tests. Default: %default')) |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 30 | parser.add_option('--ref_video', type='string', |
| 31 | help='Reference video to compare with (YUV).') |
| 32 | parser.add_option('--test_video', type='string', |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 33 | help=('Test video to be compared with the reference ' |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 34 | 'video (YUV).')) |
| 35 | parser.add_option('--frame_analyzer', type='string', |
| 36 | help='Path to the frame analyzer executable.') |
| 37 | parser.add_option('--stats_file', type='string', default='stats.txt', |
| 38 | help=('Path to the temporary stats file to be created and ' |
| 39 | 'used. Default: %default')) |
| 40 | parser.add_option('--yuv_frame_width', type='int', default=640, |
| 41 | help='Width of the YUV file\'s frames. Default: %default') |
| 42 | parser.add_option('--yuv_frame_height', type='int', default=480, |
| 43 | help='Height of the YUV file\'s frames. Default: %default') |
| 44 | options, _args = parser.parse_args() |
| 45 | |
| 46 | if not options.ref_video: |
| 47 | parser.error('You must provide a path to the reference video!') |
| 48 | if not os.path.exists(options.ref_video): |
| 49 | parser.error('Cannot find the reference video at %s' % options.ref_video) |
| 50 | |
| 51 | if not options.test_video: |
| 52 | parser.error('You must provide a path to the test video!') |
| 53 | if not os.path.exists(options.test_video): |
| 54 | parser.error('Cannot find the test video at %s' % options.test_video) |
| 55 | |
| 56 | if not options.frame_analyzer: |
| 57 | parser.error('You must provide the path to the frame analyzer executable!') |
| 58 | if not os.path.exists(options.frame_analyzer): |
| 59 | parser.error('Cannot find frame analyzer executable at %s!' % |
| 60 | options.frame_analyzer) |
| 61 | return options |
| 62 | |
| 63 | |
| 64 | def main(): |
| 65 | """The main function. |
| 66 | |
| 67 | A simple invocation is: |
| 68 | ./webrtc/tools/barcode_tools/compare_videos.py |
| 69 | --ref_video=<path_and_name_of_reference_video> |
| 70 | --test_video=<path_and_name_of_test_video> |
| 71 | --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable> |
| 72 | |
| 73 | Notice that the prerequisites for barcode_decoder.py also applies to this |
| 74 | script. The means the following executables have to be available in the PATH: |
| 75 | * zxing |
| 76 | * ffmpeg |
| 77 | """ |
| 78 | options = _ParseArgs() |
| 79 | |
| 80 | # Run barcode decoder on the test video to identify frame numbers. |
| 81 | path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools', |
| 82 | 'barcode_decoder.py') |
kjellander@webrtc.org | c0b4c4a | 2013-10-02 15:04:45 +0000 | [diff] [blame] | 83 | |
| 84 | # On Windows, sometimes the inherited stdin handle from the parent process |
| 85 | # fails. Work around this by passing null to stdin to the subprocesses. |
| 86 | null_filehandle = open(os.devnull, 'r') |
| 87 | |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 88 | cmd = [ |
| 89 | sys.executable, |
| 90 | path_to_decoder, |
| 91 | '--yuv_file=%s' % options.test_video, |
| 92 | '--yuv_frame_width=%d' % options.yuv_frame_width, |
| 93 | '--yuv_frame_height=%d' % options.yuv_frame_height, |
| 94 | '--stats_file=%s' % options.stats_file, |
| 95 | ] |
kjellander@webrtc.org | c0b4c4a | 2013-10-02 15:04:45 +0000 | [diff] [blame] | 96 | barcode_decoder = subprocess.Popen(cmd, stdin=null_filehandle, |
| 97 | stdout=sys.stdout, stderr=sys.stderr) |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 98 | barcode_decoder.wait() |
| 99 | if barcode_decoder.returncode != 0: |
phoglund@webrtc.org | 8400246 | 2013-07-29 11:01:03 +0000 | [diff] [blame] | 100 | print 'Failed to run barcode decoder script.' |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 101 | return 1 |
| 102 | |
| 103 | # Run frame analyzer to compare the videos and print output. |
| 104 | cmd = [ |
| 105 | options.frame_analyzer, |
kjellander@webrtc.org | f880f86 | 2013-09-10 12:10:01 +0000 | [diff] [blame] | 106 | '--label=%s' % options.label, |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 107 | '--reference_file=%s' % options.ref_video, |
phoglund@webrtc.org | 7bcc7e3 | 2013-06-27 14:05:26 +0000 | [diff] [blame] | 108 | '--test_file=%s' % options.test_video, |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 109 | '--stats_file=%s' % options.stats_file, |
| 110 | '--width=%d' % options.yuv_frame_width, |
| 111 | '--height=%d' % options.yuv_frame_height, |
| 112 | ] |
kjellander@webrtc.org | c0b4c4a | 2013-10-02 15:04:45 +0000 | [diff] [blame] | 113 | frame_analyzer = subprocess.Popen(cmd, stdin=null_filehandle, |
| 114 | stdout=sys.stdout, stderr=sys.stderr) |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 115 | frame_analyzer.wait() |
| 116 | if frame_analyzer.returncode != 0: |
phoglund@webrtc.org | 8400246 | 2013-07-29 11:01:03 +0000 | [diff] [blame] | 117 | print 'Failed to run frame analyzer.' |
kjellander@webrtc.org | a6ff845 | 2013-05-14 09:43:04 +0000 | [diff] [blame] | 118 | return 1 |
| 119 | |
| 120 | return 0 |
| 121 | |
| 122 | if __name__ == '__main__': |
| 123 | sys.exit(main()) |