title: Pipe video frame to OpenCV image and then to FFmpeg
body: <p>There is a similar question here: 
  <a href="https://stackoverflow.com/questions/38156265/getting-av-interleaved-write-frame-broken-pipe-error">Getting 'av_interleaved_write_frame(): Broken pipe' error
  </a></p>
  
  <p>But what should I do if I want to write the data?</p>
  
  <p>I put  <code>pipe_out.stdin.write(image.tostring())</code> in the while loop, like this</p>
  
  <pre><code>FFMPEG_BIN = "/home/media/Downloads/ffmpeg"
  import subprocess as sp
  import sys
  width = 360
  height = 240
  command_in = [ FFMPEG_BIN,
              '-i', '/home/media/Videos/mytestvideo/zhou.avi',
              '-f', 'image2pipe',
              '-pix_fmt', 'bgr24',
              '-vcodec', 'rawvideo', '-']
  pipe_in = sp.Popen(command_in, stdout = sp.PIPE, bufsize = 10**8)
  
  command_out = [ FFMPEG_BIN,
          '-y', # (optional) overwrite output file if it exists
          '-f', 'rawvideo',
          '-vcodec','rawvideo',
          '-s', '360x240', # size of one frame
          '-pix_fmt', 'bgr24',
          '-r', '28', # frames per second
          '-i', '-', # The imput comes from a pipe
          '-an', # Tells FFMPEG not to expect any audio
          #'-vcodec', 'mpeg',
          'my_output_videofile.mp4' ]
  
  pipe_out = sp.Popen( command_out, stdin=sp.PIPE, stderr=sp.PIPE)
  
  import numpy
  import cv2
  import pylab
  # read width*height*3 bytes (= 1 frame)
  while True:
      raw_image = pipe_in.stdout.read(width*height*3)
      image =  numpy.fromstring(raw_image, dtype='uint8')
      image = image.reshape((height,width,3))
      pipe_in.communicate()
      pipe_out.stdin.write(image.tostring())
      pipe_out.communicate()
      pipe_in.stdout.flush()
      #cv2.imshow('image',image)
      #cv2.waitKey(0)
      # throw away the data in the pipe's buffer.
  
  
  '''
  pipe_in.stdin.close()
  pipe_in.stderr.close()
  pipe_in.wait()
  pipe_out.stdout.close()
  pipe_out.stderr.close()
  pipe_out.wait()
  '''
  #pipe_out.stdin.write(image.tostring())
  </code></pre>
  
  <p>However, the output video has only 1 frame(the first frame of input video)</p>
  
  <p>Any ideas?</p>
  
  <p>Thanks!</p>