Quick way to find out how much silence is at the start of an MP3?

4

What's a quick and easy way to find out how much silence is at the start of an MP3? I know there's a lot that goes into that... I don't need anything too precise. Within 50 or so milliseconds is great.

I have 1000 or so MP3s I want to do this with, so a solution I could script with would be best. Thanks!

Note that I don't want to trim the silence, I need to know the length of the silence.

Hilton Campbell

Posted 2011-04-05T22:13:51.363

Reputation: 143

Answers

1

Since you haven't specified your OS, I'll assume Windows.

Use ffmpeg in combination with PowerShell ISE

PowerShell

$folder = "C:\path\to\musicfolder"
$ffmpeg = "C:\path\to\ffmpeg.exe"

$content = "track start     track end   Filepath`n"
Get-ChildItem $folder -Recurse -Include *.mp3,*.ogg,*.flac  | foreach {        

    $log = & $ffmpeg -hide_banner -i `"$_`" -af "silencedetect=duration=2:noise=-50dB" -f null - 2>&1
    #echo [string]$log

    $totalLength = [string]$log | where {$_ -match '(?<= Duration:.*)[\d:\.]+' } | % {$matches[0]}        
    $totalLength = ([TimeSpan]::Parse($totalLength)).TotalSeconds

    [string[]]$silenceEnd = $log | where {$_ -match '(?<=silence_end: )[-\d\.]+' } | % {$matches[0]}            
    If ($silenceEnd.count -gt 0 -And [double]$silenceEnd[0] -lt $totalLength/2) {
        [string]$trackStart = $silenceEnd[0]
    } else {
        [string]$trackStart = 0
    }

    [string[]]$silenceStart = $log | where {$_ -match '(?<=silence_start: )[-\d\.]+' } | % {$matches[0]}                
    If ($silenceStart.count -gt 0 -And $silenceStart[$silenceStart.count-1] -gt $totalLength/2) {
        [string]$trackEnd = $silenceStart[$silenceStart.count-1]
    } else {        
        [string]$trackEnd = $totalLength
    }    
    $content += "$trackStart                $trackEnd       $_  `n"
}
Clear-Content "$folder\silenceLog.txt"
Add-Content "$folder\silenceLog.txt" $content

Example output

enter image description here

  • First column is a time stamp in seconds and milliseconds where silence ends and music starts
  • Second column is a time stamp in seconds and milliseconds where music ends and silence starts
  • Third column is the file path

nixda

Posted 2011-04-05T22:13:51.363

Reputation: 23 233