RTMP Streaming using FFmpeg Tutorial

In this article, we will go through the process of RTMP streaming using FFmpeg. For more details about the RTMP streaming protocol, please refer to this article on the basics of RTMP and other low-latency streaming protocols.

What is RTMP protocol?

Real Time Messaging Protocol (RTMP) is a TCP-based communication protocol. It provides a bidirectional message multiplex service and is mainly used to carry the streams of videos, audios and messages.

Steps Involved in RTMP Video Streaming using FFmpeg

To have a better understanding of the complete process of video streaming using RTMP, we can represent the steps involved using the following diagram :

RTMP FFmpeg

Let’s discuss each of the steps in detail.

Step 1 : Encoding

The encoder encodes the audio/video using any one of the supported codecs and transfers the data to the RTMP-SERVER using the RTMP protocol.
We would be using FFmpeg to encode and send data to the server. Following is the FFmpeg command to encode the video and transfer it to the server.

ffmpeg -re -i crowdrun.mp4 -c:v libx264 -c:v aac -f flv rtmp://localhost/show/stream

Let’s go through the meaning of the parameters used in the above command.

-reIt is an input parameter that instructs FFmpeg to read the same number of frames per second as the framerate of the input video. It is most used for live streaming or with a camera input.
-i crowdrun.mp4 It is the input video that we are using for the discussion and can be downloaded from here
-c:v libx264 Here we are specifying the encoder to be used for the video
-c:v aacSimilarly, here we are specifying the audio encoder
-f flv It is an output parameter that defines the output container format of the video. For the RTMP protocol, we need to use the flv (Flash Video) format.
rtmp://localhost/show/stream This is the target RTMP destination for the video. The url is based on the configuration of the streaming server.

Running the above command on the terminal after the streaming server is configured, would look like this:

FFmpeg to stream RTMP
RTMP streaming using FFmpeg

Now, the video is being encoded and transferred to the server.

Let’s move to the next step.

Step 2: Serving 

The RTMP-SERVER receives the feed and is responsible for scaling and delivering the content to a large number of audiences over the internet. 

For our test, we have used the NginX server along with the RTMP module. The server application is installed and configured in local and hence the localhost is being used as the url. It is configured to serve the content using the same RTMP protocol to the end user. 

Related:  Low Latency Streaming Protocols SRT, WebRTC, LL-HLS, UDP, TCP, RTMP Explained

Step 3: Consuming 

The last step of the process is to receive the content and consume it on the end user’s device. Since the server is configured for using the RTMP protocol for serving, we will again use the same to demonstrate this process. Although, nowadays, many other alternate HTTP based protocols such as HLS are being used for this due to their other advantages. 

As discussed in the previous sections, the target address we have used to publish the video stream was : rtmp://localhost/show/stream 

We can use the VLC player which supports RTMP to demonstrate the playback of the video.

RTMP streaming using FFmpeg

Use the network stream option in VLC and add the RTMP url.

RTMP streaming using FFmpeg

Conclusion

In this article, we learned how to stream video using the RTMP video streaming protocol using FFmpeg. This opens up a world of opportunities as one can now stream to different video streaming platforms like Facebook and Youtube. I hope you found this useful and do come back and read the rest of the articles on OTTVerse.

Be the first to comment

Leave a Reply

Your email address will not be published.


*