I am no expert on ffmpeg
, so I will defer to anybody who knows better and delete my answer if it proves incorrect.
As far as I can see, you have an RTMP stream that you want to ingest into OpenCV. OpenCV uses Numpy arrays with BGR ordering to store images - and video frames obviously, which are just lots of images one after the other. So, I would suggest you ask ffmpeg
to convert the Flash video stream to exactly what OpenCV wants:
ffmpeg <RTMP INPUT STUFF> -pix_fmt bgr24 -f rawvideo -
and then change this since it is now BGR888:
self.fs = width * height * 3
As I don't have an RTMP source available, I generated a test stream like this:
# Generate raw video stream to read into OpenCV
ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -pixel_format rgb24 -f rawvideo -
And then I piped that into Python with:
ffmpeg -f lavfi -i testsrc=duration=10:size=640x480:rate=30 -pixel_format rgb24 -f rawvideo - | ./PlayRawVideo
The Python program PlayRawVideo
looks like this:
#!/usr/bin/env python3
import numpy as np
import cv2
import sys
# Set width and height
w, h = 640, 480
while True:
data = sys.stdin.buffer.read(w * h *3)
if len(data) == 0:
break
frame = np.frombuffer(data, dtype=np.uint8).reshape((h, w, 3))
cv2.imshow("Stream", frame)
cv2.waitKey(1)
Note that I had to use sys.stdin.buffer.read()
to get raw binary data.
