# [FFmpeg] 音量調整
## 改變音量
如果要改變音量,你可以使用 `volume` 篩選器,用法如下。
0.5倍音量:
```powershell
ffmpeg -i input.wav -af "volume=0.5" output.wav
```
1.5倍音量:
```powershell
ffmpeg -i input.wav -af "volume=1.5" output.wav
```
使用分貝單位:
```powershell
ffmpeg -i input.wav -af "volume=5dB" output.wav
```
## 峰值正常化
如果要將一個 音訊的音量正常化,可以使用 volumedetect 檢測 音量峰值 `max_volume`,然後使用 `volume` 篩選器將音量峰值歸零或接近零即可,如果音量峰值是 -6 dB 則提高 6 dB 就能歸零。
例如要將一個 WAV 音訊轉為 AAC 格式並將音量正常化。
首先,使用 `VolumeDetect` 檢測音量:
```powershell
ffmpeg -i input.wav -af "volumedetect" -f null -
```
假設 VolumeDetect 輸出訊息為:
```text
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] mean_volume: -16.0 dB
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] max_volume: -5.0 dB
[Parsed_volumedetect_0 @ 0x7f8ba1c121a0] histogram_0db: 87861
```
承上,輸出訊息得知音量峰值 `max_volume` 為 -5 dB,所以要將音量峰值歸零需要提高 5dB。
音量峰值歸零需要提高 5dB:
```powershell
ffmpeg -i input.wav -af "volume=5dB" output.wav
```
如果用了 pan filter 或是 `-ac` 選項來混合聲道,你會發現輸出的音訊的音量變小,可以用同樣的參數設定加上 `volumedetect` 篩選器來檢測 混合聲道後的音量。
使用 `volumedetect` 檢測縮混之後的音量:
```powershell
ffmpeg -i input.wav -ac 2 -af "volumedetect" -f null -
```
> 注意:因為是要檢測 downmix 為雙聲道之後的音量所以 `-ac 2` 是必要的。
假設 `volumedetect` 輸出訊息為:
```test
[Parsed_volumedetect_0 @ 00600c60] n_samples: 814080
[Parsed_volumedetect_0 @ 00600c60] mean_volume: -29.5 dB
[Parsed_volumedetect_0 @ 00600c60] max_volume: -7.9 dB
```
由上面輸出訊息得知音量峰值 (`max_volume`) 為 -7.9 dB,所以要將音量峰值歸零需要提高 7.9dB。
## 感知音量正常化
使用 `loudnorm` 篩選器正常化音量:
```powershell
ffmpeg -i input.wav -filter:a loudnorm output.wav
```
### 取得統計資料
設定 `print_format` 參數之值為 `json` 或 `summary`,輸出統計資料將包含音源與輸出的 I (Integrated Loudness, 綜合響度)、LRA (Loudness Range, 響度範圍)、 TP (True Peak, 真實峰值)、Threshold (閾值) 之值。
```powershell
ffmpeg -i input.wav -filter:a loudnorm=print_format=json output.wav
```
```json
[Parsed_loudnorm_0 @ 000001ae4d5bc040]
{
"input_i" : "-24.09",
"input_tp" : "-7.96",
"input_lra" : "4.80",
"input_thresh" : "-35.76",
"output_i" : "-23.23",
"output_tp" : "-8.22",
"output_lra" : "4.00",
"output_thresh" : "-34.40",
"normalization_type" : "dynamic",
"target_offset" : "-0.77"
}
```
如果只是要取得音源的 I、LAR、TP、Threshold 之值,可以使用 `null` demuxer 不輸出檔案。
```powershell
ffmpeg -i input.wav -filter:a loudnorm=print_format=json -f null -
```
### 二階段處裡
使用二階段處裡來得到更加準確的音量正常化效果。
承上,再次使用 `loudnorm` 篩選器處理音源,設定 `linear` 參數之值為 `true`,以應用線性縮放,並設定 `measured_I`, `measured_LRA`, `measured_TP`, `measured_thresh` 為之前獲得的音源 I、LRA、TP、Threshold 之值。
```powershell
ffmpeg -input.wav -filter:a loudnorm=linear=true:measured_I=-24.09:measured_LRA=4.80:measured_TP=-7.96:measured_thresh=-35.76 output.wav
```
###### tags: `ffmpeg`