How to Create an Infinite Audio Loop with FFmpeg

This article provides a quick guide on how to use FFmpeg to create an infinite audio loop for testing environments, live streaming, or audio development. You will learn the specific command-line parameters required to loop an audio file indefinitely and how to apply them to different testing scenarios.

To loop an audio file infinitely in FFmpeg, you must use the -stream_loop option. This option must be placed before the input file (-i) in your command line. Setting the value of -stream_loop to -1 tells FFmpeg to loop the input source infinitely.

The Basic Infinite Loop Command

If you want to stream or process an audio file in an infinite loop, use the following syntax:

ffmpeg -re -stream_loop -1 -i input.mp3 -c:a copy -f null -

Command Breakdown: * -re: Read the input at the native frame rate. This is crucial for real-time testing and streaming simulation, as it prevents FFmpeg from processing the file as fast as possible. * -stream_loop -1: Loops the input file infinitely. * -i input.mp3: Specifies your input audio file. * -c:a copy: Copies the audio stream without re-encoding, saving CPU resources during testing. * -f null -: Sends the output to a null sink, which is ideal for testing system performance or stream stability without writing to disk.


Scenario 1: Outputting to a Local Network Stream (UDP)

If you are testing network audio receivers, you can stream the infinite audio loop over UDP to a specific IP address and port:

ffmpeg -re -stream_loop -1 -i input.wav -acodec libmp3lame -f mpegts udp://127.0.0.1:12345

Scenario 2: Creating a Looped File of a Specific Duration

If you do not want a literal infinite stream, but instead need a single audio file of a fixed duration (e.g., a 1-hour background track created from a 30-second clip), combine -stream_loop -1 with the -t (duration) parameter:

ffmpeg -stream_loop -1 -i short_clip.wav -t 3600 -c:a copy long_loop.wav

In this command, -t 3600 limits the output file to exactly 3,600 seconds (1 hour), at which point FFmpeg will automatically stop looping and close the file.