Set RTMP Stream Key and Publishing Mode in FFmpeg
This article provides a straightforward guide on how to configure the stream key and publishing mode when sending an RTMP stream using FFmpeg. By understanding the composition of the RTMP destination URL and utilizing FFmpeg’s dedicated RTMP flags, you can control stream authentication, stream targeting, and determine whether the remote server treats the broadcast as a real-time live event or records it directly to disk.
RTMP URL Structure
An RTMP URL is generally structured into three components: the server address, the application name, and the stream key:
rtmp://[server-address]:[port]/[application]/[stream-key]
- Server Address: The hostname or IP address of the
ingestion server (e.g.,
live.example.com). - Application: The RTMP application defined on the
server (e.g.,
live,app). - Stream Key: The unique identifier for your stream, which acts as both the stream's name and its access token.
1. Setting the Stream Key
The stream key is placed directly after the application name in the RTMP path.
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -f flv rtmp://live.example.com/app/mySecretStreamKeyIf your streaming provider separates the RTMP URL and the Stream Key (common with platforms like YouTube, Twitch, or Facebook), concatenate them with a slash:
- RTMP URL:
rtmp://a.rtmp.youtube.com/live2 - Stream Key:
xxxx-xxxx-xxxx-xxxx - FFmpeg Target:
rtmp://a.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx
If the stream key requires query parameters (e.g., for tokens or authentication), append them using standard URL query syntax:
rtmp://live.example.com/app/mySecretStreamKey?token=abc123xyz2. Setting the Publishing Mode
RTMP publishing modes instruct the receiving server how to handle the stream. The three primary modes defined in the RTMP specification are:
live: Broadcasts content in real-time without writing it to server storage (default).record: Writes the incoming stream to a file on the server, overwriting existing files with the same name.append: Appends the incoming stream data to an existing recorded file.
Method A: Using
FFmpeg’s Native -rtmp_live Option
FFmpeg’s built-in RTMP protocol handler allows you to specify whether
the stream should be treated as live or playable via the
-rtmp_live option:
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -rtmp_live live -f flv rtmp://live.example.com/app/streamKeyValues for -rtmp_live:
live: Default behavior; specifies real-time transmission.recorded: Informs the server that the source is pre-recorded.
Method B: Specifying Mode in the RTMP URL
Many media servers (such as NGINX-RTMP, Wowza, and Red5) allow or require the publishing mode to be declared as an inline parameter directly in the stream key path:
Record Mode:
ffmpeg -i input.mp4 -c copy -f flv "rtmp://live.example.com/app/streamKey?publish_type=record"(Some servers use
?mode=recordor append the mode directly to the stream name, such asstreamKey?record)Append Mode:
ffmpeg -i input.mp4 -c copy -f flv "rtmp://live.example.com/app/streamKey?publish_type=append"
Method C: Using
librtmp Syntax
If FFmpeg was compiled with librtmp support
(--enable-librtmp), connection parameters, including the
publishing mode, can be passed as space-delimited options in the URL
string:
ffmpeg -i input.mp4 -c copy -f flv "rtmp://live.example.com/app/streamKey live=1"For recording:
ffmpeg -i input.mp4 -c copy -f flv "rtmp://live.example.com/app/streamKey record=1"
Complete Command Example
To capture a video file, encode it to standard H.264/AAC, assign the
stream key live_user_123, and enforce live broadcast
mode:
ffmpeg -re -i source.mp4 \
-c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k \
-c:a aac -b:a 128k -ar 44100 \
-rtmp_live live \
-f flv "rtmp://live.example.com/live/live_user_123"