使用FFmpeg搭建YouTube无限循环RTMP电台遇故障求助
I’ve run into similar issues with the concat demuxer causing stream interruptions when looping media files of vastly different lengths. Let’s break down why your original command failed and fix it with a more reliable approach.
Why Your Concat Method Caused Skipping
The concat demuxer works by reading files sequentially from your text lists. Every time FFmpeg finishes one entry in video.txt or audio.txt, it has to close the current file, open the next one, and reinitialize the decoder. For a 10-second video looping thousands of times, this constant file switching creates tiny but noticeable interruptions (the 10-second skips you observed). Plus, since your video and audio have wildly different loop cycles (10s vs 4hrs), their concat sequences never align, amplifying the problem.
The -stream_loop parameter does work for multi-input streams—you just need to apply it to each input individually instead of trying to use a global setting.
Working FFmpeg Command
Here’s a command that loops your video and audio independently without skips, while matching your desired output parameters:
ffmpeg \ # Loop the 10-second video infinitely -stream_loop -1 -i video.mp4 \ # Loop the 4-hour audio infinitely -stream_loop -1 -i audio.flac \ # Process video and audio with filter complex (keeps streams independent) -filter_complex " [0:v]scale=1920:1080,fps=4,format=yuv420p[v]; [1:a]aac=bit_rate=256000[a] " \ # Map the processed video and audio streams to output -map "[v]" -map "[a]" \ # Video encoding settings -c:v libx264 \ -b:v 2500k \ -maxrate 1024k \ -bufsize 2048k \ # Fixed your typo (048k → 2048k; smooths bitrate fluctuations) -preset veryfast \ -q:v 3 \ -threads 2 \ # Output as FLV for YouTube RTMP -f flv \ "$YOUTUBE_URL/$KEY"
Key Improvements Explained
- Per-input
-stream_loop -1: This tells FFmpeg to loop each source infinitely at the decoder level, no file switching required. The video will loop every 10 seconds, the audio every 4 hours—completely independent, no sync needed. - Filter Complex: Centralizes video/audio processing to apply your desired settings (resolution, frame rate, pixel format, audio bitrate) without disrupting the loop behavior.
- Fixed Bitrate Buffering: Your original
bufsize 048kwas almost certainly a mistake. 2048k is a reasonable value for RTMP streaming, helping prevent YouTube from dropping the stream due to sudden bitrate spikes. - Explicit Mapping: Using
-mapensures we only output the processed video and audio streams, avoiding unintended extra streams from the original inputs.
Quick Checks to Avoid Issues
- Ensure
video.mp4uses a compatible codec (H.264 works best for RTMP). If FFmpeg struggles to loop it, re-encode once withffmpeg -i video.mp4 -c copy video-loopable.mp4to fix container oddities. - If you hear audio artifacts, add
-ar 44100to the audio filter to enforce a standard sample rate for YouTube.
内容的提问来源于stack exchange,提问作者Jarro

