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:a 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:

Related:  RTMP (Real-Time Messaging Protocol) - How Does It Work

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. 

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.

5 thoughts on “RTMP Streaming using FFmpeg Tutorial”

  1. Dear OTTVerse,
    There is a typing error in the command,
    the ‘-c:v aac’ should be ‘-c:a aac’ (for audio codec specification).

  2. Hi,
    It would be helpful to provide more info about NginX RTMP server setup which provides the url “rtmp://localhost/show/stream ” in this article

  3. I am working with ffmpeg and rtmp camera streaming in a constrained environment. I have rtmp server running on a cloud (say eg. 11.23.123.13 in port 1935 for rtmp & 4936 for rtmps). However is there any port on sending end , i should request team to open to avoid blocking the stream sending from device end to rtmp server end(camera streaming end). Only port 443 is open in device where camera connected and ffmpeg commands are running.

    It would be helpful if somebody give insights on how network is connecting from ffmpeg encoder to rtmp server

    Getting the details how network is connecting from ffmpeg encoder to rtmp server?
    Which ports to be open in device end(where camera connecting) to start ffmpeg streaming to rtmp server running on cloud?

Leave a Comment

Your email address will not be published. Required fields are marked *

Enjoying this article? Subscribe to OTTVerse and receive exclusive news and information from the OTT Industry.