乐闻世界logo
搜索文章和话题

如何使用 FFmpeg 进行音频处理?常用的音频滤镜有哪些?

2月18日 10:43

FFmpeg 提供了强大的音频处理功能,包括音频提取、转换、混音、降噪等多种操作。

音频提取与转换

从视频中提取音频

bash
# 提取音频流(不重新编码) ffmpeg -i video.mp4 -vn -acodec copy audio.aac # 提取并转换为 MP3 ffmpeg -i video.mp4 -vn -acodec libmp3lame -ab 192k audio.mp3 # 提取并转换为 WAV ffmpeg -i video.mp4 -vn audio.wav

音频格式转换

bash
# MP3 转 AAC ffmpeg -i input.mp3 -c:a aac -b:a 128k output.aac # WAV 转 MP3 ffmpeg -i input.wav -c:a libmp3lame -b:a 192k output.mp3 # FLAC 转 MP3(有损压缩) ffmpeg -i input.flac -c:a libmp3lame -b:a 320k 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 "volume=3dB" output.mp3 # 归一化音量 ffmpeg -i input.mp3 -af "loudnorm" output.mp3

调整采样率

bash
# 转换为 44.1kHz ffmpeg -i input.wav -ar 44100 output.wav # 转换为 48kHz ffmpeg -i input.wav -ar 48000 output.wav # 转换为 16kHz(语音识别常用) ffmpeg -i input.wav -ar 16000 output.wav

调整声道

bash
# 转换为单声道 ffmpeg -i input.mp3 -ac 1 output.mp3 # 转换为立体声 ffmpeg -i input.mp3 -ac 2 output.mp3 # 提取左声道 ffmpeg -i input.mp3 -af "pan=mono|c0=c0" output.mp3 # 提取右声道 ffmpeg -i input.mp3 -af "pan=mono|c0=c1" output.mp3

音频剪辑

按时间剪辑

bash
# 从 10 秒开始,剪辑 30 秒 ffmpeg -i input.mp3 -ss 00:00:10 -t 00:00:30 output.mp3 # 从 1 分钟开始,剪辑到 3 分钟 ffmpeg -i input.mp3 -ss 00:01:00 -to 00:03:00 output.mp3 # 剪辑前 60 秒 ffmpeg -i input.mp3 -t 60 output.mp3

音频拼接

bash
# 使用 concat 协议拼接 echo "file 'part1.mp3'" > filelist.txt echo "file 'part2.mp3'" >> filelist.txt echo "file 'part3.mp3'" >> filelist.txt ffmpeg -f concat -safe 0 -i filelist.txt -c copy output.mp3 # 使用 concat filter 拼接 ffmpeg -i part1.mp3 -i part2.mp3 -i part3.mp3 \ -filter_complex "[0:a][1:a][2:a]concat=n=3:v=0:a=1[outa]" \ -map "[outa]" output.mp3

音频效果

淡入淡出

bash
# 淡入 3 秒 ffmpeg -i input.mp3 -af "afade=t=in:st=0:d=3" output.mp3 # 淡出 3 秒 ffmpeg -i input.mp3 -af "afade=t=out:st=5:d=3" output.mp3 # 淡入淡出 ffmpeg -i input.mp3 -af "afade=t=in:st=0:d=3,afade=t=out:st=7:d=3" output.mp3

音频混音

bash
# 混合两个音频文件 ffmpeg -i audio1.mp3 -i audio2.mp3 -filter_complex "amix=inputs=2:duration=first" output.mp3 # 混合三个音频文件 ffmpeg -i audio1.mp3 -i audio2.mp3 -i audio3.mp3 \ -filter_complex "amix=inputs=3:duration=longest" output.mp3 # 调整混合比例 ffmpeg -i audio1.mp3 -i audio2.mp3 \ -filter_complex "[0:a]volume=0.7[a0];[1:a]volume=0.3[a1];[a0][a1]amix=inputs=2" output.mp3

音频延迟

bash
# 延迟音频 500 毫秒 ffmpeg -i input.mp3 -af "adelay=500|500" output.mp3 # 左右声道不同延迟 ffmpeg -i input.mp3 -af "adelay=500|1000" output.mp3 # 提前音频(负延迟) ffmpeg -i input.mp3 -af "adelay=-200|-200" output.mp3

音频降噪

bash
# 使用 highpass 滤波器去除低频噪音 ffmpeg -i input.mp3 -af "highpass=f=200" output.mp3 # 使用 lowpass 滤波器去除高频噪音 ffmpeg -i input.mp3 -af "lowpass=f=3000" output.mp3 # 使用 bandpass 滤波器 ffmpeg -i input.mp3 -af "bandpass=f=1000:width_type=h:width=500" output.mp3

音频分析与处理

音频可视化

bash
# 生成音频波形图 ffmpeg -i input.mp3 -filter_complex "showwavespic=s=640x320" waveform.png # 生成频谱图 ffmpeg -i input.mp3 -lavfi showspectrumpic=s=640x320 spectrum.png # 生成音频视频 ffmpeg -i input.mp3 -filter_complex "showwaves=s=640x320" output.mp4

音频信息查看

bash
# 查看音频详细信息 ffmpeg -i input.mp3 -hide_banner # 查看音频采样率、比特率等 ffprobe -i input.mp3 -show_streams -select_streams a # 分析音频音量 ffmpeg -i input.mp3 -af "volumedetect" -f null -

高级音频处理

音频变速

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 # 大幅变速(需要多次 atempo) ffmpeg -i input.mp3 -filter:a "atempo=2.0,atempo=2.0" output.mp3

音频变调

bash
# 提高音调(半音) ffmpeg -i input.mp3 -filter:a "asetrate=44100*1.05946" output.mp3 # 降低音调(半音) ffmpeg -i input.mp3 -filter:a "asetrate=44100*0.94387" output.mp4

音频回声

bash
# 添加回声效果 ffmpeg -i input.mp3 -af "aecho=0.8:0.9:1000:0.3" output.mp3

音频编码参数

常用音频编码器

bash
# AAC 编码 ffmpeg -i input.wav -c:a aac -b:a 128k output.aac # MP3 编码 ffmpeg -i input.wav -c:a libmp3lame -b:a 192k output.mp3 # Opus 编码(高质量) ffmpeg -i input.wav -c:a libopus -b:a 128k output.opus # FLAC 编码(无损) ffmpeg -i input.wav -c:a flac output.flac

音频质量设置

bash
# 高质量 MP3 (320kbps) ffmpeg -i input.wav -c:a libmp3lame -b:a 320k output.mp3 # 中等质量 MP3 (192kbps) ffmpeg -i input.wav -c:a libmp3lame -b:a 192k output.mp3 # 低质量 MP3 (128kbps) ffmpeg -i input.wav -c:a libmp3lame -b:a 128k output.mp3 # VBR 编码(可变比特率) ffmpeg -i input.wav -c:a libmp3lame -q:a 2 output.mp3

音频处理时,建议使用无损格式(如 WAV、FLAC)进行中间处理,最后再转换为有损格式以减少质量损失。

标签:FFmpeg