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:

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.