RTMP to HLS: Convert using FFMPEG or NGINX


RTMP to HLS: Convert using FFMPEG or NGINX

RTMP to HLS: Convert using FFMPEG or NGINX

Real-Time Messaging Protocol (RTMP) and HTTP Live Streaming (HLS) are two popular protocols used for streaming video content over the internet. RTMP, developed by Adobe, is commonly used for live streaming to platforms like YouTube and Twitch. HLS, developed by Apple, is widely supported on iOS devices and HTML5 players. Converting RTMP streams to HLS can help you reach a broader audience, as HLS is more compatible with modern web standards and devices.

This article explains how to convert RTMP to HLS using two popular tools: FFMPEG and NGINX.

How to Convert RTMP to HLS Using FFMPEG

FFMPEG is a powerful open-source tool that can handle multimedia data, including video, audio, and other streams. Here's a step-by-step guide to converting RTMP to HLS using FFMPEG:

  1. Install FFMPEG:

    First, you need to install FFMPEG on your system. You can download it from the official website and follow the installation instructions for your operating system.

  2. Prepare the RTMP Stream:

    Ensure you have an RTMP stream URL ready for conversion.

  3. Run the FFMPEG Command:

    Open your terminal or command prompt and run the following command:

    ffmpeg -i rtmp://your_rtmp_stream_url -c:v libx264 -crf 18 -preset veryfast -c:a aac -b:a 160k -f hls -hls_time 10 -hls_list_size 6 -hls_wrap 10 -start_number 1 output.m3u8
    • -i rtmp://your_rtmp_stream_url: The input RTMP stream URL.
    • -c:v libx264: Use the H.264 video codec.
    • -crf 18: Set the Constant Rate Factor (CRF) to 18 for high quality.
    • -preset veryfast: Use the "veryfast" preset for encoding speed.
    • -c:a aac: Use the AAC audio codec.
    • -b:a 160k: Set the audio bitrate to 160 kbps.
    • -f hls: Specify HLS as the output format.
    • -hls_time 10: Set the segment length to 10 seconds.
    • -hls_list_size 6: Maintain a playlist with the last 6 segments.
    • -hls_wrap 10: Wrap segment numbers to 10.
    • -start_number 1: Start the segment numbering at 1.
    • output.m3u8: The output HLS playlist file.
  4. Access the HLS Stream:

    Once the command is executed, you will have an HLS playlist file (output.m3u8) and corresponding segment files. You can now use a media player or a web server to serve the HLS stream.

How to Convert RTMP to HLS Using NGINX

NGINX is a high-performance web server that can also be used for media streaming. By adding the NGINX-RTMP module, you can convert RTMP streams to HLS. Here's how:

  1. Install NGINX with RTMP Module:
    • On Debian/Ubuntu:

      sudo apt-get install nginx libnginx-mod-rtmp

    • On CentOS/RHEL:

      sudo yum install epel-release
      sudo yum install nginx nginx-mod-rtmp

  2. Configure NGINX:

    Open the NGINX configuration file (nginx.conf) in a text editor:

    sudo nano /etc/nginx/nginx.conf

    Add the following configuration to the file:

    rtmp { server { listen 1935; chunk_size 4096; application live { live on; record off; hls on; hls_path /tmp/hls; hls_fragment 3; hls_playlist_length 10; } } } http { server { listen 8080; location /hls { types { application/vnd.apple.mpegurl m3u8; video/mp2t ts; } root /tmp; add_header Cache-Control no-cache; } } }
  3. Start NGINX:

    Start or restart NGINX to apply the changes:

    sudo systemctl restart nginx
  4. Push RTMP Stream to NGINX:

    Use a tool like OBS Studio to push your RTMP stream to your NGINX server. Set the stream URL to rtmp://your_server_ip/live/stream_name.

  5. Access the HLS Stream: The HLS stream will be available at http://your_server_ip:8080/hls/stream_name.m3u8. You can use a media player or embed the stream on a webpage to view it.

By following these steps, you can successfully convert RTMP streams to HLS using either FFMPEG or NGINX, making your live streams more accessible and compatible with a wider range of devices and platforms.

Read also:

Technology Partners

VXG works with the biggest and best names in the video business