Set up the source stream using FFmpeg
Use FFmpeg, a command-line tool to configure your source stream.
For testing purposes, MK.IOprovides you with a sample ffmpeg
command line to ingest a source feed into the live event. For SRT it will require an ffmpeg
binary compiled with libsrt.
RTMP basic example
This FFmpeg command creates a constant bitrate. In this example, we will use three video qualities for output and ingest to Azure Media Services channel. For this configuration, you should use a basic or standard pass-through channel, as you do not need cloud transcoding when sending multiple bitrates to Azure.live stream at 1080p resolution with a test pattern video and a 150 Hz beeping tone, encoding video using H.264 and audio using AAC. It sends this stream to an RTMP server, with settings optimized for real-time streaming and stable bitrate management.
ffmpeg -threads 0 \
-re \
-f lavfi \
-i testsrc=rate=30:size=1920x1080,format=yuv420p \
-f lavfi \ -i "sine=frequency=150:sample_rate=48000:beep_factor=2" \
-c:a aac \
-ab 48k \
-ac 2 \
-ar 48000 \
-c:v libx264 \
-preset:v fast \
-b:v 500k \
-minrate 500k -maxrate 500k -bufsize 500k \
-r 30 \
-g 60 -keyint_min 60 \
-sc_threshold 0 \
-f flv \
rtmp://in-7334a7c7-a376-4edf-aeb9-d6bfd7fab995.japaneast.streaming.mediakind.com:1935/de04ea37-ace5-46e1-9a54-6cb045c424df/stream
Command Breakdown
Let’s break down each part of the command in detail:
Command | Description |
---|---|
-threads 0 | Sets the number of threads for processing. 0 lets ffmpeg decide the optimal number of threads based on the available CPU cores. |
-re | Makes ffmpeg read input in real-time rather than as fast as possible. This is useful when streaming to a live server so that it matches real-world playback speed. |
-f lavfi | Specifies the input format. lavfi stands for “libavfilter input”, meaning that the input will be generated using an FFmpeg filter rather than an external file or stream. |
-i testsrc=rate=30:size=1920x1080,format=yuv420p | Specifies the video source: testsrc generates a test pattern, often used for debugging or setup. rate=30 sets the frame rate to 30 frames per second. size=1920x1080 sets the resolution to 1080p. format=yuv420p sets the pixel format to YUV 4:2:0, a common format for video compression. |
-f lavfi -i "sine=frequency=150:sample_rate=48000:beep_factor=2" | Adds an audio source generated by a sine tone generator: frequency=150 generates a 150 Hz tone.sample_rate=48000 sets the audio sample rate to 48 kHz. beep_factor=2 causes the tone t ep” with breaks, rather than a continuous tone. |
-c:a aac | Sets the audio codec to AAC, a widely used audio compression format. |
-ab 48k | Sets the audio bitrate to 48 kbps. |
-ac 2 | Sets the number of audio channels to 2 (stereo). |
-ar 48000 | Sets the audio sample rate to 48 kHz. |
-c:v libx264 | Sets the video codec to H.264 using libx264, which is efficient and compatible with most streaming platforms. |
-preset:v fast | Specifies a faster encoding preset for libx264, balancing encoding speed and compression efficiency. |
-b:v 500k | Sets the target video bitrate to 500 kbps, which controls the overall data rate for the video. |
-minrate 500k -maxrate 500k -bufsize 500k | Configures constant bitrate (CBR) streaming by setting: minrate and maxrate to 500 kbps, forcing the video bitrate to be stable. bufsize to 500 kbps, controlling the buffer for the video rate control. |
-r 30 | Sets the output frame rate to 30 fps, matching the input video frame rate. |
-g 60 -keyint_min 60 | Controls the group of pictures (GOP) size: -g 60 sets a GOP length of 60 frames (every 2 seconds at 30 fps). keyint_min 60 ensures that keyframes (I-frames) occur every 60 frames, helping with consistent quality and synchronization. This value needs to match the Input key frame intervalset in MK.IO UI . |
-sc_threshold 0 | Sets the scene change threshold to 0, meaning it won’t add extra keyframes for scene changes. This helps keep the GOP structure stable. |
-f flv | Specifies the output format as flv, which is commonly used for streaming to RTMP servers. |
rtmp://... | Specifies the RTMP server URL where the stream will be sent. Paste the input URL given in the Live Event. |
RTMP example with multiple bitrates
This example ingests three video qualities to MK.IO live event using a basic or standard pass-through event. Unique stream names need to be used for each quality, and key frames need to be aligned across all video qualities.
ffmpeg.exe -threads 0 -re /
-stream_loop -1 -i "C:\Videos\sample.mp4" /
-c:a aac -ab 128k -ac 2 -ar 48000 -c:v libx264 -s svga -b:v 500k -minrate 500k -maxrate 500k -bufsize 500k -r 30 -g 48 -keyint_min 48 -sc_threshold 0 -f flv rtmp://in-7334a7c7-a376-4edf-aeb9-d6bfd7fab995.japaneast.streaming.mediakind.com:1935/de04ea37-ace5-46e1-9a54-6cb045c424df/stream_500 /
-c:a aac -ab 128k -ac 2 -ar 48000 -c:v libx264 -s vga -b:v 300k -minrate 300k -maxrate 300k -bufsize 300k -r 30 -g 48 -keyint_min 48 -sc_threshold 0 -f flv rtmp://in-7334a7c7-a376-4edf-aeb9-d6bfd7fab995.japaneast.streaming.mediakind.com:1935/de04ea37-ace5-46e1-9a54-6cb045c424df/stream_300 /
-c:a aac -ab 128k -ac 2 -ar 48000 -c:v libx264 -s qvga -b:v 150k -minrate 150k -maxrate 150k -bufsize 150k -r 30 -g 48 -keyint_min 48 -sc_threshold 0 -f flv rtmp://in-7334a7c7-a376-4edf-aeb9-d6bfd7fab995.japaneast.streaming.mediakind.com:1935/de04ea37-ace5-46e1-9a54-6cb045c424df/stream_150
When running FFmpeg, be sure to adjust the encoding settings based on available CPU capabilities and bandwidth on your network to achieve the desired quality setting.
Updated about 1 month ago