Reland "Tidy up and increase exception handling in compare_videos"

This is a reland of 1c60ff521eda26c80fa53097d9c614f10200f651

Original change's description:
> Tidy up and increase exception handling in compare_videos
> 
> Bug: webrtc:9642
> Change-Id: I5c8b252de3b285f81a5437af99d789b5a28ce646
> Reviewed-on: https://webrtc-review.googlesource.com/102880
> Commit-Queue: Paulina Hensman <phensman@webrtc.org>
> Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
> Cr-Commit-Position: refs/heads/master@{#24909}

Bug: webrtc:9642
Change-Id: I11078a358297ae5046991ac3b0680df468bb413b
Reviewed-on: https://webrtc-review.googlesource.com/c/102941
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Reviewed-by: Patrik Höglund <phoglund@webrtc.org>
Commit-Queue: Paulina Hensman <phensman@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25024}
diff --git a/rtc_tools/compare_videos.py b/rtc_tools/compare_videos.py
index ac41ef8..7979043 100755
--- a/rtc_tools/compare_videos.py
+++ b/rtc_tools/compare_videos.py
@@ -136,7 +136,7 @@
   return 0
 
 
-def _RunFrameAnalyzer(options):
+def _RunFrameAnalyzer(options, yuv_directory=None):
   """Run frame analyzer to compare the videos and print output."""
   cmd = [
     options.frame_analyzer,
@@ -152,15 +152,17 @@
     cmd.append('--chartjson_result_file=%s' % options.chartjson_result_file)
   if options.aligned_output_file:
     cmd.append('--aligned_output_file=%s' % options.aligned_output_file)
-  if options.yuv_directory:
-    cmd.append('--yuv_directory=%s' % options.yuv_directory)
+  if yuv_directory:
+    cmd.append('--yuv_directory=%s' % yuv_directory)
   frame_analyzer = subprocess.Popen(cmd, stdin=_DevNull(),
                                     stdout=sys.stdout, stderr=sys.stderr)
   frame_analyzer.wait()
+  if frame_analyzer.returncode != 0:
+    print 'Failed to run frame analyzer.'
   return frame_analyzer.returncode
 
 
-def _RunVmaf(options):
+def _RunVmaf(options, yuv_directory):
   """ Run VMAF to compare videos and print output.
 
   The provided vmaf directory is assumed to contain a c++ wrapper executable
@@ -174,22 +176,28 @@
     'yuv420p',
     str(options.yuv_frame_width),
     str(options.yuv_frame_height),
-    os.path.join(options.yuv_directory, "ref.yuv"),
-    os.path.join(options.yuv_directory, "test.yuv"),
+    os.path.join(yuv_directory, "ref.yuv"),
+    os.path.join(yuv_directory, "test.yuv"),
     options.vmaf_model,
   ]
   if options.vmaf_phone_model:
     cmd.append('--phone-model')
 
   vmaf = subprocess.Popen(cmd, stdin=_DevNull(),
-                                  stdout=subprocess.PIPE, stderr=sys.stderr)
+                          stdout=subprocess.PIPE, stderr=sys.stderr)
   vmaf.wait()
   if vmaf.returncode != 0:
     print 'Failed to run VMAF.'
     return 1
   output = vmaf.stdout.read()
   # Extract score from VMAF output.
-  score = float(output.split('\n')[2].split()[3])
+  try:
+    score = float(output.split('\n')[2].split()[3])
+  except (ValueError, IndexError):
+    print 'Error in VMAF output (expected "VMAF score = [float]" on line 3):'
+    print output
+    return 1
+
   print 'RESULT Vmaf: %s= %f' % (options.label, score)
   return 0
 
@@ -226,25 +234,21 @@
                            options.test_video, options.stats_file_test) != 0:
     return 1
 
-  try:
-    # Create a directory to save temporary YUV files for VMAF in frame_analyzer.
-    options.yuv_directory = None
-    if options.vmaf:
-      options.yuv_directory = tempfile.mkdtemp() + '/'
+  if options.vmaf:
+    try:
+      # Directory to save temporary YUV files for VMAF in frame_analyzer.
+      yuv_directory = tempfile.mkdtemp()
 
-    # Run frame_analyzer to compare the videos and print output.
-    if _RunFrameAnalyzer(options) != 0:
-      print 'Failed to run frame analyzer.'
-      return 1
+      # Run frame analyzer to compare the videos and print output.
+      if _RunFrameAnalyzer(options, yuv_directory=yuv_directory) != 0:
+        return 1
 
-    # Run VMAF for further video comparison and print output.
-    if options.vmaf:
-      return _RunVmaf(options)
-
-  finally:
-    if options.yuv_directory:
-      shutil.rmtree(options.yuv_directory)
-
+      # Run VMAF for further video comparison and print output.
+      return _RunVmaf(options, yuv_directory)
+    finally:
+      shutil.rmtree(yuv_directory)
+  else:
+    return _RunFrameAnalyzer(options)
 
   return 0
 
diff --git a/rtc_tools/frame_analyzer/frame_analyzer.cc b/rtc_tools/frame_analyzer/frame_analyzer.cc
index 56dc1f4..89440ea 100644
--- a/rtc_tools/frame_analyzer/frame_analyzer.cc
+++ b/rtc_tools/frame_analyzer/frame_analyzer.cc
@@ -23,6 +23,20 @@
 #include "rtc_tools/video_file_writer.h"
 #include "test/testsupport/perf_test.h"
 
+namespace {
+
+#ifdef WIN32
+const char* const kPathDelimiter = "\\";
+#else
+const char* const kPathDelimiter = "/";
+#endif
+
+std::string JoinFilename(std::string directory, std::string filename) {
+  return directory + kPathDelimiter + filename;
+}
+
+}  // namespace
+
 /*
  * A command line tool running PSNR and SSIM on a reference video and a test
  * video. The test video is a record of the reference video which can start at
@@ -147,9 +161,11 @@
   }
   std::string yuv_directory = parser.GetFlag("yuv_directory");
   if (!yuv_directory.empty()) {
-    webrtc::test::WriteVideoToFile(reordered_video, yuv_directory + "ref.yuv",
+    webrtc::test::WriteVideoToFile(reordered_video,
+                                   JoinFilename(yuv_directory, "ref.yuv"),
                                    /*fps=*/30);
-    webrtc::test::WriteVideoToFile(test_video, yuv_directory + "test.yuv",
+    webrtc::test::WriteVideoToFile(test_video,
+                                   JoinFilename(yuv_directory, "test.yuv"),
                                    /*fps=*/30);
   }