简体   繁体   中英

How should I should I configure NGINX rtmp server so I can publish both a rtmp stream and hls at the same time

I currently have an ffmpeg bash script running that pulls an RTSP feed from a live camera feed and then publishes it as RTM` to my Nginx server.

The requirements for the server is that uses can subscribe to both the RTMP feed that the Nginx server publishes or the HLS stream.

The problem I'm finding is that the RTMP stream freezes after a while, although the HLS stream continues to works.

I'm wondering if this is because it's separate process and not one executes by Nginx itself via it's exec command.

Also I've done not tuning on this server so I have no idea I need to change the configuration some.

Here's my two scripts.

#!/bin/bash

sleep 1

    VID_SOURCE="rtsp://camerastream.com/MediaInput/h264/stream_"
    VIDEO_OPTS="-vcodec libx264 -video_size 1280x720 -b:v 2048k"
    AUDIO_OPTS="-c:a copy"
    VID_OUTPUT="rtmp://localserver:1935/live/livestream"
    
    ffmpeg -rtsp_transport tcp -r 15 -i $VID_SOURCE $VIDEO_OPTS $AUDIO_OPTS -f flv $VID_OUTPUT

And then my Nginx configuration

vents {}

rtmp { 
    server { 
        listen 1935; 

        application live { 
            live on; 
            interleave on;

            # Turn on HLS
            hls on;
            hls_path /tmp/hls;
            hls_playlist_length 15s;

        }
    } 
} 
 
http {
    default_type application/octet-stream;
 
   server {
        listen 8080;

        location /hls {

            # Disable cache
            add_header 'Cache-Control' 'no-cache';

            # CORS setup
            add_header 'Access-Control-Allow-Origin' '*' always;
            add_header 'Access-Control-Expose-Headers' 'Content-Length';

            # allow CORS preflight requests
            if ($request_method = 'OPTIONS') {
             add_header 'Access-Control-Allow-Origin' '*';
             add_header 'Access-Control-Max-Age' 1728000;
             add_header 'Content-Type' 'text/plain charset=UTF-8';
             add_header 'Content-Length' 0;
             return 204;
            }

            types {
            application/dash+xml mpd;
            application/vnd.apple.mpegurl m3u8;
            video/mp2t ts;
            }

            root /tmp/;

        }
    }
}

The goal is to have this one server where subscribers could watch the rtmp feed or the HLS one

You're able to play the RTMP and HLS stream. I think the problem is not about the protocol, but the content or timestamp from your camera.

The problem I'm finding is that the RTMP stream freezes after a while,
although the HLS stream continues to works.

You could try:

  1. Use smaller bitrate, like -b:v 500k
  2. Also transcode the audio stream, or disable the audio.
  3. Try another media server, to enable the atc by-pass the timestamp.
  4. Try another RTMP player, like VLC or ffplay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM