Skip to main content
11 min readLast updated: November 2026

Run a 24/7 YouTube loop with FFmpeg on a VPS, and what breaks

One FFmpeg command keeps a video looping on a YouTube live stream. Keeping it there for weeks takes a few more flags, a service manager and a plan for the moments it stops. Every command on this page was run before it was printed.

Encode the file once, to YouTube's settingsThe whole recipe: encode the file once to YouTube's settings, stream it with -re -stream_loop -1 and the video copied, and let systemd restart FFmpeg every time it exits.

To loop a video to YouTube Live from a server, put -re -stream_loop -1 before the input, copy the video stream, and send FLV to rtmp://a.rtmp.youtube.com/live2/ followed by your stream key. Encode the file to YouTube's settings once beforehand, so the server only copies video while it streams, and run FFmpeg under systemd with Restart=always, because sooner or later it will exit.

The short version of that command is in our guide to looping a video on a live stream. This page is the long version: the flags we run on our own streaming servers and the failure each one is there for, a systemd unit, what size of server it needs, and what will go wrong anyway. Commands were tested on 12 November 2026 with FFmpeg 8.1 and 7.1.5, against a local RTMP receiver standing in for YouTube.

How to run a 24/7 loop with FFmpeg on a server

Five steps, the last one optional. Steps 1 and 2 work on any computer; steps 3 and 4 assume a Linux server with systemd, which is what Debian and Ubuntu VPS images run.

  1. Step 1

    Encode the file once, to YouTube's settings

    Streaming with the video copied, not re-encoded, is what lets a small server run a loop for months. That only works if the file is already in a shape YouTube accepts.

    • H.264 video in yuv420p, a keyframe every 2 seconds, and a bitrate inside the range YouTube's encoder settings page gives for your resolution: 5 to 14 Mbps for 1080p at 30 fps.
    • -g 60 -keyint_min 60 -sc_threshold 0 gives exactly one keyframe every 60 frames, which is 2 seconds at 30 fps. At 60 fps use 120.
    • -bf 0 turns B-frames off. YouTube recommends two, and accepts them; we turn them off because a file without them has fewer timestamp edge cases each time the loop starts again.
    • -q:a 2 asks FFmpeg's AAC encoder for a quality, not a bitrate. Why that matters is in the errors section below.
    • -shortest cuts the audio and video to the same length. A track that runs longer than the other leaves a frozen picture or a silent gap at every pass of the loop.
    • Check the result before uploading it: ffprobe -v error -select_streams v -skip_frame nokey -show_entries frame=pts_time -of csv=p=0 video.mp4 prints the time of every keyframe, and it should read 0, 2, 4, 6 and so on.
    ffmpeg -i source.mp4 \
      -c:v libx264 -preset veryfast -b:v 6000k -maxrate 6000k -bufsize 12000k \
      -g 60 -keyint_min 60 -sc_threshold 0 -bf 0 -pix_fmt yuv420p \
      -c:a aac -q:a 2 -ar 48000 -ac 2 -shortest -movflags +faststart \
      video.mp4

    The whole recipe: encode the file once to YouTube's settings, stream it with -re -stream_loop -1 and the video copied, and let systemd restart FFmpeg every time it exits.

  2. Step 2

    Put FFmpeg and the file on the server, and test by hand

    Install FFmpeg from the distribution (apt install ffmpeg on Debian or Ubuntu), copy the file to /srv/loop/video.mp4, and run the command from the next section once in a terminal.

    • Copy the stream key from YouTube Studio → Create → Go live → Stream tab. Our stream key guide shows where it is.
    • Start FFmpeg, then open the Live Control Room. YouTube shows the preview and the stream health within a few seconds of the first packets arriving.
    • Leave it running past the end of the file at least once. The moment the loop starts again is where a badly prepared file shows its problems.

    Stop FFmpeg with Ctrl+C, not by closing the terminal: a terminal that closes takes FFmpeg with it, which is exactly why the next step exists.

  3. Step 3

    Make it a systemd service that restarts itself

    FFmpeg has no reconnect of its own for an RTMP output. When the connection drops, it exits, and the stream stays down until something starts it again. systemd is that something.

    • Save the unit as /etc/systemd/system/youtube-loop.service.
    • Put the key in /etc/youtube-loop.env as one line, STREAM_KEY=your-key, and make it readable by root only: chmod 600 /etc/youtube-loop.env. systemd reads the file before it starts FFmpeg.
    • Restart=always restarts FFmpeg whatever the exit was, and RestartSec=10 waits ten seconds first, so a YouTube ingest that is still closing the old session is not hammered with reconnects.
    • DynamicUser=yes runs FFmpeg as a throwaway user with no rights on the rest of the system. The video must then be readable by everyone (chmod 644).
    • Run systemctl daemon-reload, then systemctl enable --now youtube-loop. enable makes it start again after a reboot.
    [Unit]
    Description=24/7 video loop to YouTube Live
    Wants=network-online.target
    After=network-online.target
    
    [Service]
    EnvironmentFile=/etc/youtube-loop.env
    ExecStart=/usr/bin/ffmpeg -hide_banner -loglevel warning \
      -re -stream_loop -1 -fflags +genpts+discardcorrupt+igndts -i /srv/loop/video.mp4 \
      -map 0:v:0 -map 0:a:0? \
      -c:v copy -c:a aac -q:a 2 -ar 48000 -ac 2 -af aresample=async=1000 \
      -max_interleave_delta 0 -avoid_negative_ts make_non_negative \
      -flvflags +no_duration_filesize \
      -f flv rtmp://a.rtmp.youtube.com/live2/${STREAM_KEY}
    Restart=always
    RestartSec=10
    DynamicUser=yes
    
    [Install]
    WantedBy=multi-user.target

    The key never appears in the unit file, but it does appear in FFmpeg's command line while it runs, so anyone who can list processes on the server can read it. On a server only you log in to, that is acceptable; on a shared one it is not.

  4. Step 4

    Watch it, because nothing else will

    A service that restarts itself also fails quietly. Two commands tell you what it has been doing.

    • journalctl -u youtube-loop --since today shows FFmpeg's warnings and every exit, with times.
    • systemctl show youtube-loop -p NRestarts counts the restarts since the service was started. One or two a day is ordinary; one a minute means the same fault every time, and a restart is not fixing it.
    • YouTube's Live Control Room shows stream health from YouTube's side. It is the only view that tells you whether viewers actually see the picture.
    • Nothing here alerts you. If you want to hear about an outage at night, a check that looks at the restart count, or at the channel, has to be built as well.

    -loglevel warning is deliberate. With errors only, an exit is logged with no reason; the warnings are what say whether YouTube closed the connection or FFmpeg refused a packet.

  5. Step 5

    Optional: send the same loop to more platforms with tee

    FFmpeg's tee output sends one encode to several addresses. The audio is encoded once, the video is copied, and each extra destination costs little more processor time.

    • onfail=ignore keeps the other destinations going when one refuses. FFmpeg's documented default is abort, which ends every destination with it.
    • use_fifo=1 with drop_pkts_on_overflow=1 gives each destination its own queue that drops packets when it fills, instead of blocking. Without it, one platform that stops reading can freeze all of them, with no error at all.
    • The backslash before the colon inside fifo_options is doubled on purpose: tee unescapes the options once and the nested fifo_options once more. With a single backslash, the FLV muxer is handed drop_pkts_on_overflow, rejects it, and every output goes down.
    • -tag:v 7 is FLV's code for H.264. Without it the tee path refuses to start: Tag avc1 incompatible with output codec id '27'.
    • A destination that tee drops stays dropped until FFmpeg restarts. When Twitch ends its broadcast at 48 hours, YouTube carries on, and Twitch is back only after the next restart.
    ffmpeg -hide_banner -loglevel warning \
      -re -stream_loop -1 -fflags +genpts+discardcorrupt+igndts -i video.mp4 \
      -map 0:v:0 -map '0:a:0?' \
      -c:v copy -tag:v 7 -c:a aac -q:a 2 -ar 48000 -ac 2 -af aresample=async=1000 \
      -max_interleave_delta 0 \
      -f tee '[f=flv:flvflags=+no_duration_filesize:onfail=ignore:use_fifo=1:fifo_options=queue_size=600\\:drop_pkts_on_overflow=1]rtmp://a.rtmp.youtube.com/live2/YOUR-YOUTUBE-KEY|[f=flv:flvflags=+no_duration_filesize:onfail=ignore:use_fifo=1:fifo_options=queue_size=600\\:drop_pkts_on_overflow=1]rtmp://ingest.global-contribute.live-video.net/app/YOUR-TWITCH-KEY'

    The tee string is easy to get wrong by hand. Our free FFmpeg tee builder writes it for any list of RTMP and RTMPS addresses, with these options already in.

The command, flag by flag

This is the command the service above runs, written for a terminal. Each flag below is there because we watched a stream fail without it on our own servers; the reason is next to it.

ffmpeg -hide_banner -loglevel warning \
  -re -stream_loop -1 -fflags +genpts+discardcorrupt+igndts -i video.mp4 \
  -map 0:v:0 -map '0:a:0?' \
  -c:v copy -c:a aac -q:a 2 -ar 48000 -ac 2 -af aresample=async=1000 \
  -max_interleave_delta 0 -avoid_negative_ts make_non_negative \
  -flvflags +no_duration_filesize \
  -f flv "rtmp://a.rtmp.youtube.com/live2/YOUR-STREAM-KEY"
  • -re reads the file at its real playback speed. Without it FFmpeg sends the video as fast as the disk allows, and YouTube receives an hour of video in a minute.
  • -stream_loop -1 starts the file again each time it ends, forever. It is an input option: written after -i, it does nothing.
  • -fflags +genpts+discardcorrupt+igndts regenerates timestamps, drops damaged packets and ignores decode timestamps on the input, so the jump back to zero at every pass does not stop the output. These are input flags too, and must come before -i.
  • -map 0:v:0 -map '0:a:0?' takes the first video and, if there is one, the first audio track. The question mark lets a file with no sound stream as a picture only; it is quoted so the shell does not read it as a file pattern.
  • -c:v copy sends the video exactly as it is stored. It is what keeps the processor almost idle, and why step 1 matters.
  • -c:a aac -q:a 2 -ar 48000 -ac 2 -af aresample=async=1000 re-encodes the audio, which is cheap, and lets the resampler absorb the small timing step at each loop point. YouTube's table lists 44.1 kHz for stereo; 48 kHz is what our own streams send, and YouTube accepts it.
  • -max_interleave_delta 0 stops FFmpeg holding packets back to interleave them. With a waiting window, the FLV muxer eventually meets an audio packet older than one it already wrote, refuses it, and FFmpeg exits two seconds into every attempt.
  • -avoid_negative_ts make_non_negative moves the first packets, which start a few milliseconds below zero, to zero. FLV stores timestamps as signed 32-bit numbers, so a negative one goes out as a time about 24.8 days in the future.
  • -flvflags +no_duration_filesize leaves out the duration and file size fields, which mean nothing on a live stream, and keeps the width and height a receiver reads to learn the stream's shape. Without it, FFmpeg also ends every session complaining that it could not rewrite the header (Failed to update header with correct duration), because a live connection cannot be rewound.
  • -f flv is the container RTMP carries. YouTube's primary ingest is rtmp://a.rtmp.youtube.com/live2; the RTMP guide covers RTMPS and the backup address.

Copying the audio as well (-c copy for everything) also works with a well-prepared file, and saves a little more processor time. We re-encode the audio on every stream we run because a file whose audio track is a fraction of a second longer or shorter than its video then drifts out of sync, pass after pass.

YouTube's numbers, and what the server needs

YouTube publishes its H.264 bitrate ranges on Choose live encoder settings, bitrates, and resolutions, read on 12 November 2026. Encode the file inside the range for its resolution:

ResolutionMinimum (H.264)Recommended (H.264)
2160p, 60 fps14 Mbps50 Mbps
2160p, 30 fps11 Mbps42 Mbps
1440p, 60 fps8 Mbps34 Mbps
1440p, 30 fps7 Mbps21 Mbps
1080p, 60 fps6 Mbps17 Mbps
1080p, 30 fps5 Mbps14 Mbps
720p, 60 or 30 fps3 Mbps8 Mbps
  • Keyframe every 2 seconds, never more than 4. CBR, AAC or MP3 audio, 128 Kbps for stereo, progressive scan, square pixels, frame rate up to 60 fps, all from the same YouTube page.
  • Copying the video is almost free. On an Apple M4 Pro, streaming a 1080p30 file at 6 Mbps with the video copied and the audio re-encoded, as in the command above, used about 2–3% of one core. Encoding the same video live with x264 veryfast used about 40%, some fifteen times as much. A VPS core is slower than a desktop one, but the gap between the two is the point.
  • On our own streaming servers, the same arrangement (video copied, audio re-encoded) measured about 10% of a core per stream, and about 2 points more for each extra destination through tee.
  • Bandwidth is the real cost. A 6 Mbps stream sends about 1.9 TB a month to each destination, around the clock. Check the traffic allowance of the plan before the price: many VPS plans include a monthly transfer quota and charge for what goes over it.
  • Memory and disk are small. FFmpeg copying one stream needs little memory; the disk has to hold the file, plus the logs journald keeps.

If the bitrate calculator says a home connection cannot hold the rate you want, that is often the reason to move the loop to a server in the first place. For a loop that runs for weeks, our 24/7 bitrate settings guide explains why a steady rate below the maximum is usually the better choice.

What YouTube's rules mean for a loop from a server

YouTube does not care where the encoder runs. It does care what the stream contains, and a stream that never ends behaves differently from one that does.

  • A stream over 12 hours may not be archived at all. YouTube's Archive live streams page says it can archive a stream automatically only if it is shorter than 12 hours, and that a longer one may not be captured. A 24/7 loop leaves no replay; keep the source file.
  • Looping and repetition are a policy question, not a technical one. What YouTube, Twitch, Kick, Facebook, TikTok and Rumble each say about recorded and looped video is in our guide to pre-recorded live streams.
  • Music and footage you do not own get claimed on a live stream too. Content ID checks live streams, and a claim on a loop repeats with every pass. The music licensing guide covers what to use instead.
  • The stream key is a password. Anyone who has it can broadcast on your channel. Keep it in a root-only file, never in a script in a public repository, and reset it in YouTube Studio if it leaks.

None of this changes because the stream comes from FFmpeg rather than from OBS on a desktop: YouTube sees an RTMP connection with a key, and nothing about the software behind it.

What breaks, and what FFmpeg says when it does

These are the messages FFmpeg prints for the failures a 24/7 loop meets. The first two are faults in the command itself, and restarting will not cure them; the last two are what restarting is for.

  • Packets are not in the proper order with respect to DTS

    The FLV muxer met a packet older than one it had already written, refused it, and FFmpeg stopped with Invalid argument. It happens two seconds into every attempt, so the service restarts into the same failure forever.

    • Add -max_interleave_delta 0. We measured every non-zero window failing on the file that broke one of our streams, and zero muxing it cleanly.
    • Keep +igndts in the input -fflags: the jump back to the start of the file at each loop is a decode-timestamp discontinuity.
    • Reproduce it with the same command writing to -f flv /dev/null. If it fails there too, YouTube was never involved.
  • Assertion diff >= 0 && diff <= 120 failed at libavcodec/aacenc.c

    FFmpeg's built-in AAC encoder aborts after a long run when it is given a bitrate target with -b:a. On one of our streams it happened about every 41 minutes of audio, repeatably, and the log said only that FFmpeg had exited.

    • Use -q:a 2 instead of -b:a. It sets the encoder's quality directly instead of steering towards a bitrate, and the value that drifts no longer accumulates. The same stream then ran for over 65 hours without an abort.
    • Never give both -b:a and -q:a: which one wins is not obvious.
    • Copying the audio (-c:a copy) avoids the encoder altogether, if the file's audio is already AAC and exactly as long as the video.
  • Error opening output file rtmp://a.rtmp.youtube.com/live2/...

    The connection or the key was refused before a single frame was sent. The line above it says why: Connection refused, a timeout, or Input/output error when the ingest accepted the connection and then rejected the stream.

    • Check the key: a key that was reset in YouTube Studio, or copied with a missing character, is refused every time.
    • Right after a drop, YouTube can still be holding the previous session on that key. RestartSec=10 gives it time; a refusal that lasts for many minutes is a key problem, not a held session.
    • Check that the server can reach the ingest at all: an outbound firewall that blocks port 1935 gives a timeout here.
  • Error submitting a packet to the muxer: Broken pipe

    The connection closed while the stream was running. FFmpeg prints Broken pipe or Connection reset by peer and exits. YouTube resets ingest connections from time to time, and so does any network in between; on our own streams there is no fixed hour at which it happens.

    • This is the failure Restart=always exists for. The journal shows the exit and the restart ten seconds later.
    • Many of these an hour, on one server only, point at the server or its provider rather than at YouTube.
    • Each restart reconnects every destination of a tee output, including the ones that were healthy.

When doing it yourself stops being worth it

A loop on a VPS costs the server, the traffic and your time. The server can be small, because the video is only copied. The traffic is fixed by the bitrate and the number of platforms. The time is the part that is hard to estimate: preparing each file, reading the journal when the restart count climbs, and noticing at all when the stream is down, since nothing on this page tells you.

Most loops stay that simple, and FFmpeg is the right tool for them. The work grows with everything around the command: a playlist instead of one file, several platforms with different session limits, a schedule, a stream that has to come back by itself on a key that is still busy.

That is the part TheLoops does. You upload the files, add each platform's address and key, and the stream runs on our servers with the flags described here, the restarts handled, and each dropped destination brought back. If the loop on your own server works, there is no reason to move it.

Keep the loop running without the server

Upload the video once, paste each platform's address and key, and let the stream run from our data center.

  • Video copied, not re-encoded, as on this page
  • Automatic restart when a platform drops the connection
  • Up to 10 destinations from one broadcast
Start Free Trial

FFmpeg loop questions

Put -re -stream_loop -1 before -i, copy the video with -c:v copy, and send FLV to rtmp://a.rtmp.youtube.com/live2/ followed by your stream key. Encode the file to YouTube's settings first, with a keyframe every 2 seconds, so that copying works.

It is an input option, so it must come before the -i it applies to. Written after the file name, FFmpeg applies it to nothing and plays the file once.

Not for an RTMP output. It exits, and the stream stays down until something starts it again. On a server, run it as a systemd service with Restart=always, or in a loop in a shell script.

With the video copied rather than re-encoded, very little processor: about a tenth of a core per stream on our own servers. Bandwidth matters more: a 6 Mbps stream sends about 1.9 TB a month, so check the plan's traffic allowance. Encoding live instead needs one or more full cores.

Yes, with the concat demuxer: list the files in a text file, one file 'name.mp4' line each, and read it with -f concat -safe 0 -i list.txt, keeping -stream_loop -1 before it. The files should share resolution, frame rate and codecs, or copying will break at the joins.

Probably not. YouTube says it archives a stream automatically only if it is shorter than 12 hours, and that a longer one may not be captured at all.

Yes, with the tee output and one bracketed address per platform. Set onfail=ignore and a dropping queue for each, or one platform failing can stop the others. Our FFmpeg tee builder writes the argument for you.