Stream Local Video to RTMP with FFmpeg on Linux
Streaming a local video file to an RTMP server on Linux is a highly efficient way to broadcast pre-recorded content to platforms like YouTube, Twitch, or a self-hosted server. By utilizing FFmpeg, a powerful command-line tool, you can transcode, scale, and push your media smoothly without the overhead of a graphical user interface. This guide will walk you through the precise command-line syntax required, explain how to optimize your encoding settings for stable streaming, and provide troubleshooting steps for common playback issues.
Prerequisite: Installing FFmpeg on Linux
Before streaming, ensure you have FFmpeg installed with the necessary
encoders (libx264 for video and aac for
audio). You can install it via your distribution’s package manager:
- Ubuntu/Debian:
sudo apt update && sudo apt install ffmpeg - Fedora:
sudo dnf install ffmpeg - Arch Linux:
sudo pacman -S ffmpeg
Verify your installation and available encoders by running
ffmpeg -encoders in your terminal.
The Basic RTMP Streaming Command
To stream a local MP4 file to an RTMP destination without re-encoding (which saves CPU power but requires the file to already be in a web-compatible format), use the following syntax:
ffmpeg -re -i input.mp4 -c copy -f flv rtmp://your-server-url/live/stream-key
Optimizing and Re-encoding for Live Streaming
If your source video is not optimized for streaming, or if you have a strict upload bandwidth limit, you should re-encode the video on the fly. The command below transcodes the video to H.264 and audio to AAC, applies a constant bitrate, and sets a strict keyframe interval, which is mandatory for most RTMP servers:
ffmpeg -re -i input.mp4 -c:v libx264 -pix_fmt yuv420p -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k -g 60 -c:a aac -b:a 128k -f flv rtmp://your-server-url/live/stream-key
Breaking Down the Command Arguments
Understanding each flag allows you to tweak the command to match your network speed and hardware capabilities:
-re: Reads the input file at its native frame rate. Without this, FFmpeg will stream the file as fast as your CPU can process it, causing the RTMP server to reject the stream.-i input.mp4: Specifies the path to your local video file.-c:v libx264: Uses the H.264 video encoder, which is universally accepted by RTMP ingest points.-pix_fmt yuv420p: Ensures maximum compatibility across web players by using the standard YUV 4:2:0 pixel format.-preset veryfast: Balances CPU usage and video quality. Faster presets lower CPU load, which is ideal for older Linux machines.-b:v 3000k -maxrate 3000k -bufsize 6000k: Enforces a strict 3000 Kbps video bitrate. The buffer size tells the encoder how often to check the bitrate conformity.-g 60: Sets the Group of Pictures (GOP) size, or keyframe interval. If your video is 30 frames per second, a value of 60 ensures a keyframe is sent every 2 seconds.-c:a aac -b:a 128k: Encodes the audio to AAC format at a standard 128 Kbps bitrate.-f flv: Forces the output format to FLV, which is the container format used by the RTMP protocol.