title: Pipe video frames from ffmpeg to numpy array without loading whole movie into memory
body: <p>I'm not sure whether what I'm asking is feasible or functional, but I'm experimenting with trying to load frames from a video in an ordered, but &quot;on-demand,&quot; fashion.</p>
  <p>Basically what I have now is to read the entire uncompressed video into a buffer by piping through <code>stdout</code>, e.g.:</p>
  <pre><code>H, W = 1080, 1920 # video dimensions
  video = '/path/to/video.mp4' # path to video
  
  # ffmpeg command
  command = [ &quot;ffmpeg&quot;,
              '-i', video,
              '-pix_fmt', 'rgb24',
              '-f', 'rawvideo',
              'pipe:1' ]
  
  # run ffmpeg and load all frames into numpy array (num_frames, H, W, 3)
  pipe = subprocess.run(command, stdout=subprocess.PIPE, bufsize=10**8)
  video = np.frombuffer(pipe.stdout, dtype=np.uint8).reshape(-1, H, W, 3)
  
  # or alternatively load individual frames in a loop
  nb_img = H*W*3 # H * W * 3 channels * 1-byte/channel
  for i in range(0, len(pipe.stdout), nb_img):
      img = np.frombuffer(pipe.stdout, dtype=np.uint8, count=nb_img, offset=i).reshape(H, W, 3)
  </code></pre>
  <p>I'm wondering if it's possible to do this same process, in Python, but without first loading the entire video into memory. In my mind, I'm picturing something like:</p>
  <ol>
  <li>open the a buffer</li>
  <li>seeking to memory locations on demand</li>
  <li>loading frames to numpy arrays</li>
  </ol>
  <p>I know there are other libraries, like OpenCV for example, that enable this same sort of behavior, but I'm wondering:</p>
  <ul>
  <li>Is it possible to do this operation efficiently using this sort of ffmpeg-pipe-to-numpy-array operation?</li>
  <li>Does this defeat the speed-up benefit of ffmpeg directly rather than seeking/loading through OpenCV or first extracting frames and then loading individual files?</li>
  </ul>