deepch / RTSPtoWeb

RTSP Stream to WebBrowser
MIT License
1.32k stars 312 forks source link

Encode MJPEG into h264 and stream it by means of WebRTC #240

Open AlessandroMartinelli opened 2 years ago

AlessandroMartinelli commented 2 years ago

Hi, Thanks for RTSPtoWeb, it works really well with WebRTC.

I have a scenario where I need to use a videocamera unable to send a stream; it can only send raw frames to the PC it's connected to. It's easy to generate a MJPEG stream from such frames, but it's highly inefficient.

I'd like to encode the frames of this MJPEG stream in h264 and serve the h264 stream by means of WebRTC. Would it be possible to implement this feature? I'd be willing to pay for helping the development.

Thanks, Alessandro

roger- commented 2 years ago

I was trying to do something similar in #239

Best solution seems to be using a combination of ffmpeg and rtsp-simple-server.

AlessandroMartinelli commented 2 years ago

Best solution seems to be using a combination of ffmpeg and rtsp-simple-server.

I've tried that approach in the past but I've had various problems... could you please share the ffmpeg command you've used?

roger- commented 2 years ago

Sure, here's my docker-compose.yaml file:

version: "3.3"
services:
  rtsp-server:
    privileged: true  
    restart: unless-stopped
    ports:
      - "8554:8554"
      - "8888:8888"
    environment:
      - RTSP_PROTOCOLS=tcp
    image: aler9/rtsp-simple-server

  ffmpeg-1:
    privileged: true 
    restart: unless-stopped
    image: akashisn/ffmpeg:4.4.1-qsv  
    links:
      - "rtsp-server:rtsp-server"
    devices:
      - /dev/dri/renderD128
    depends_on:
      - rtsp-server
    command: ${FFMPEG_INPUT_OPTS} -i ${STREAM1_URL} ${FFMPEG_OUTPUT_OPTS} ${RTSP_SERVER}/${STREAM1_NAME} ${FFMPEG_OUTPUT_OPTS} 

and my .env file:

FFMPEG_INPUT_OPTS="-loglevel warning -hide_banner -hwaccel_output_format qsv -hwaccel qsv -qsv_device /dev/dri/renderD128 -avoid_negative_ts make_zero -fflags nobuffer -flags low_delay -strict experimental -fflags +genpts+discardcorrupt -use_wallclock_as_timestamps 1 -f mjpeg -re"
FFMPEG_OUTPUT_OPTS="-an -g 20 -c:v h264_qsv -r 10 -global_quality 25 -look_ahead 1 -f rtsp -rtsp_transport tcp"
RTSP_SERVER="rtsp://rtsp-server:8554"

STREAM1_NAME="stream1"
STREAM1_URL="http://xxx"

If you don't have an Intel CPU you can remove some of the hardware acceleration stuff. Incidentally this also gets you an HLS stream, so you may not even need RTSPtoWeb for browser playback.

(Inspired by https://github.com/blakeblackshear/frigate/issues/4153)