Smooth FFmpeg Live Loop with Different Resolutions

To stream a continuous live loop using FFmpeg with input files of varying resolutions, you must standardize the video and audio properties of all source files before looping them. If you try to concatenate videos with different resolutions, frame rates, or audio channel layouts on the fly, the RTMP/SRT stream will crash or suffer from severe buffering and synchronization issues. The most efficient and reliable method to achieve smooth transitions is to pre-process the files to matching specifications and then stream them using the concat demuxer.

Step 1: Standardize the Video Resolutions and Formats

Before starting your live stream, normalize all video files to a uniform resolution (such as 1080p), frame rate, and audio profile. You can scale and pad the videos so that they maintain their original aspect ratio without stretching.

Run the following command on each input file that does not match your target streaming format:

ffmpeg -i input.mp4 -vf "scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2" -r 30 -c:v libx264 -pix_fmt yuv420p -c:a aac -ar 44100 -ac 2 output_1080p.mp4

What this command does: * scale=1920:1080:force_original_aspect_ratio=decrease: Scales the video to fit within a 1920x1080 boundary while preserving the aspect ratio. * pad=1920:1080:(ow-iw)/2:(oh-ih)/2: Centers the scaled video and adds black bars (pillarboxes or letterboxes) to fill the remaining 1920x1080 area. * -r 30: Forces a constant frame rate of 30 frames per second. * -pix_fmt yuv420p: Sets a standard pixel format compatible with almost all media players and streaming ingest points. * -c:a aac -ar 44100 -ac 2: Standardizes the audio codec to AAC, sample rate to 44.1kHz, and channels to stereo.

Step 2: Create an Input List File

Once all your video files are standardized to the exact same resolution, frame rate, audio sample rate, and codec, create a text file named inputs.txt. List the paths to your standardized files in the order you want them to play:

file 'output1_1080p.mp4'
file 'output2_1080p.mp4'
file 'output3_1080p.mp4'

Step 3: Stream the Loop Seamlessly

Use the FFmpeg concat demuxer to read the text file, loop the inputs infinitely, and stream the output to your destination (such as YouTube, Twitch, or an RTMP server).

ffmpeg -re -f concat -safe 0 -stream_loop -1 -i inputs.txt -c copy -f flv rtmp://your-live-server-url/live/stream-key

Key parameters used here: * -re: Reads the input files at their native frame rate (real-time). This is required for live streaming. * -f concat: Uses the concat demuxer to join the files dynamically. * -safe 0: Allows the demuxer to accept relative and absolute file paths in inputs.txt. * -stream_loop -1: Loops the input list infinitely. * -c copy: Copies the video and audio streams without re-encoding, minimizing CPU usage and ensuring a seamless transition between files.