Stream MP4 Playlist Continuously with FFmpeg Concat
This article provides a direct, step-by-step guide on how to continuously stream a playlist of static MP4 video files to a live-streaming destination using FFmpeg and the concat demuxer. You will learn how to format your playlist file, configure the FFmpeg command to loop indefinitely, and stream the output to an RTMP server like YouTube, Twitch, or Facebook Live.
Step 1: Create the Playlist File
The FFmpeg concat demuxer requires a text file containing the paths
to your MP4 files. Create a text file named playlist.txt in
the same directory as your videos, and list them in the following
format:
file 'video1.mp4'
file 'video2.mp4'
file 'video3.mp4'
Note: For the concat demuxer to work seamlessly without rendering errors, all MP4 files should have the same resolution, frame rate, video codec, and audio codec.
Step 2: The FFmpeg Command
To stream this playlist continuously (in an infinite loop) to an RTMP destination, run the following command in your terminal:
ffmpeg -re -stream_loop -1 -f concat -safe 0 -i playlist.txt \
-c:v libx264 -preset veryfast -b:v 3000k -maxrate 3000k -bufsize 6000k \
-pix_fmt yuv420p -g 60 -c:a aac -b:a 128k -ar 44100 \
-f flv rtmp://your.rtmp.server/live/stream_keyParameter Breakdown
-re: Tells FFmpeg to read the input at the native frame rate. This is required for real-time live streaming.-stream_loop -1: Instructs FFmpeg to loop the input playlist infinitely.-f concat: Specifies the use of the concat demuxer.-safe 0: Allows the use of absolute or relative paths in theplaylist.txtfile.-i playlist.txt: Defines the input text file.-c:v libx264: Re-encodes the video to H.264, which is the standard format required by most live-streaming platforms.-preset veryfast: Balance between compression efficiency and CPU usage.-b:v 3000k -maxrate 3000k -bufsize 6000k: Controls the video bitrate for a stable stream. Adjust3000kto match your internet upload bandwidth.-pix_fmt yuv420p: Ensures compatibility with various players and streaming platforms.-g 60: Sets the keyframe interval (GOP size). Set this to double your video’s frame rate (e.g., 60 for a 30 FPS stream) to ensure a keyframe every 2 seconds.-c:a aac -b:a 128k -ar 44100: Transcodes the audio to AAC at 128kbps with a sample rate of 44.1kHz.-f flv: Formats the output stream as FLV, the standard format for RTMP.rtmp://...: Your target RTMP ingest URL and secret stream key provided by your streaming platform.