Set RTMP Stream Key and Publishing Mode in FFmpeg

This article explains how to properly configure an RTMP URL, include your stream key, and define the publishing mode (such as live or record) when using FFmpeg. You will learn the correct command-line syntax and URL structure required to stream successfully to platforms like YouTube, Twitch, or custom RTMP servers.

The RTMP URL and Stream Key Structure

In FFmpeg, the RTMP destination is defined as a single URL. The stream key is typically appended to the end of the URL path.

The standard format is: rtmp://[server_ip_or_domain]/[application_name]/[stream_key]

For example, if your server URL is rtmp://live.example.com/app and your stream key is secret_key_123, the complete output target in FFmpeg will be: rtmp://live.example.com/app/secret_key_123

An example FFmpeg command using this structure:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://live.example.com/app/secret_key_123

Setting the Publishing Mode

By default, FFmpeg streams in “live” mode. However, if your RTMP server (like Adobe Media Server or Red5) supports different publishing modes—such as recording the stream on the server or appending to an existing file—you must specify this using the -rtmp_live option.

FFmpeg’s -rtmp_live option accepts the following values: * live: Streams the content as a live broadcast (default). * recorded: Instructs the server to record the stream as a file. * any: Allows the server to decide the mode.

Example: Streaming in Live Mode

To explicitly set the mode to live:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f flv -rtmp_live live rtmp://live.example.com/app/secret_key_123

Example: Streaming in Record Mode

To instruct the server to record the incoming stream:

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f flv -rtmp_live recorded rtmp://live.example.com/app/secret_key_123

Handling Complex Stream Keys with Playpath

If your stream key contains slashes, special characters, or query parameters, appending it directly to the URL can cause parsing errors. To prevent this, use the -rtmp_playpath option to isolate the stream key from the base RTMP URL.

ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f flv -rtmp_playpath "secret/stream/key?auth=123" rtmp://live.example.com/app