Loop Video Playlist with FFmpeg Concat Demuxer
This article explains how to use the FFmpeg concat
demuxer to create and continuously loop a playlist of video files. You
will learn how to format your playlist text file, apply the loop option,
and configure the FFmpeg command for either saving the looped output to
a file or broadcasting it as a stream.
Step 1: Create the Playlist Text File
The concat demuxer requires a plain text file containing
the list of videos you want to play. Create a file named
playlist.txt and list your video files in the order you
want them to play. Use the following format:
file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
Note: If your video files are in a different directory, use absolute paths or relative paths. Ensure all listed videos have the same resolution, frame rate, and codecs to prevent playback issues.
Step 2: Use the FFmpeg Command to Loop the Playlist
To loop the playlist, you must place the -loop 1 option
before the input flag (-i). This tells the
demuxer to start reading from the beginning of the text file once it
reaches the end.
Option A: Loop and Save to a Finite File
When rendering to a local file, an infinite loop will result in an
infinitely growing file. You must specify a duration limit using the
-t parameter (in seconds).
ffmpeg -f concat -safe 0 -loop 1 -i playlist.txt -t 600 -c:v libx264 -c:a aac output.mp4-f concat: Specifies the concat demuxer.-safe 0: Allows the use of relative or absolute file paths.-loop 1: Enables infinite looping of the input list.-i playlist.txt: Defines the input playlist file.-t 600: Stops the recording after 10 minutes (600 seconds).-c:v libx264 -c:a aac: Re-encodes the video and audio to ensure seamless transitions during the loops.
Option B: Stream the Loop Infinitely (RTMP/SRT)
If you are streaming to a platform like YouTube, Twitch, or a custom
RTMP server, you can loop the playlist indefinitely without setting a
time limit. You should also add the -re flag to read the
input at native frame rates.
ffmpeg -re -f concat -safe 0 -loop 1 -i playlist.txt -c:v libx264 -preset veryfast -c:a aac -f flv rtmp://your-server-url/stream-key-re: Reads the input at the native frame rate (required for real-time streaming).-f flv: Sets the output format suitable for streaming.
Why Re-encoding is Recommended
While stream copying (-c copy) is faster and uses less
CPU, it often causes sync issues, frozen frames, or crashes at the
transition points when looping. Re-encoding the video
(-c:v libx264) forces FFmpeg to generate consistent
timestamps across all loops, ensuring a seamless and stutter-free
playback experience.