How to Split and Upload Video in Real-Time Using FFmpeg
This article explains how to split an incoming video stream into small, manageable segments and upload them to a remote server in real-time using FFmpeg. You will learn the exact FFmpeg commands required to chunk video on the fly and transmit those segments instantly to an HTTP server using the built-in HTTP Live Streaming (HLS) publishing features.
The Real-Time Segmentation and Upload Process
To segment and upload video simultaneously, the most efficient method
is using FFmpeg’s native HLS (HTTP Live Streaming) muxer. This tool
automatically cuts the incoming video into segments (usually
.ts files) and generates a playlist (.m3u8
file). By utilizing the HTTP POST or PUT
methods built directly into FFmpeg, these files are sent to your
destination web server as soon as they are created.
The FFmpeg Command
Run the following command to start the real-time segmenting and uploading process:
ffmpeg -re -i input.mp4 -c:v libx264 -c:a aac -f hls \
-hls_time 4 \
-hls_list_size 5 \
-hls_flags delete_segments \
-method PUT \
http://yourserver.com/live/stream.m3u8Parameter Breakdown
-re: Tells FFmpeg to read the input at the native frame rate. This is crucial for simulating a real-time live stream.-i input.mp4: Defines your source video. This can be a local file, a live camera feed, or an RTSP stream.-c:v libx264 -c:a aac: Encodes the video to H.264 and the audio to AAC, which are standard, highly compatible formats for web streaming.-f hls: Specifies the output format as HLS.-hls_time 4: Sets the target segment duration to 4 seconds. FFmpeg will attempt to split the video at the nearest keyframe every 4 seconds.-hls_list_size 5: Keeps only the 5 most recent segments in the playlist file, which minimizes server storage use during live broadcasts.-hls_flags delete_segments: Automatically deletes old segments from the server once they fall out of the playlist range.-method PUT: Instructs FFmpeg to use HTTPPUTrequests to upload the playlist and video segments to the target server. You can change this toPOSTdepending on your server configuration.http://yourserver.com/live/stream.m3u8: The destination URL on your remote server where the playlist and segments (stream0.ts,stream1.ts, etc.) will be uploaded.
Server-Side Requirements
For this setup to work, your receiving server (such as Nginx, Apache,
or a Node.js backend) must be configured to accept HTTP PUT
or POST requests at the specified directory. Ensure that
the server has write permissions enabled for that location so FFmpeg can
successfully save the .m3u8 and .ts files as
they are uploaded in real-time.