Mr.Gu's StudioMr.Gu's Studio
开通会员 · 查看专属内容
笔记 MP3批量压缩脚本

无界面、无体积上限、处理速度最快,且完全免费开源。通过 PowerShell 脚本即可实现多线程或遍历文件夹批量压制。

第一步:环境准备

winget install Gyan.FFmpeg

如果错误,系统没有没有内置应用安装程序

快速自动化部署 FFmpeg

直接复制并执行以下这段脚本(自动下载官方构建版、解压至 C:\ffmpeg 并写入系统环境变量):

$ErrorActionPreference = "Stop"
# 1. 设置安装路径并创建目录
$installDir = "C:\ffmpeg"
if (!(Test-Path $installDir)) { New-Item -ItemType Directory -Path $installDir | Out-Null }

# 2. 从官方源下载精简发布版 (约 30MB)
$zipUrl = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
$zipPath = "$env:TEMP\ffmpeg-release.zip"

Write-Host "正在下载 FFmpeg..." -ForegroundColor Cyan
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $zipUrl -OutFile $zipPath

# 3. 解压并提取 bin 目录中的核心可执行文件
Write-Host "正在解压文件..." -ForegroundColor Cyan
$tempExtract = "$env:TEMP\ffmpeg-extract"
Expand-Archive -Path $zipPath -DestinationPath $tempExtract -Force

$binFolder = Get-ChildItem -Path $tempExtract -Recurse -Directory -Filter "bin" | Select-Object -First 1
Copy-Item -Path "$($binFolder.FullName)\*" -Destination $installDir -Force

# 4. 清理临时下载文件
Remove-Item -Path $zipPath -Force
Remove-Item -Path $tempExtract -Recurse -Force

# 5. 写入系统级 Path 环境变量(需要管理员权限),并同步生效到当前会话
$currentPath = [Environment]::GetEnvironmentVariable("Path", "Machine")
if ($currentPath -notlike "*$installDir*") {
    [Environment]::SetEnvironmentVariable("Path", "$currentPath;$installDir", "Machine")
}
$env:Path = "$env:Path;$installDir"

Write-Host "FFmpeg 安装配置完成!" -ForegroundColor Green

验证步骤

脚本执行完毕后,在当前窗口直接运行:

ffmpeg -version

第二步:打开目标目录终端

以管理员或当前用户打开 Windows PowerShell ,定位至音频文件夹:

Set-Location -Path "D:\music"

第三步:执行批量转码替换脚本

Get-ChildItem -LiteralPath . -Filter *.mp3 | Where-Object { $_.Name -notmatch '\.tmp\.mp3$' } | ForEach-Object {
    $src = $_.FullName
    $tmp = "$($_.FullName).tmp.mp3"
    Write-Host "正在压缩: $($_.Name)..." -ForegroundColor Cyan

    # 调用 FFmpeg 压制为 192 kbps,保留元数据与封面
    & ffmpeg -hide_banner -loglevel error -y -i $src -c:a libmp3lame -b:a 192k -map 0 -map_metadata 0 -id3v2_version 3 $tmp

    # 检查进程退出码与文件状态,成功则安全覆盖原文件
    if ($LASTEXITCODE -eq 0 -and (Test-Path -LiteralPath $tmp)) {
        Remove-Item -LiteralPath $src -Force
        Move-Item -LiteralPath $tmp -Destination $src -Force
        Write-Host "压缩成功: $($_.Name)" -ForegroundColor Green
    } else {
        Write-Host "压缩失败: $($_.Name)" -ForegroundColor Red
        if (Test-Path -LiteralPath $tmp) { Remove-Item -LiteralPath $tmp -Force }
    }
}

第四步:清理与验证

  • 清理过程残留临时脚本/文件:
Remove-Item .\run.bat, .\compress.ps1, ./*.tmp.mp3 -Force -ErrorAction SilentlyContinue
  • 验证文件体积:
Get-ChildItem -Filter *.mp3 | Select-Object Name, @{N="体积(MB)";E={[math]::Round($_.Length / 1MB, 2)}} | Format-Table -AutoSize
Mr.Gu's Studio

会员登录