How to Split RTMP Stream by Size Using FFmpeg

Recording a live RTMP stream to a local drive is a common task, but continuous recording can result in massive, unmanageable files. This article provides a direct, step-by-step guide on how to capture an RTMP feed using FFmpeg and split the output into smaller, local files based on size. You will learn both the strict byte-size splitting method using system pipes and the recommended video-safe time-approximation method.


Method 1: Strict Size Splitting (Using Pipes)

FFmpeg does not have a native option to split files precisely by file size (such as exactly 100 MB) because doing so can break video containers and corrupt file headers. However, you can achieve strict size splitting by piping FFmpeg’s output into the standard command-line utility split.

To do this, you must output the video in a streamable format like MPEG-TS (-f mpegts), which can be safely split at arbitrary byte boundaries.

Run the following command in your terminal (Linux/macOS):

ffmpeg -i rtmp://your-server-ip/live/stream -c copy -f mpegts - | split -b 100M --filter='cat > $FILE.ts' - chunk_

How it works:


If you want to ensure your output files are perfectly compatible with all media players and have correct duration headers, you should split the files using FFmpeg’s native segment muxer.

Because FFmpeg segments by time rather than file size, you must calculate the segment duration based on your stream’s bitrate.

Step 1: Calculate the segment time

Use this formula to find the time limit for your target file size:

\[\text{Segment Time (seconds)} = \frac{\text{Target Size in Bits}}{\text{Stream Bitrate in Bits Per Second}}\]

Example: If your stream has a bitrate of 2 Mbps (2,000 kbps) and you want 100 MB files: * 100 MB = 800 Megabits (800,000,000 bits) * 2 Mbps = 2,000,000 bits per second * 800,000,000 / 2,000,000 = 400 seconds

Step 2: Run the FFmpeg command

Use the calculated time (e.g., 400 seconds) in the following command:

ffmpeg -i rtmp://your-server-ip/live/stream -c copy -f segment -segment_time 400 -reset_timestamps 1 -segment_format mp4 output_%03d.mp4

How it works: