DEV Community

Cover image for Streaming Video to AWS MediaConnect Using FFmpeg and SRT Protocol: A Complete Guide
Md Mohaymenul Islam (Noyon)
Md Mohaymenul Islam (Noyon)

Posted on

Streaming Video to AWS MediaConnect Using FFmpeg and SRT Protocol: A Complete Guide

Introduction

In today's multimedia-driven world, streaming video content efficiently and reliably is more critical than ever. AWS MediaConnect and the Secure Reliable Transport (SRT) protocol provide robust solutions for broadcasting video streams across unstable networks. Why Secure Reliable Transport (SRT) protocol? This blog post will guide you through setting up FFmpeg with SRT support and streaming video files or live camera feeds to AWS MediaConnect. We will also cover how to containerize this setup using Docker for more streamlined deployment.


Prerequisites

  • Ubuntu/Debian or CentOS/RHEL system for setting up FFmpeg
  • Basic knowledge of terminal commands
  • Docker installed on your system for the Docker part of the tutorial
  • Access to AWS MediaConnect for testing the streaming setup

Step-by-Step Guide

Step 1: Installing Required Dependencies

To compile FFmpeg with SRT support, start by installing the necessary tools and libraries:

For Ubuntu/Debian:



sudo apt update
sudo apt install autoconf automake build-essential cmake git-core libass-dev libfreetype6-dev \
libsdl2-dev libtool libva-dev libvdpau-dev libvorbis-dev libxcb1-dev libxcb-shm0-dev \
libxcb-xfixes0-dev pkg-config texinfo wget yasm zlib1g-dev libx264-dev libx265-dev \
libnuma-dev libvpx-dev libfdk-aac-dev libmp3lame-dev libopus-dev


Enter fullscreen mode Exit fullscreen mode

For CentOS/RHEL:



sudo yum groupinstall "Development Tools"
sudo yum install autoconf automake cmake freetype-devel git libtool mercurial pkgconfig \
zlib-devel gcc-c++ libX11-devel libXfixes-devel SDL2-devel texinfo wget yasm \
libXv-devel libXinerama-devel libass-devel


Enter fullscreen mode Exit fullscreen mode

Step 2: Installing SRT (Secure Reliable Transport)

Clone and install the SRT library from its GitHub repository:



git clone https://github.com/Haivision/srt.git
cd srt
./configure
make
sudo make install


Enter fullscreen mode Exit fullscreen mode

Step 3: Compiling FFmpeg with SRT Support

Compile FFmpeg with enabled SRT support:



cd ~
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
./configure --enable-gpl --enable-nonfree --enable-libsrt --enable-libx264 --enable-libx265 \
--enable-libvpx --enable-libfdk-aac --enable-libmp3lame --enable-libopus
make
sudo make install


Enter fullscreen mode Exit fullscreen mode

Verify the installation:



ffmpeg -protocols | grep srt


Enter fullscreen mode Exit fullscreen mode

You should see srt in the output, confirming SRT support.

Step 4: Testing FFmpeg

Test your FFmpeg setup by converting a video file:



ffmpeg -i video.mp4 -c:v libx264 -c:a aac output.mp4


Enter fullscreen mode Exit fullscreen mode

Step 5: Create an AWS MediaConnect Flow

After configuring FFmpeg and your local setup to handle SRT streaming, the next step involves setting up an AWS MediaConnect flow that will receive your stream. Here’s how to create this flow:

  1. Open AWS MediaConnect service.

  2. Create a New Flow:

    • Click on “Create flow”.
    • Enter the flow details as described below:

Image description

Details Section:

  • Name: Provide a unique name for the flow, e.g., TestFlow.
  • Availability Zone: You can select the default option or choose a specific one if you have a preference.

Source Section:

  • Source type: Select Standard source since this content comes from a non-entitlement, on-premises encoder.
  • Name: Name your source, e.g., TestFlowSource.
  • Protocol: Choose SRT Listener as the protocol.
  • Allowlist CIDR Block: Input the CIDR block that fits your network security policies. For example, to allow any IP, you can use 0.0.0.0/0 (not recommended, it will be open for the world).
  • Inbound port: Typically, SRT uses port 5000, but you can specify any port that you prefer and is open in your network firewall.
  • Minimum Latency (Optional): Set this according to your latency requirements; 1000 milliseconds is a typical starting point.

Decryption Info (if applicable):

  • If your source is encrypted, enable decryption and specify the necessary details. This might include entering decryption keys managed in AWS Secrets Manager.
  1. Create the Flow:

    • After filling out the form, click “Create flow” at the bottom of the page. AWS will set up the flow and allocate resources, including generating the endpoint IP address and port number.
  2. Obtain the MediaConnect IP and Port:

    • Once the flow is created, go to the details page of the flow. Under the source section, you will see the IP address and port number. This is the endpoint to which you will stream your video using FFmpeg.

Image description

Note: The IP address and port provided by MediaConnect are crucial for setting up your FFmpeg command correctly. You will replace the SRT_URL in your FFmpeg command with this new endpoint, like so:



srt://<MediaConnect_IP>:<MediaConnect_Port>  
Ex: srt://65.0.244.153:5000


Enter fullscreen mode Exit fullscreen mode

Step 6: Streaming Video File to AWS MediaConnect

Use the following FFmpeg command to stream your video:



ffmpeg -re -stream_loop -1 -i './video.mp4' -c copy -f mpegts 'srt://65.0.244.153:5000'  
# Replace with your video file and actual MediaConnect SRT URL and port


Enter fullscreen mode Exit fullscreen mode

OR

Setting Up Docker for Streaming

To simplify deployment, you can containerize the FFmpeg setup using Docker:

Dockerfile for FFmpeg and SRT



FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y autoconf automake build-essential cmake git libtool libssl-dev pkg-config nasm yasm t

clsh

RUN git clone https://github.com/Haivision/srt.git /usr/local/src/srt && \
    cd /usr/local/src/srt && \
    ./configure && \
    make && \
    make install

RUN git clone --depth 1 https://git.ffmpeg.org/ffmpeg.git /usr/local/src/ffmpeg && \
    cd /usr/local/src/ffmpeg && \
    ./configure --enable-libsrt --enable-gpl --enable-nonfree && \
    make && \
    make install

RUN echo "/usr/local/lib" > /etc/ld.so.conf.d/local.conf && ldconfig
WORKDIR /data
ENV VIDEO_PATH /data/video.mp4
ENV SRT_URL srt://localhost:5000
ENTRYPOINT ["/bin/bash", "-c"]
CMD ["ffmpeg -re -stream_loop -1 -i \"$VIDEO_PATH\" -c copy -f mpegts \"$SRT_URL\""]

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*


Enter fullscreen mode Exit fullscreen mode

Build and run the Docker container:



docker build -t ffmpeg-srt-streamer .
docker run -v "/path/to/videos:/data" -e VIDEO_PATH='/data/video.mp4' -e SRT_URL='srt://192.168.1.100:5000' ffmpeg-srt-streamer  
# Replace with your video file and actual MediaConnect SRT URL and port


Enter fullscreen mode Exit fullscreen mode

Image description


Viewing Stream Metrics in AWS MediaConnect

In the AWS Elemental MediaConnect console, you can view detailed metrics about the stream that you are broadcasting
Image description

Check Source Metrics:

  • Source bitrate: This graph shows the bitrate of the incoming stream in bits per second (bps). A consistent bitrate close to your expected settings indicates a stable connection. For instance, as shown in the screenshot, the bitrate graph fluctuates around 4 million bits per second, which typically reflects the expected data flow for a high-quality video stream.
  • Source total packets: This metric displays the total number of packets received from the source. Regular fluctuations in this graph are normal as video data transmission can vary slightly with each segment of the video.

  • AWS Elemental MediaConnect integrates with Amazon CloudWatch, allowing you to view more detailed metrics by clicking the 'View in CloudWatch' option. This can give insights into packet loss, errors, and other performance metrics that help in optimizing the streaming quality.

How to React to Metrics

  • High Bitrate Fluctuations: If you notice significant drops in the bitrate graph, it might indicate a problem with the network or the source encoder. Check your network settings or encoder performance.
  • Packet Loss: If the total packets graph shows a drop in packets, this could signify packet loss which might result in degraded video quality or interruptions in the stream.

Configuring and Using Outputs in AWS MediaConnect

Once your video stream is successfully being sent to AWS MediaConnect, setting up an output allows you to distribute or view the stream externally.

Creating an Output in AWS MediaConnect

Image description

  • Name: Enter a name for your output, e.g., NoyonPC.
  • Description: Provide a meaningful description, such as "Test output for Noyon".
  • Output type: Choose "Standard output".
  • Protocol: Select "SRT Listener" to use the SRT protocol for the output.
  • Minimum latency: Set this according to your latency requirements (e.g., 2000 milliseconds).
  • Port: Specify a port for the output, e.g., 1030.
  • CIDR allow list: Enter the IP range allowed to access this output, for example, 0.0.0.0/0 for global access (note that setting to 0.0.0.0/0 should be done with caution as it allows access from any IP).

Using the Output in VLC Media Player
Once the output is configured, you can use VLC Media Player to view the stream:

  • Navigate to "Media" > "Open Network Stream" or press Ctrl+N.
  • Enter the stream address in the format srt://:.

Image description
If everything is set up correctly, you should now see the video streaming in VLC Media Player.


Conclusion

This comprehensive guide provides all the steps needed to set up FFmpeg with SRT support and stream video to AWS MediaConnect, using FFmpeg commands. Additionally, by containerizing the setup with Docker, you ensure a more manageable and replicable streaming environment. Whether you're broadcasting MP4 files or live camera feeds, these setups ensure robust, efficient delivery over potentially unreliable networks.

Thanks for reading! Happy Cloud Computing!

Connect with me: Linkedin

Top comments (0)