in powershell on windows using ffmpeg, how do i extract every 5 second chunk (timestamp…
- User
in powershell on windows using ffmpeg, how do i extract every 5 second chunk (timestamp +- 2.5s) where an audio spike happens?
Assistant
Here's a PowerShell script that uses ffmpeg to detect audio spikes and extract 5-second chunks around them:
# Parameters $inputFile = "input.mp4" $outputDir = "audio_spikes" $threshold = -20 # dB threshold for spike detection (adjust as needed) $chunkDuration = 5 # seconds $offset = 2.5 # seconds before/after spike # Create output directory New-Item -ItemType Directory -Force -Path $outputDir | Out-Null # Step 1: Detect audio levels and find spikes Write-Host "Analyzing audio levels..." $audioData = & ffmpeg -i $inputFile -af "astats=metadata=1:reset=1,ametadata=print:key=lavfi.astats.Overall.RMS_level" -f null - 2>&1 # Parse the output to find timestamps where RMS level exceeds threshold $spikes = @() $audioData | ForEach-Object { if ($_ -match "pts_time:(\d+\.?\d*)" -and $lastTime) { $time = [double]$Matches[1] } if ($_ -match "lavfi\.astats\.Overall\.RMS_level=(-?\d+\.?\d*)") { $level = [double]$Matches[1] if ($level -gt $threshold -and $time) { $spikes += $time $lastTime = $null } } if ($_ -match "pts_time:(\d+\.?\d*)") { $lastTime = [double]$Matches[1] } } # Remove spikes that are too close together (within chunk duration) $filteredSpikes = @() $lastSpike = -999 foreach ($spike in $spikes) { if (($spike - $lastSpike) -ge $chunkDuration) { $filteredSpikes += $spike $lastSpike = $spike } } Write-Host "Found $($filteredSpikes.Count) audio spikes" # Step 2: Extract chunks around each spike $counter = 1 foreach ($spike in $filteredSpikes) { $startTime = [Math]::Max(0, $spike - $offset) $outputFile = Join-Path $outputDir ("spike_{0:D3}_{1:F1}s.mp4" -f $counter, $spike) Write-Host "Extracting chunk $counter at $($spike)s..." & ffmpeg -ss $startTime -i $inputFile -t $chunkDuration -c copy $outputFile -y $counter++ } Write-Host "Done! Extracted $($filteredSpikes.Count) chunks to $outputDir"For a more robust solution that uses ffmpeg's silencedetect filter (which detects non-silent parts):
# Alternative approach using silencedetect $inputFile = "input.mp4" $outputDir = "audio_spikes" $silenceThreshold = -30 # dB threshold for silence $minDuration = 0.5 # minimum duration of non-silence to consider New-Item -ItemType Directory -Force -Path $outputDir | Out-Null # Detect non-silent parts Write-Host "Detecting audio activity..." $output = & ffmpeg -i $inputFile -af "silencedetect=n=${silenceThreshold}dB:d=$minDuration" -f null - 2>&1 | Out-String # Parse silence_end times (which indicate start of audio activity) $pattern = "silence_end: (\d+\.?\d*)" $matches = [regex]::Matches($output, $pattern) $spikeTimes = $matches | ForEach-Object { [double]$_.Groups[1].Value } # Extract chunks $counter = 1 foreach ($spike in $spikeTimes) { $startTime = [Math]::Max(0, $spike - 2.5) $outputFile = Join-Path $outputDir ("spike_{0:D3}_{1:F1}s.mp4" -f $counter, $spike) Write-Host "Extracting chunk $counter at $($spike)s..." & ffmpeg -ss $startTime -i $inputFile -t 5 -c copy $outputFile -y $counter++ }To use these scripts:
-
Save either script to a
.ps1file -
Modify the parameters at the top:
$inputFile: Path to your input video/audio file$threshold: Adjust the dB threshold for what constitutes a "spike"$outputDir: Where to save the extracted chunks
-
Run the script in PowerShell
The first script analyzes RMS levels to find spikes, while the second uses silence detection to find where audio activity begins. You may need to adjust the threshold values depending on your audio content. Lower (more negative) values are quieter, higher values are louder.
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.