TechBuzzPro

Fix FFmpeg Reconnect Errors on a 24/7 Meditation Stream

· 6 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

If you're running a 24/7 meditation stream from India, you've likely seen FFmpeg drop the connection to YouTube or Twitch, especially during monsoon season or power fluctuations. This guide walks you through the exact settings and commands to make your stream resilient, so it stays up without manual restarts.

What You'll Need

  • A Linux server or PC (Ubuntu/Debian recommended) with FFmpeg installed (version 4.x or newer).
  • Root or sudo access to edit system files.
  • Your streaming key (RTMP URL) from YouTube, Twitch, or Facebook.
  • Basic comfort with the terminal and a text editor like nano or vim.

Step-by-Step

  1. Identify the exact error. Run your stream command and watch the output. Common errors are Connection refused, Broken pipe, Server error, or End of file. Note the line where it fails – this tells you if it's a network issue, a server reject, or a codec problem.
  2. Add the basic reconnect flags. The most important FFmpeg options are -reconnect 1 (enable reconnection), -reconnect_streamed 1 (reconnect even if the stream is mid-way), -reconnect_delay_max 30 (wait up to 30 seconds between attempts). Your output command should look like:
    ffmpeg -re -i your_input.mp4 -c:v libx264 -preset veryfast -b:v 2500k -maxrate 2500k -bufsize 5000k -c:a aac -b:a 128k -f flv "rtmp://a.rtmp.youtube.com/live2/STREAM_KEY" -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 30
    Note: Place these flags after the output URL, not before.
  3. Set a higher delay and infinite attempts. By default, FFmpeg tries a limited number of times. Add -reconnect_at_eof 1 to reconnect if the input file ends (useful for looping videos). To make it retry forever, use -reconnect_delay_max 60 and add -timeout 30 (for the input) and -rw_timeout 30000000 (30,000,000 microseconds = 30 seconds for output). This prevents FFmpeg from giving up after a few seconds.
  4. Loop your input file. For a 24/7 stream, you need a loop. Use -stream_loop -1 before the input file. This tells FFmpeg to loop the video forever. Combine it with -re (read input at native framerate) to avoid sending frames too fast.
  5. Wrap the command in a shell loop. Even with reconnect flags, FFmpeg can crash due to a bug or a codec issue. Create a script that restarts FFmpeg automatically:
    #!/bin/bash
    while true; do
      ffmpeg -re -stream_loop -1 -i /path/to/meditation.mp4 \
        -c:v libx264 -preset veryfast -b:v 2500k -maxrate 2500k -bufsize 5000k \
        -c:a aac -b:a 128k -f flv "rtmp://a.rtmp.youtube.com/live2/STREAM_KEY" \
        -reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 60 -timeout 30 -rw_timeout 30000000
      echo "FFmpeg exited, restarting in 5 seconds..."
      sleep 5
    done
    Save this as stream.sh, make it executable with chmod +x stream.sh, and run it with nohup ./stream.sh & to keep it running after you log out.
  6. Test with a short stream. Before going live 24/7, run the script for 10 minutes. Unplug your ethernet cable or disconnect Wi-Fi for 30 seconds. Watch if FFmpeg reconnects and the stream resumes without a black screen.
  7. Monitor and log. Add -loglevel warning to reduce noise, but also redirect output to a log file: nohup ./stream.sh > stream.log 2>&1 &. Check the log periodically with tail -f stream.log to see reconnection attempts.

Common Mistakes

  • Putting reconnect flags before the input. They must go after the output URL. If you put them before, FFmpeg ignores them.
  • Not using -re with -stream_loop. Without -re, FFmpeg reads the file as fast as possible, causing high CPU and a stream that buffers on the viewer's side.
  • Using a too-low -reconnect_delay_max. If your internet drops for 2 minutes, a 10-second max delay will fail. Set it to at least 30, ideally 60.
  • Forgetting to set -rw_timeout. This is the read/write timeout for the output. Without it, FFmpeg may hang indefinitely instead of reconnecting.
  • Not testing. A 24/7 stream will fail eventually. Test the reconnect by pulling the network cable – if it doesn't recover, your settings are wrong.

Tips

  • Use a static IP or a reliable DNS like Google's (8.8.8.8) to avoid DNS resolution failures.
  • In India, power cuts are common. Use a UPS for your streaming machine, and consider a 4G LTE backup router that automatically switches if your wired connection drops.
  • Lower your bitrate to 1500-2000k if your upload speed is below 2 Mbps. A stable low-bitrate stream beats a high-bitrate one that keeps buffering.
  • Use -preset ultrafast if your CPU is old, but be aware it increases file size and may reduce quality. For meditation streams (mostly static visuals), veryfast is fine.
  • Set up a cron job to restart the stream script every 24 hours: 0 0 * * * pkill ffmpeg; sleep 5; nohup /path/to/stream.sh > /dev/null 2>&1 &. This clears any memory leaks.

Troubleshooting

  • Still getting "Connection refused"? Your streaming key may be wrong or expired. Double-check the RTMP URL and key from your platform's dashboard. Also, ensure your firewall allows outbound traffic on port 1935 (RTMP).
  • Reconnects but stream stays black? This often happens when the input file is corrupted or the loop point causes a glitch. Try re-encoding the file first: ffmpeg -i input.mp4 -c:v libx264 -preset veryfast -c:a aac -movflags +faststart output.mp4.
  • FFmpeg crashes with "Segmentation fault"? Update FFmpeg to the latest version. On Ubuntu, use sudo apt update && sudo apt upgrade ffmpeg. If it persists, your input file may have a bad frame – use a different file.
  • High CPU usage (above 80%)? Lower the bitrate or use -preset ultrafast. Also, avoid running other heavy processes on the same machine.
  • Stream drops every hour exactly? Some platforms have a session timeout. YouTube, for example, may drop idle streams. Add a subtle audio track (like a bell every 30 minutes) to keep the stream active.

Frequently Asked Questions

Why does FFmpeg keep disconnecting even with -reconnect 1?

Often because the reconnect flags are placed incorrectly (before the input) or the server is rejecting the connection due to an invalid key. Also, if your internet goes down completely, FFmpeg can't reconnect until it's back – the script loop handles that.

Can I use a free CDN or VPN to improve stability?

Using a VPN may actually increase latency and cause more drops. Stick to a direct connection. A CDN is not needed for streaming to YouTube/Twitch – they have their own edge networks.

How do I know if my internet is the problem?

Run a continuous ping to 8.8.8.8 in another terminal. If you see packet loss or high latency during a drop, it's your network. If ping stays stable, the issue is likely FFmpeg or the platform.

Is it better to use -reconnect_delay_max 30 or 60?

60 is safer for 24/7 streams, but if your internet drops for more than a minute, the platform may end the stream session anyway. In that case, the script loop will restart a new session.

Do I need to change anything for YouTube vs Twitch?

The RTMP URL and key differ, but the FFmpeg flags are the same. Twitch is more sensitive to bitrate spikes, so keep your bitrate constant and use -maxrate and -bufsize as shown.

Can I stream from a Raspberry Pi?

Yes, but only if you use hardware encoding (-c:v h264_omx or h264_v4l2m2m). Software encoding on a Pi will overheat and crash. Also, use a lower resolution like 720p.