Loop a video with FFmpeg

Put -stream_loop before -i: -stream_loop 3 plays the file four times, -stream_loop -1 forever until -t or -shortest stops it. Add -c copy and it takes no time at all. For passes that land on the exact frame, loop the decoded video with the loop and aloop filters instead.

Fast Stream copy

Copies the clip as many times as you ask. Each pass comes out 21 ms long.

ffmpeg -stream_loop 3 -i clip.mp4 -c copy out.mp4

Four plays in one file, 384 frames, copied. 3 means three more passes after the first. Each pass runs 4.021 s, not 4.000.

Plays
4, from -stream_loop 3
Pass starts
0, 4.021, 8.043, 12.064 s
Run time
88 ms
Output
6.5 MB
Exact Re-encode

Every pass starts on the exact frame. Re-encodes, so it takes longer.

ffmpeg -i clip.mp4 -filter_complex "[0:v]loop=loop=3:size=96[v];[0:a]aloop=loop=3:size=192000[a]" -map "[v]" -map "[a]" -c:v libx264 -crf 20 -c:a aac out.mp4

Four plays, video exactly 16.000 s. The passes start at 4.000, 8.000 and 12.000.

Plays
4, from loop=3
Pass starts
0, 4, 8, 12 s
Run time
1.4 s
Output
6.3 MB

Proof

The first frame of every pass

The clip has its source timecode burned in. Each picture is the first frame of a pass, labelled with its time in the output.
-stream_loop 3 -c copy Each pass starts 21 ms late
Four frames from the stream-copied loop, all showing source timecode 1:16.000, frame 1824, at output times 0, 4.021, 8.043 and 12.064
16.06 s Every pass opens on frame 1824. The fourth starts at 12.064 s instead of 12.000, because each pass carries one extra audio frame.
loop=loop=3:size=96 Each pass starts on time
Four frames from the loop filter output, all showing source frame 1824, at output times 0, 4, 8 and 12 seconds
16.00 s The same frame 1824 at exactly 4, 8 and 12 s. The filter replays the 96 decoded frames with no gap.
reverse + concat The turn frame shows twice
Boomerang output: frame 1824 at 0 s, frame 1919 at 3.958 s and again at 4.000 s, frame 1824 at 7.958 s
8.00 s Forward to frame 1919, then back to 1824. Frame 1919 closes the forward half and opens the reversed one. The fix is in the questions below.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Timecode added by us.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"clip.mp4": "https://cdn.rendobar.com/examples/loop-video/clip.mp4",
},
params: {
command: "ffmpeg -stream_loop 3 -i clip.mp4 -c copy out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"clip.mp4": "https://cdn.rendobar.com/examples/loop-video/clip.mp4"
},
"params": {
"command": "ffmpeg -stream_loop 3 -i clip.mp4 -c copy out.mp4"
}
}'

Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.

Every variant

Pick the command for your case

Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.

  1. Loop a video to a set length

    Stream copy
    ffmpeg -stream_loop -1 -i clip.mp4 -t 30 -c copy out.mp4

    30.11 s: seven and a half passes. -1 loops forever and -t 30 stops it. A copy ends on a whole packet, so it can run a little past 30.

    86 ms run 12.1 MB 30.11 s long

  2. Loop a video until the audio ends

    Stream copy
    ffmpeg -stream_loop -1 -i clip.mp4 -i voice.m4a -map 0:v -map 1:a -c copy -shortest out.mp4

    The 4 s picture repeats under 30 s of dialogue. Video 29.96 s, audio 30.00 s. -shortest ends it with the voice.

    89 ms run 12.1 MB 30.00 s long

  3. Loop audio to the length of a video

    Stream copy
    ffmpeg -i in.mp4 -stream_loop -1 -i bed.m4a -map 0:v -map 1:a -c copy -shortest out.mp4

    The 6 s music bed plays five times under 30 s of picture. Video 30.00 s, audio 29.99 s.

    78 ms run 5.9 MB 30.00 s long

  4. Make a boomerang that plays forward then back

    Re-encode
    ffmpeg -i clip.mp4 -filter_complex "[0:v]reverse[r];[0:v][r]concat=n=2:v=1:a=0[v]" -map "[v]" -c:v libx264 -crf 20 out.mp4

    8.00 s, 192 frames: forward to frame 1919, then back to 1824. No sound. reverse holds the whole clip in memory, so keep it short.

    789 ms run 3.1 MB 8.00 s long

  5. Hold the last frame for 3 more seconds

    Re-encode
    ffmpeg -i clip.mp4 -vf tpad=stop_mode=clone:stop_duration=3 -af apad=pad_dur=3 -c:v libx264 -crf 20 -c:a aac out.mp4

    7.00 s of picture: the clip, then its last frame for 3 s. apad adds 3 s of silence so the sound lasts as long.

    552 ms run 1.6 MB 7.01 s long

Gotchas

Where it goes wrong

Does -stream_loop 3 play the video 3 times or 4?

Four. The number counts the extra passes after the first one. On the test clip -stream_loop 3 gave 384 frames, four times the clip's 96.

-stream_loop -1 loops forever, so always pair it with something that ends the output: -t for a length, or -shortest and a second input that has an end. It is an input option, so it goes before the -i it applies to.

Why is my looped file longer than N times the clip?

The clip is 4.000 s, four passes should be 16.000 s, and the copied loop came out 16.064 s. Each pass started 21 ms after the last one ended: 4.021, 8.043, 12.064.

21 ms is 1024 samples at 48 kHz, one AAC frame. AAC files start with that much encoder priming, and -stream_loop counts it on every pass. Re-encoding after -stream_loop does not remove it:

ffmpeg -stream_loop 3 -i clip.mp4 -c:v libx264 -crf 20 -c:a aac out.mp4

16.083 s of video and 16.106 s of audio. The gaps were already in the timestamps before the encoder saw them.

For an exact loop, replay decoded frames with loop and aloop, as in the Exact answer. Its passes start at 4.000, 8.000 and 12.000.

Why does -vf loop=3 do nothing?

The loop filter only replays the frames it holds, and size, the number it holds, defaults to 0:

ffmpeg -i clip.mp4 -vf loop=3 -c:v libx264 -crf 20 -c:a aac out.mp4

4.00 s, 96 frames. The same length as the clip, no loop.

  • loop needs size in frames. The clip is 96 frames, 4 s at 24 fps, so size=96. The filter keeps them in memory, up to 32767 frames.
  • aloop needs size in samples. 4 s at 48 kHz is size=192000.
  • Both count extra passes like -stream_loop: loop=3 plays four times.

Why does my boomerang stutter where it turns around?

The last frame of the forward half is also the first frame of the reversed half. On the test clip frame 1919 showed at 3.958 s and again at 4.000 s, so the picture holds for two frames. Drop the first frame of the reversed copy:

ffmpeg -i clip.mp4 -filter_complex "[0:v]reverse,trim=start_frame=1,setpts=PTS-STARTPTS[r];[0:v][r]concat=n=2:v=1:a=0[v]" -map "[v]" -c:v libx264 -crf 20 out.mp4

7.96 s, 191 frames. The turn now reads frame 1918, 1919, 1918, 1917.

Related: Merge videos, Add audio to a video, Change video speed, MP4 to GIF.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.