本文目录导读:

使用 FFmpeg 脚本
Windows (PowerShell)
# 批量转换当前目录下所有音频文件为16位深度
Get-ChildItem -Path ".\" -Filter "*.wav" | ForEach-Object {
$output = "converted_" + $_.Name
ffmpeg -i $_.FullName -sample_fmt s16 -ar 44100 $output
}
# 处理多种格式
$formats = @("*.wav", "*.flac", "*.aiff")
$formats | ForEach-Object {
Get-ChildItem -Path ".\" -Filter $_ | ForEach-Object {
$output = [System.IO.Path]::GetFileNameWithoutExtension($_.FullName) + "_16bit.wav"
ffmpeg -i $_.FullName -sample_fmt s16 $output
}
}
Windows (CMD批处理)
@echo off
setlocal enabledelayedexpansion
for %%f in (*.wav *.flac *.aiff) do (
ffmpeg -i "%%f" -sample_fmt s16 "16bit_%%~nf.wav"
echo 已转换: %%f -> 16bit_%%~nf.wav
)
echo 批量转换完成!
pause
Linux/Mac (Bash)
#!/bin/bash
# 设置输入输出目录
input_dir="./input"
output_dir="./output"
mkdir -p "$output_dir"
# 转换为24位深度
for file in "$input_dir"/*.{wav,flac,aiff}; do
if [ -f "$file" ]; then
filename=$(basename "$file")
ffmpeg -i "$file" -sample_fmt s32 "$output_dir/24bit_${filename%.*}.wav"
echo "已转换: $filename"
fi
done
使用 SoX 工具脚本
#!/bin/bash
# 将所有WAV文件转换为16位深度
for file in *.wav; do
output="sox_${file}"
sox "$file" -b 16 "$output"
done
Python 脚本 (使用 pydub)
from pydub import AudioSegment
import os
import glob
def batch_convert_bit_depth(input_dir, output_dir, target_bit_depth=16):
"""
批量转换音频位深度
Args:
input_dir: 输入目录
output_dir: 输出目录
target_bit_depth: 目标位深度 (8, 16, 24, 32)
"""
os.makedirs(output_dir, exist_ok=True)
# 支持的音频格式
formats = ["*.wav", "*.mp3", "*.flac", "*.aiff"]
for format in formats:
for file_path in glob.glob(os.path.join(input_dir, format)):
try:
# 加载音频文件
audio = AudioSegment.from_file(file_path)
# 设置采样率(根据位深度调整)
if target_bit_depth <= 16:
audio = audio.set_frame_rate(44100)
audio = audio.set_sample_width(2) # 16位 = 2字节
elif target_bit_depth <= 24:
audio = audio.set_frame_rate(48000)
audio = audio.set_sample_width(3) # 24位 = 3字节
else:
audio = audio.set_frame_rate(96000)
audio = audio.set_sample_width(4) # 32位 = 4字节
# 生成输出文件名
basename = os.path.basename(file_path)
name_without_ext = os.path.splitext(basename)[0]
output_path = os.path.join(output_dir, f"{name_without_ext}_{target_bit_depth}bit.wav")
# 导出音频
audio.export(output_path, format="wav")
print(f"已转换: {basename} -> {output_path}")
except Exception as e:
print(f"转换失败: {file_path}: {str(e)}")
# 使用示例
if __name__ == "__main__":
batch_convert_bit_depth(
input_dir="./audio_input",
output_dir="./audio_output",
target_bit_depth=16
)
高级 FFmpeg 脚本 (带选项控制)
Windows PowerShell (高级版)
param(
[string]$InputDir = ".\input",
[string]$OutputDir = ".\output",
[int]$BitDepth = 16,
[string]$Format = "wav"
)
# 创建输出目录
New-Item -ItemType Directory -Force -Path $OutputDir
# 设置采样格式映射
$sampleFormatMap = @{
8 = "u8"
16 = "s16"
24 = "s32" # FFmpeg用32位存储24位
32 = "s32"
}
$sampleFormat = $sampleFormatMap[$BitDepth]
# 获取所有音频文件
$audioFiles = Get-ChildItem -Path $InputDir -Include @("*.wav", "*.flac", "*.aiff", "*.mp3") -Recurse
foreach ($file in $audioFiles) {
$outputName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$outputPath = Join-Path $OutputDir "${outputName}_${BitDepth}bit.${Format}"
Write-Host "转换中: $($file.Name) -> $outputPath"
# FFmpeg命令
$ffmpegArgs = @(
"-i", "`"$($file.FullName)`"",
"-sample_fmt", $sampleFormat,
"-ar", "44100",
"-y",
"`"$outputPath`""
)
# 执行转换
ffmpeg $ffmpegArgs
if ($LASTEXITCODE -eq 0) {
Write-Host "成功: $($file.Name)" -ForegroundColor Green
} else {
Write-Host "失败: $($file.Name)" -ForegroundColor Red
}
}
Write-Host "`n批量转换完成!" -ForegroundColor Cyan
使用说明
-
安装必要工具:
- FFmpeg: https://ffmpeg.org/download.html
- SoX (可选): http://sox.sourceforge.net/
- Python (可选): 安装
pydub库 (pip install pydub)
-
运行脚本前:
- 备份原始文件
- 创建输入/输出目录
- 根据实际需求调整参数
-
常见位深度:
- 8位: 低质量(常用语音)
- 16位: CD标准质量
- 24位: 高分辨率音频
- 32位: 专业录音室质量
这些脚本可以帮你高效地批量转换音频文件的位深度。