To extract every 5-second chunk centered around audio spikes (peaks) using ffmpeg in PowerShell on Windows, you need to:
- Detect the timestamps of audio spikes.
- Extract 5-second segments centered on those timestamps (±2.5 seconds).
Step 1: Detect audio spikes (peaks)
You can use ffmpeg's astats or volumedetect filter to analyze audio levels, but for precise spike detection, ffmpeg's silencedetect or astats with a custom script is better.
A common approach is to use ffmpeg's astats filter to output per-frame volume stats, then parse those to find spikes.
Example command to get volume stats per frame:
ffmpeg -i input.wav -af astats=metadata=1:reset=1 -f null -
This outputs stats like:
[Parsed_astats_0 @ 0000023f8c1a0c00]
frame: 0
...
lavfi.astats.Overall.RMS_level=-20.0
...
You can parse the RMS level or peak level to find spikes.
Step 2: Parse spikes and extract segments
You want to:
- Parse the output to find timestamps where the volume exceeds a threshold.
- For each timestamp, extract a 5-second clip centered on it.
Full example workflow in PowerShell
1. Extract volume stats with timestamps
ffmpeg -i input.wav -af astats=metadata=1:reset=1 -f null - 2>&1 |
Select-String "lavfi.astats.Overall.RMS_level" |
ForEach-Object {
# Extract frame number and RMS level
$line = $_.Line
if ($line -match "frame:\s*(\d+)") { $frame = [int]$matches[1] }
if ($line -match "lavfi.astats.Overall.RMS_level=(-?\d+\.?\d*)") {
$rms = [double]$matches[1]
# Calculate timestamp from frame number (assuming 25 fps)
$fps = 25
$time = $frame / $fps
[PSCustomObject]@{Time=$time; RMS=$rms}
}
} | Where-Object { $_.RMS -gt -20 } > spikes.txt
Adjust -20 to your spike threshold (in dB).
2. Extract timestamps from spikes.txt
$spikes = Get-Content spikes.txt | ForEach-Object {
if ($_ -match "Time\s*=\s*(\d+\.?\d*)") { [double]$matches[1] }
}
3. Extract 5-second clips around each spike
$input = "input.wav"
$outputDir = "clips"
New-Item -ItemType Directory -Path $outputDir -Force | Out-Null
foreach ($t in $spikes) {
$start = [math]::Max(0, $t - 2.5)
$outputFile = Join-Path $outputDir ("clip_{0:N2}.wav" -f $t)
ffmpeg -ss $start -i $input -t 5 -c copy $outputFile
}
Notes:
- Adjust the frame rate (
$fps) to your audio sample rate or frame rate.
- The threshold
-20 dB is an example; tune it to your audio.
- This method assumes spikes are separated enough to avoid overlapping clips.
- You can improve spike detection by smoothing or grouping close spikes.
If you want a simpler but less precise method, you can use ffmpeg's silencedetect filter to find loud parts and extract around those.
If you want me to help you with a ready-to-run script or a more precise method, just ask!