0

Im looking for a way to extract one single image from an rtsp stream. My current solution is based on doing that with opencv, but i want to optimize this. So I'm going with a solution I found here Handle large number of rtsp cameras without real-time constraint

The drawback here is that ffmpeg opens a whole videostream, so a consequence is that this is not more efficient than opencv. I want to fetch one single image with ffmpeg, but currently im not able to do that. i get error "ValueError: cannot reshape array of size 0 into shape (160,240,3)" - So there is no image in the frame. I'm not completely convinced that my ffmpeg_cmd is correct.

import numpy as np
import subprocess as sp
import cv2

# Use public RTSP Streaming for testing:
in_stream = "rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4"

# Use OpenCV for getting the video resolution.
cap = cv2.VideoCapture(in_stream)

# Get resolution of input video
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

# Release VideoCapture - it was used just for getting video resolution
cap.release()

# http://zulko.github.io/blog/2013/09/27/read-and-write-video-frames-in-python-using-ffmpeg/
FFMPEG_BIN = "ffmpeg"

ffmpeg_cmd = [FFMPEG_BIN,
              '-vframes', '1',
              '-rtsp_transport', 'tcp'
              '-i', in_stream,'-c:v','mjpeg','-f','image2pipe', 'pipe:']

process = sp.Popen(ffmpeg_cmd, stdout=sp.PIPE)

raw_frame = process.stdout.read(width * height * 3)

frame = np.frombuffer(raw_frame, np.uint8).reshape((height, width, 3))

process.stdout.close()
process.wait()
cv2.destroyAllWindows()

If someone have a completely different solution, thats ok. I need something that is more efficient than doing it with opencv.

kirkegaard
  • 1,058
  • 2
  • 12
  • 32
  • If you're just reading one frame/feed, opencv is likely more efficient. As for reading a raw (RGB?) frame in ffmpeg, you're using a wrong codec. Try `..., -f rawvideo -pix_fmt rgb24 pipe:`. This should return a bytes data that conforms to the rest of your code. – kesh Oct 05 '22 at 13:03
  • BTW, you may want to look into `pyav` package, which could result in more efficient use of libav library in Python. – kesh Oct 05 '22 at 13:05
  • Thanks for reply. Is pyav more efficient than opencv for one frame? – kirkegaard Oct 05 '22 at 13:26

0 Answers0