How to Loop Video to RTMP with FFmpeg

This article explains how to use FFmpeg to stream a single video file in an infinite loop to an RTMP destination, such as YouTube, Twitch, or a custom media server. You will learn the exact FFmpeg command required for this setup, the purpose of each command parameter, and best practices for maintaining a stable, continuous live stream.

The Command

To stream a video file in an infinite loop, run the following FFmpeg command in your terminal. Replace input.mp4 with your source file path and the RTMP URL at the end with your actual ingest server and stream key:

ffmpeg -re -stream_loop -1 -i input.mp4 -c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k -pix_fmt yuv420p -g 60 -c:a aac -b:a 128k -f flv rtmp://your-rtmp-server/live/stream_key

Parameter Breakdown

Understanding how each flag works is critical to ensuring your stream does not drop or lag:

While copying the codecs directly using -c:v copy -c:a copy uses significantly less CPU, it often causes timestamp desynchronization at the loop boundary. When the video restarts, the timecodes reset, which can cause RTMP servers to reject the stream or freeze. Re-encoding the video using libx264 and aac standardizes the timestamps and guarantees a seamless loop transition.