diff --git a/test/video_source.h b/test/video_source.h
index 94500b43d3e887ea63791e4b7bd4faebc7ebb864..78e7d466b8775e79ef18678e5c13aa569313744a 100644
--- a/test/video_source.h
+++ b/test/video_source.h
@@ -10,6 +10,9 @@
 #ifndef TEST_VIDEO_SOURCE_H_
 #define TEST_VIDEO_SOURCE_H_
 
+#if defined(_WIN32)
+#include <windows.h>
+#endif
 #include <cstdio>
 #include <cstdlib>
 #include <string>
@@ -55,10 +58,70 @@ static FILE *OpenTestOutFile(const std::string& file_name) {
   return fopen(path_to_source.c_str(), "wb");
 }
 
-static FILE *OpenTempOutFile() {
-  return tmpfile();
+static std::string GetTempOutFilename() {
+  std::string basename;
+#if defined(_WIN32)
+  char fname[MAX_PATH];
+  // Assume for now that the filename generated is unique per process
+  const UINT ret = GetTempFileNameA(
+      GetDataPath().c_str(), "lvx", 0, fname);
+  if (ret != 0) {
+    const char *slash = strrchr(fname, '\\');
+    if (slash == NULL) slash = strrchr(fname, '/');
+    if (slash == NULL)
+      basename.assign(fname);
+    else
+      basename.assign(slash + 1);
+  } else {
+    basename.clear();
+  }
+#else
+  char fname[256];
+  const std::string templ = GetDataPath() + "/libvpx_test_XXXXXX";
+  strncpy(fname, templ.c_str(), templ.size());
+  fname[templ.size()] = '\0';
+  const int fd = mkstemp(fname);
+  if (fd != -1) {
+    close(fd);
+    basename.assign(strrchr(fname, '/') + 1);
+  } else {
+    basename.clear();
+  }
+#endif
+  return basename;
 }
 
+class TempOutFile {
+ public:
+  TempOutFile() {
+    file_name_ = GetTempOutFilename();
+    file_ = OpenTestOutFile(file_name_);
+  }
+  ~TempOutFile() {
+    CloseFile();
+    if (!file_name_.empty()) {
+      const std::string path_to_source = GetDataPath() + "/" + file_name_;
+      EXPECT_EQ(0, remove(path_to_source.c_str()));
+    }
+  }
+  FILE *file() {
+    return file_;
+  }
+  const std::string& file_name() {
+    return file_name_;
+  }
+  void CloseFile() {
+    if (file_) {
+      fclose(file_);
+      file_ = NULL;
+    }
+  }
+
+ protected:
+  FILE *file_;
+  std::string file_name_;
+};
+
 // Abstract base class for test video sources, which provide a stream of
 // vpx_image_t images with associated timestamps and duration.
 class VideoSource {
diff --git a/test/y4m_test.cc b/test/y4m_test.cc
index 73ff683086d4b9f2305e8926980d63e1fefe92c5..3927b7da4888e5d741571cc8b3f4945572ec8117 100644
--- a/test/y4m_test.cc
+++ b/test/y4m_test.cc
@@ -138,14 +138,11 @@ INSTANTIATE_TEST_CASE_P(C, Y4mVideoSourceTest,
 class Y4mVideoWriteTest
     : public Y4mVideoSourceTest {
  protected:
-  Y4mVideoWriteTest() : Y4mVideoSourceTest() {}
+  Y4mVideoWriteTest() {}
 
-  virtual void ReplaceInputFp(FILE *input_file) {
+  virtual ~Y4mVideoWriteTest() {
     CloseSource();
-    frame_ = 0;
-    input_file_ = input_file;
-    rewind(input_file_);
-    ReadSourceToStart();
+    delete tmpfile_;
   }
 
   // Writes out a y4m file and then reads it back
@@ -153,26 +150,28 @@ class Y4mVideoWriteTest
     ASSERT_TRUE(input_file_ != NULL);
     char buf[Y4M_BUFFER_SIZE] = {0};
     const struct VpxRational framerate = {y4m_.fps_n, y4m_.fps_d};
-    FILE *out_file = libvpx_test::OpenTempOutFile();
-    ASSERT_TRUE(out_file != NULL);
+    tmpfile_ = new libvpx_test::TempOutFile;
+    ASSERT_TRUE(tmpfile_->file() != NULL);
     y4m_write_file_header(buf, sizeof(buf),
                           kWidth, kHeight,
                           &framerate, y4m_.vpx_fmt,
                           y4m_.bit_depth);
-    fputs(buf, out_file);
+    fputs(buf, tmpfile_->file());
     for (unsigned int i = start_; i < limit_; i++) {
       y4m_write_frame_header(buf, sizeof(buf));
-      fputs(buf, out_file);
-      write_image_file(img(), out_file);
+      fputs(buf, tmpfile_->file());
+      write_image_file(img(), tmpfile_->file());
       Next();
     }
-    ReplaceInputFp(out_file);
+    tmpfile_->CloseFile();
+    Y4mVideoSourceTest::Init(tmpfile_->file_name(), limit_);
   }
 
   virtual void Init(const std::string &file_name, int limit) {
     Y4mVideoSourceTest::Init(file_name, limit);
     WriteY4mAndReadBack();
   }
+  libvpx_test::TempOutFile *tmpfile_;
 };
 
 TEST_P(Y4mVideoWriteTest, WriteTest) {
diff --git a/y4menc.c b/y4menc.c
index 9211452a4858cb5dbe1031f0b1e1787705c6ca3f..b647e8dcc5e891a3e57ea637be4dd40102e081c4 100644
--- a/y4menc.c
+++ b/y4menc.c
@@ -48,6 +48,7 @@ int y4m_write_file_header(char *buf, size_t len, int width, int height,
               "C420p16 XYSCSS=420P16\n";
       break;
     default:
+      color = NULL;
       assert(0);
   }
   return snprintf(buf, len, "YUV4MPEG2 W%u H%u F%u:%u I%c %s", width, height,