Record Live Stream and Split into 1-Hour Files with FFmpeg
This article provides a straightforward guide on how to use FFmpeg to capture a live streaming video and automatically split the recording into individual, one-hour files. By utilizing FFmpeg’s built-in segment muxer, you can continuously record a stream over long periods without overloading your system’s storage or creating single, unmanageably large video files.
To record a live stream and split it into hourly chunks, you should
use the FFmpeg segment muxer. This tool splits the incoming
stream on the fly without needing to stop and restart the
connection.
Here is the standard command to achieve this:
ffmpeg -i "STREAM_URL" -c copy -f segment -segment_time 3600 -reset_timestamps 1 -strftime 1 "output_%Y-%m-%d_%H-%M-%S.mp4"How the Command Works
-i "STREAM_URL": Replace"STREAM_URL"with the link to your live stream (such as an HLS.m3u8link, an RTMP stream, or an RTSP stream).-c copy: This stream-copies the audio and video payloads without re-encoding them. It keeps CPU usage extremely low and preserves the original quality of the stream.-f segment: This tells FFmpeg to use the segment muxer, which splits the output into multiple files.-segment_time 3600: Defines the target duration for each segment in seconds. Since one hour contains 3600 seconds, this setting creates 1-hour files.-reset_timestamps 1: Resets the timestamps at the beginning of each segment. This ensures that each output file starts at 0:00 and plays correctly in media players.-strftime 1: Enables the use of standard date and time layout tags in the output filename.output_%Y-%m-%d_%H-%M-%S.mp4: The naming template for the output files. FFmpeg will name each file based on the exact start time of the segment (e.g.,output_2023-10-27_14-00-00.mp4).
Ensuring Accurate Splits
FFmpeg can only split a video stream cleanly on a keyframe (I-frame). If your live stream does not have a keyframe exactly at the 1-hour mark, FFmpeg will split the file at the nearest keyframe immediately following the 3600-second mark. This prevents any video corruption at the boundaries of your files.