Stream Live DASH Video with FFmpeg Segment Cleanup

This article provides a straightforward guide on how to stream live video using the MPEG-DASH protocol with FFmpeg while automatically cleaning up old video segments. When streaming live, continuously generating video segments can quickly deplete your server's storage; this guide explains the exact FFmpeg parameters required to maintain a rolling window of active segments and delete expired files automatically.

To stream live DASH video and dynamically clean up old segments, you must configure FFmpeg's DASH muxer to use a sliding window. This configuration ensures that FFmpeg only keeps a specified number of recent segments on your disk, automatically deleting older files as new ones are created.

The FFmpeg Command

Below is a standard FFmpeg command configured for a live DASH stream with dynamic segment cleanup:

ffmpeg -re -i input.mp4 \
  -c:v libx264 -b:v 3000k -g 60 -keyint_min 60 -sc_threshold 0 \
  -c:a aac -b:a 128k \
  -f dash \
  -window_size 5 \
  -extra_window_size 3 \
  -remove_at_exit 1 \
  -seg_duration 4 \
  -use_template 1 \
  -use_timeline 1 \
  manifest.mpd

Key Parameters Explained

To achieve dynamic segment cleanup and efficient live streaming, the following DASH-specific parameters are critical:

  • -f dash: Specifies that the output format is MPEG-DASH.
  • -seg_duration 4: Sets the target duration for each video segment to 4 seconds. Consistent segment sizes are crucial for smooth live playback.
  • -window_size 5: Defines the maximum number of segments kept in the manifest file (.mpd) at any given time. In this example, only the 5 most recent segments are advertised to players.
  • -extra_window_size 3: Dictates how many segments are kept on the disk outside of the manifest window before being deleted. Here, FFmpeg keeps 5 active segments plus 3 older segments (a total of 8 files on disk) to prevent playback errors for slow clients, automatically deleting anything older.
  • -remove_at_exit 1: Instructs FFmpeg to automatically delete all generated chunk files and the main .mpd manifest file from your storage when the FFmpeg process is stopped or interrupted.
  • -use_template 1 and -use_timeline 1: Enables template-based segment naming and generates bandwidth-efficient manifests that are easier for media players to parse during live playback.

By combining -window_size, -extra_window_size, and -remove_at_exit, you can run a continuous live stream indefinitely without running out of disk space.