FFmpeg Command to Loop a Video Playlist
To loop a playlist of videos using FFmpeg, you need to combine the concat demuxer with the stream loop option. This article provides a quick, step-by-step guide on how to format your playlist text file and write the exact FFmpeg commands required to loop your videos either a specific number of times or infinitely for live streaming.
Step 1: Create the Playlist File
FFmpeg requires a text file containing the paths to the videos you
want to play. Create a text file named playlist.txt and
list your video files in the following format:
file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
Ensure that all video files in the playlist have the same resolution, frame rate, and codecs to prevent playback issues.
Step 2: Run the FFmpeg Loop Command
Depending on your goal, choose one of the two commands below.
Option A: Loop the Playlist to Create a New File
If you want to loop the playlist a set number of times and save the
result as a single, longer video file, use the -stream_loop
flag before the input.
ffmpeg -stream_loop 3 -f concat -safe 0 -i playlist.txt -c copy output.mp4-stream_loop 3: Loops the input 3 times (playing the playlist a total of 4 times). Set this to the number of loops you want.-f concat: Tells FFmpeg to concatenate the files listed in the text file.-safe 0: Allows the use of relative or absolute file paths in the playlist.-c copy: Copies the video and audio streams without re-encoding, making the process incredibly fast.
Option B: Loop the Playlist Infinitely for Live Streaming
If you want to loop the playlist forever (for example, to stream to
YouTube, Twitch, or a local server), set the stream loop value to
-1 and enable native frame rate reading.
ffmpeg -re -stream_loop -1 -f concat -safe 0 -i playlist.txt -c:v libx264 -preset veryfast -c:a aac -f flv rtmp://your-rtmp-endpoint-re: Reads the input at the native frame rate. This is required for real-time live streaming.-stream_loop -1: Loops the input stream infinitely.-c:v libx264 -c:a aac: Re-encodes the stream to H.264 video and AAC audio, which ensures compatibility with most streaming platforms.