音频处理
FFMPEG 提供了强大的音频处理功能,包括提取、转码、格式转换、降噪等操作。
音频提取
从视频中提取音频
bash
ffmpeg -i input.mp4 -vn -acodec copy output.aac提取为 MP3
bash
ffmpeg -i input.mp4 -vn -acodec libmp3lame -b:a 192k output.mp3提取为 WAV(无损)
bash
ffmpeg -i input.mp4 -vn -acodec pcm_s16le output.wav参数说明
-vn: 禁用视频(不处理视频流)-acodec copy: 音频直接复制,不重新编码-acodec libmp3lame: 使用 MP3 编码器-b:a: 音频比特率
音频格式转换
MP3 转 AAC
bash
ffmpeg -i input.mp3 -acodec aac -b:a 192k output.aacWAV 转 MP3
bash
ffmpeg -i input.wav -acodec libmp3lame -b:a 192k output.mp3转换为 FLAC(无损)
bash
ffmpeg -i input.mp3 -acodec flac output.flac转换为 OGG
bash
ffmpeg -i input.mp3 -acodec libvorbis output.ogg音频质量设置
音频比特率
bash
# 高质量(320kbps)
ffmpeg -i input.mp3 -acodec libmp3lame -b:a 320k output.mp3
# 中等质量(192kbps)
ffmpeg -i input.mp3 -acodec libmp3lame -b:a 192k output.mp3
# 低质量(128kbps)
ffmpeg -i input.mp3 -acodec libmp3lame -b:a 128k output.mp3采样率调整
bash
# 设置为 44.1kHz(CD 质量)
ffmpeg -i input.mp3 -ar 44100 output.mp3
# 设置为 48kHz(专业音频)
ffmpeg -i input.mp3 -ar 48000 output.mp3声道处理
bash
# 转换为单声道
ffmpeg -i input.mp3 -ac 1 output.mp3
# 转换为立体声
ffmpeg -i input.mp3 -ac 2 output.mp3音频合并
合并多个音频文件
bash
# 创建文件列表
echo "file 'audio1.mp3'" > filelist.txt
echo "file 'audio2.mp3'" >> filelist.txt
echo "file 'audio3.mp3'" >> filelist.txt
# 合并
ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp3合并视频和音频
bash
ffmpeg -i video.mp4 -i audio.mp3 \
-c:v copy \
-c:a aac \
-strict experimental \
output.mp4替换视频中的音频
bash
ffmpeg -i video.mp4 -i new_audio.mp3 \
-c:v copy \
-c:a aac \
-map 0:v:0 -map 1:a:0 \
output.mp4音频处理技巧
音频降噪
bash
ffmpeg -i input.mp3 -af "highpass=f=200,lowpass=f=3000" output.mp3音量调整
bash
# 增加音量(2倍)
ffmpeg -i input.mp3 -af "volume=2.0" output.mp3
# 减少音量(0.5倍)
ffmpeg -i input.mp3 -af "volume=0.5" output.mp3
# 标准化音量
ffmpeg -i input.mp3 -af "loudnorm=I=-16:TP=-1.5:LRA=11" output.mp3音频淡入淡出
bash
# 淡入 3 秒,淡出 3 秒
ffmpeg -i input.mp3 -af "afade=t=in:ss=0:d=3,afade=t=out:st=57:d=3" output.mp3提取音频片段
bash
# 从第 10 秒开始,提取 30 秒
ffmpeg -i input.mp3 -ss 00:00:10 -t 00:00:30 output.mp3音频加速/减速
bash
# 1.5 倍速(音调不变)
ffmpeg -i input.mp3 -filter:a "atempo=1.5" output.mp3
# 0.75 倍速(音调不变)
ffmpeg -i input.mp3 -filter:a "atempo=0.75" output.mp3
# 2 倍速(需要多次应用 atempo,最大 2.0)
ffmpeg -i input.mp3 -filter:a "atempo=2.0,atempo=2.0" output.mp3音频混音
混合两个音频文件
bash
ffmpeg -i audio1.mp3 -i audio2.mp3 \
-filter_complex "[0:a][1:a]amix=inputs=2:duration=first:dropout_transition=2" \
output.mp3添加背景音乐
bash
# 背景音乐音量降低 50%
ffmpeg -i main_audio.mp3 -i bg_music.mp3 \
-filter_complex "[0:a]volume=1.0[a0];[1:a]volume=0.5[a1];[a0][a1]amix=inputs=2" \
output.mp3音频信息查看
查看音频详细信息
bash
ffprobe -v quiet -print_format json -show_format -show_streams input.mp3查看音频时长
bash
ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 input.mp3查看音频比特率
bash
ffprobe -v error -select_streams a:0 -show_entries stream=bit_rate -of default=noprint_wrappers=1:nokey=1 input.mp3批量音频处理脚本
批量提取音频
bash
#!/bin/bash
INPUT_DIR="./videos"
OUTPUT_DIR="./audio"
mkdir -p "$OUTPUT_DIR"
for file in "$INPUT_DIR"/*.mp4; do
filename=$(basename "$file")
output="$OUTPUT_DIR/${filename%.mp4}.mp3"
echo "提取: $filename"
ffmpeg -i "$file" -vn -acodec libmp3lame -b:a 192k "$output"
done批量转换格式
bash
#!/bin/bash
INPUT_DIR="./audio"
OUTPUT_DIR="./audio_mp3"
mkdir -p "$OUTPUT_DIR"
for file in "$INPUT_DIR"/*.wav; do
filename=$(basename "$file")
output="$OUTPUT_DIR/${filename%.wav}.mp3"
echo "转换: $filename"
ffmpeg -i "$file" -acodec libmp3lame -b:a 192k "$output"
done常用音频格式
| 格式 | 编码器 | 特点 | 适用场景 |
|---|---|---|---|
| MP3 | libmp3lame | 兼容性好,有损 | 通用音频 |
| AAC | aac | 效率高,有损 | 移动设备、流媒体 |
| FLAC | flac | 无损压缩 | 高质量音频 |
| WAV | pcm_s16le | 无损,文件大 | 专业音频 |
| OGG | libvorbis | 开源,有损 | 开源项目 |
注意事项
- 音频质量:比特率越高质量越好,但文件也越大
- 格式兼容性:MP3 和 AAC 兼容性最好
- 采样率:44.1kHz 和 48kHz 最常用
- 声道:立体声(2声道)是标准配置
- 处理速度:音频处理通常比视频处理快很多
下一步:学习高级技巧相关内容。
