Extract Audio at Regular Intervals with FFmpeg aselect

This article demonstrates how to use the FFmpeg aselect filter to extract specific audio segments at regular, repeating intervals from an audio or video file. You will learn the exact syntax for the filter expression, how the underlying math works, and how to reset audio timestamps so your extracted segments play continuously without silent gaps.

The Core Command

To extract audio segments at regular intervals, you must combine the aselect filter with the asetpts filter. The aselect filter decides which audio frames to keep, while asetpts reconstructs the audio timeline so the output plays smoothly.

Here is the template command to keep the first 5 seconds of audio out of every 30 seconds:

ffmpeg -i input.mp3 -af "aselect='lt(mod(t,30),5)',asetpts=N/SR/TB" output.mp3

How the Filter Expression Works

The magic happens inside the single quotes of the aselect filter: 'lt(mod(t,30),5)'

  1. t: This represents the current timestamp of the audio in seconds.
  2. mod(t, 30): The modulo operator divides the current time t by 30 and returns the remainder. This creates a repeating 30-second loop (from 0 to 29.99 seconds).
  3. lt(..., 5): The “less than” function checks if the resulting modulo value is less than 5. If it is (meaning the time is between 0–5s, 30–35s, 60–65s, etc.), the audio is kept. If it is 5 or greater, the audio is discarded.
  4. asetpts=N/SR/TB: When FFmpeg discards audio, it leaves blank gaps in the timeline. This filter recalculates the Presentation Timestamps (PTS) using the sample count (N), sample rate (SR), and timebase (TB), stitching the saved segments together into a single, continuous audio file.

Alternative Example: Short Bursts

If you want to extract a 1-second snippet of audio every 10 seconds, you would adjust the interval (10) and the duration (1) parameters like this:

ffmpeg -i input.wav -af "aselect='lt(mod(t,10),1)',asetpts=N/SR/TB" output.wav

This command evaluates the audio timeline in 10-second blocks, keeping only the first second of each block and discarding the remaining 9 seconds.