blob: 4b21b23a245f5662fd1836223197da649ce3df7f [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
12import subprocess
13import sys
14
15
16SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
17
18
19def _ParseArgs():
20 """Registers the command-line options."""
21 usage = 'usage: %prog [options]'
22 parser = optparse.OptionParser(usage=usage)
23
24 parser.add_option('--ref_video', type='string',
25 help='Reference video to compare with (YUV).')
26 parser.add_option('--test_video', type='string',
27 help=('Test video to be comared with the reference '
28 'video (YUV).'))
29 parser.add_option('--frame_analyzer', type='string',
30 help='Path to the frame analyzer executable.')
31 parser.add_option('--stats_file', type='string', default='stats.txt',
32 help=('Path to the temporary stats file to be created and '
33 'used. Default: %default'))
34 parser.add_option('--yuv_frame_width', type='int', default=640,
35 help='Width of the YUV file\'s frames. Default: %default')
36 parser.add_option('--yuv_frame_height', type='int', default=480,
37 help='Height of the YUV file\'s frames. Default: %default')
38 options, _args = parser.parse_args()
39
40 if not options.ref_video:
41 parser.error('You must provide a path to the reference video!')
42 if not os.path.exists(options.ref_video):
43 parser.error('Cannot find the reference video at %s' % options.ref_video)
44
45 if not options.test_video:
46 parser.error('You must provide a path to the test video!')
47 if not os.path.exists(options.test_video):
48 parser.error('Cannot find the test video at %s' % options.test_video)
49
50 if not options.frame_analyzer:
51 parser.error('You must provide the path to the frame analyzer executable!')
52 if not os.path.exists(options.frame_analyzer):
53 parser.error('Cannot find frame analyzer executable at %s!' %
54 options.frame_analyzer)
55 return options
56
57
58def main():
59 """The main function.
60
61 A simple invocation is:
62 ./webrtc/tools/barcode_tools/compare_videos.py
63 --ref_video=<path_and_name_of_reference_video>
64 --test_video=<path_and_name_of_test_video>
65 --frame_analyzer=<path_and_name_of_the_frame_analyzer_executable>
66
67 Notice that the prerequisites for barcode_decoder.py also applies to this
68 script. The means the following executables have to be available in the PATH:
69 * zxing
70 * ffmpeg
71 """
72 options = _ParseArgs()
73
74 # Run barcode decoder on the test video to identify frame numbers.
75 path_to_decoder = os.path.join(SCRIPT_DIR, 'barcode_tools',
76 'barcode_decoder.py')
77 cmd = [
78 sys.executable,
79 path_to_decoder,
80 '--yuv_file=%s' % options.test_video,
81 '--yuv_frame_width=%d' % options.yuv_frame_width,
82 '--yuv_frame_height=%d' % options.yuv_frame_height,
83 '--stats_file=%s' % options.stats_file,
84 ]
85 barcode_decoder = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
86 barcode_decoder.wait()
87 if barcode_decoder.returncode != 0:
88 print >> sys.stderr, 'Failed to run barcode decoder script.'
89 return 1
90
91 # Run frame analyzer to compare the videos and print output.
92 cmd = [
93 options.frame_analyzer,
94 '--reference_file=%s' % options.ref_video,
95 '--test_file=%s' % options.ref_video,
96 '--stats_file=%s' % options.stats_file,
97 '--width=%d' % options.yuv_frame_width,
98 '--height=%d' % options.yuv_frame_height,
99 ]
100 frame_analyzer = subprocess.Popen(cmd, stdout=sys.stdout, stderr=sys.stderr)
101 frame_analyzer.wait()
102 if frame_analyzer.returncode != 0:
103 print >> sys.stderr, 'Failed to run frame analyzer.'
104 return 1
105
106 return 0
107
108if __name__ == '__main__':
109 sys.exit(main())