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:
-i rtmp://...: Specifies the input RTMP stream URL.-c copy: Copies the video and audio streams directly without re-encoding, preserving quality and keeping CPU usage extremely low.-f mpegts -: Forces the output format to MPEG-TS and sends it tostdout(standard output).|: Pipes the stream data to the next command.split -b 100M: Instructs the system to split the incoming stream every 100 Megabytes.--filter='cat > $FILE.ts': Dynamically names each split chunk with a.tsextension (e.g.,chunk_aa.ts,chunk_ab.ts).
Method 2: Safe Segment Splitting (Recommended for Video Integrity)
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.mp4How it works:
-f segment: Activates the segment muxer.-segment_time 400: Splits the stream into a new file every 400 seconds (approximately 100 MB per file based on a 2 Mbps bitrate).-reset_timestamps 1: Resets the timestamps at the beginning of each segment so that each output file starts at 00:00:00.output_%03d.mp4: Names the files sequentially (e.g.,output_001.mp4,output_002.mp4).