Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Setting Up a Basic Live Streaming Server with Nginx and RTMP

Tech May 17 4

To establish a live streaming server, we'll leverage Nginx along with the nginx-rtmp-module. This guide outlines the steps from obtaining the necessary components to configuring the server and streaming content. First, obtain the nginx-rtmp-module:


cd /data/nginx
yum install git
git clone https://github.com/arut/nginx-rtmp-module.git

Next, install Nginx with the RTMP module. Ensure you have development tools and libraries installed: ```

yum install gcc make pcre-devel openssl-devel wget http://nginx.org/download/nginx-1.15.0.tar.gz tar xf nginx-1.15.0.tar.gz cd nginx-1.15.0 ./configure --with-http_ssl_module --add-module=../nginx-rtmp-module make && make install


Modify the Nginx configuration file (`nginx.conf`) to include RTMP server settings. The following configuration enables RTMP listening on port 1935 and sets up an HLS (HTTP Live Streaming) application: ```

rtmp {
    server {
        listen 1935;              # Port for RTMP connections
        chunk_size 4000;          # Adjust chunk size; lower values increase CPU, minimum 128

        application hls {         # Application name for RTMP stream
            live on;              # Enable live streaming
            hls on;               # Enable HLS output
            hls_path /usr/share/nginx/html/hls; # Directory to store HLS segments; ensure write permissions
            hls_fragment 5s;      # Duration of each HLS segment in seconds
        }
    }
}

Once Nginx is configured, you can push streams to it. **Using FFmpeg for Streaming:** The following command pushes an MP4 file as a live stream: ```

ffmpeg -re -i /path/to/your/video.mp4 -vcodec copy -acodec copy -f flv rtmp://YOUR_SERVER_IP:1935/hls


Replace `YOUR\_SERVER\_IP` with you're server's IP address. \*\*Using OBS Studio for Streaimng:\*\* 1. Download and install OBS Studio. 2. Go to Settings -> Stream. 3. Set the Server URL to `rtmp://YOUR\_SERVER\_IP:1935/hls`. 4. Use a Stream Key (e.g., a room number) to identify your stream. You can also use VLC or other sources to stream content. After starting the stream, check the directory specified in `hls\_path` (e.g., `/usr/share/nginx/html/hls`) for generated `.m3u8` and `.ts` files. To view the live stream, you can use various clients. \*\*Using an HTML5 Browser:\*\* Create an HTML file (e.g., `play.html`) with the following content: ```

<video controls width="640" height="360">
    <source src="http://YOUR_SERVER_IP/hls/stream_key.m3u8" type="application/x-mpegURL"/>
    Your browser does not support HTML5 video.
</video>

Replace YOUR\_SERVER\_IP and stream\_key accordingly. Access this HTML file through you're web server. **Using VLC Media Player:** Open VLC and go to Media -> Open Network Stream. Enter the following URL: ```

http://YOUR_SERVER_IP/hls/stream_key.m3u8


Replace `YOUR\_SERVER\_IP` and `stream\_key` as needed. 
Tags: nginx

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.