go2rtc icon indicating copy to clipboard operation
go2rtc copied to clipboard

How to convert RTSP to MJPEG

Open lucadici opened this issue 1 year ago • 2 comments

Hi all, I have a web application that for historical reasons can accept only MJPEG format, server-size pushed, with the famous "img" tag. Basically the browser issues an HTTP request and then it pretends that the server responds with a multipart content type: "Content-Type", "multipart/x-mixed-replace; boundary=--someboundary" Such response ends only when browser closes connection. Early millennium stuff.

Now I would like to stream contents from a camera that produces RTSP (in the example below I'm simulating it locally with a gstreamer pipe). I'm able to visualize the stream via WebRTC module by using this config:

...
rtsp:
  listen: ":8553" #just to avoid issue with gstreamer pipe listening on 8554
  username: ""
  password: ""
...
...
streams:
  camerah264: rtsp://127.0.0.1:8554/test

This correctly works in Chrome. Then I tried to follow the transcoding guidelines for MJPEG and changed the config as follows:

streams:
  - camerah264: rtsp://127.0.0.1:8554/test
  - cameramjpegconverted: ffmpeg:camerah264#video=mjpeg]

But once I try to open http://localhost:1984/stream.html?src=cameramjpegconverted&mode=mjpeg I get the following error: ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: EOF"

I also tried this:

streams:
  camera1:
    - rtsp://127.0.0.1:8554/test
    - ffmpeg:camera1#video=mjpeg

And by opening http://localhost:1984/stream.html?src=camera1&mode=mjpeg I get: ERR github.com/AlexxIT/go2rtc/internal/mjpeg/init.go:166 > error="streams: codecs not matched: H264 => JPEG"

What can be the problem?

Thanks and congrats for the great project!

lucadici avatar Mar 24 '24 06:03 lucadici

This works for me. But this only an example, ffmepg parameter should be optimized

streams:
  Test:
    - exec:ffmpeg -hwaccel vaapi -vaapi_device /dev/dri/renderD128 -i "rtsp://XXXXXXX" -c:v mjpeg -q:v 1 -f mpjpeg -an -

Scrounger avatar Apr 09 '24 10:04 Scrounger

In first config you have list inside streams section. This is wrong. Also you have bracket at the end - also wrong.

Second config right. Are you sure it's not working? You can increase log level to trace and check why ffmpeg can't start.

AlexxIT avatar Apr 23 '24 03:04 AlexxIT

Sorry for the delay. I totally forget to mention that the source uses h264 codec over rtsp. Hence the error "not matched h264 mjpeg". So actually I have two questions:

  • how to convert such h264 over rtsp to http(s) mjpeg?
  • would it be possible for go2rtc to proxy the rtsp stream to webrtc? As far as I understood webrtc is the best option to have low latency streaming on a browser

lucadici avatar Jul 25 '24 11:07 lucadici

  1. https://github.com/AlexxIT/go2rtc?tab=readme-ov-file#source-ffmpeg
  2. https://github.com/AlexxIT/go2rtc?tab=readme-ov-file

AlexxIT avatar Jul 26 '24 05:07 AlexxIT