title: Streamlink Streamdata processing in OpenCV Python
body: <p>I'm trying to read a twitch stream via streamlink (<a href="https://streamlink.github.io/api_guide.html" rel="nofollow noreferrer">https://streamlink.github.io/api_guide.html</a>)
  into OpenCV for further processing in Python.</p>
  
  <p>What works, reading the stream into a stream.ts file via popen and then into opencv:</p>
  
  <pre><code>import subprocess
  import os
  import time
  
  def create_new_streaming_file(stream_filename="stream0", stream_link="https://www.twitch.tv/tsm_viss"):
  
              try:
                  os.remove('./Engine/streaming_util/'+stream_filename+'.ts')
              except OSError:
                  pass
  
              cmd = "streamlink --force --output ./Engine/streaming_util/"+stream_filename+".ts "+stream_link+" best"
              subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
  
  create_new_streaming_file()
  
  video_capture = cv2.VideoCapture('./Engine/streaming_util/stream0.ts')
  </code></pre>
  
  <p>Which is very slow and the stream stops after about 30 seconds.</p>
  
  <p>I would like to read the bytestream directly into openCV out of streamlink's python api.
  What works is printing out the latest n bytes of a stream into the console:</p>
  
  <pre><code>import streamlink
  
  streams = streamlink.streams("https://www.twitch.tv/grimmmz")
  stream = streams["best"]
  fd = stream.open()
  
  while True:
      data = fd.read(1024)
      print(data)
  </code></pre>
  
  <p>I'm looking for something like this (does not work but you'll get the concept):</p>
  
  <pre><code>streams = streamlink.streams("https://www.twitch.tv/grimmmz")
  stream = streams["best"]
  fd = stream.open()
  bytes=''
  
  
  while True:
  # to read mjpeg frame -
      bytes+= fd.read(1024)
  
      a = bytes.find('\xff\xd8')
      b = bytes.find('\xff\xd9')
  
      if a!=-1 and b!=-1:
          jpg = bytes[a:b+2]
          bytes= bytes[b+2:]
          img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
          cv2.imwrite('messigray.png', img)
          cv2.imshow('cam2', img)
  
      else:
          continue
  </code></pre>
  
  <p>Thanks a lot in advance!</p>