Limiting an audio buffer's size in ffmpeg like ShadowPlay

I'm trying to set up an audio capture system somewhat like Nvidia ShadowPlay, wherein there's a background program that's constantly keeping a buffer of the last X minutes of footage (or audio in this case) and, at command, saves it permanently to a separate file. What I've come up with so far is:

ffmpeg -f dshow -i audio="My input device" buffer.wav

to record, and then

ffmpeg.exe -sseof -30 -i buffer.wav -c copy capture.wav

To copy the last 30 seconds of the buffer. However, this comes with the issue of the buffer file reaching up to hundreds of megabytes in a matter of minutes. I'd like to be able to instead adjust the maximum length of the recording, so that once it has surpassed this limit, it starts cutting off at the front of the file. In other words, if there's a 5 minute limit on the file, and it has been recording for 5 minutes and 10 seconds, the first 10 seconds that it recorded should be deleted, thereby making sure the file never surpasses five minutes of duration.


Solution 1:

You can use the segment muxer.

ffmpeg -f dshow -i audio="My input device" -af aresample=async=1 -f segment -segment_time 30 -segment_wrap 2 buffer%d.wav

This will create, first, buffer0.wav. Once it has 30 seconds, it will write to buffer1.wav till that file has 30 seconds.

Once buffer1 is filled up, it will write next 30 seconds of data to buffer0 again. So, at any time after the first 30 seconds of recording, you will have at least 30 seconds and upto 60 seconds of data.

Simply sort the files in ascending order according to Last modified time and use last 30 seconds from their concatenated data.