2015年11月7日 星期六

調整 mp4 檔案的音量 (normalize)

雖然,avidemux 也可以做 normalize,但不會用,後來找到用 ffmpeg 就可以了。

參考連結
How can I normalize audio using ffmpeg?   其中 [Manually normalizing audio with ffmpeg]
Normalize audio in an avi file

Manually normalizing audio with ffmpeg

In ffmpeg you can use the volume filter to change the volume of a track. Make sure you download a recent version of the program.
This guide is for peak normalization, meaning that it will make the loudest part in the file sit at 0 dB instead of something lower. There is also RMS-based normalization which tries to make the average loudness the same across multiple files. To do that, do not try to push the maximum volume to 0 dB, but the mean volume to the dB level of choice (e.g. -26 dB).

Find out the gain to apply

First you need to analyze the audio stream for the maximum volume to see if normalizing would even pay off:
ffmpeg -i video.avi -af "volumedetect" -vn -sn -dn -f null /dev/null
Replace /dev/null with NUL on Windows.
The -vn-sn, and -dn arguments instruct ffmpeg to ignore non-audio streams during this analysis. This drastically speeds up the analysis.
This will output something like the following:
[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
As you can see, our maximum volume is -5.0 dB, so we can apply 5 dB gain. If you get a value of 0 dB, then you don't need to normalize the audio.

Apply the volume filter:

Now we apply the volume filter to an audio file. Note that applying the filter means we will have to re-encode the audio stream. What codec you want for audio depends on the original format, of course. Here are some examples:
  • Plain audio file: Just encode the file with whatever encoder you need:
    ffmpeg -i input.wav -af "volume=5dB" output.mp3
    
    Your options are very broad, of course.
  • AVI format: Usually there's MP3 audio with video that comes in an AVI container:
    ffmpeg -i video.avi -af "volume=5dB" -c:v copy -c:a libmp3lame -q:a 2 output.avi
    
    Here we chose quality level 2. Values range from 0–9 and lower means better. Check the MP3 VBR guide for more info on setting the quality. You can also set a fixed bitrate with -b:a 192k, for example.
  • MP4 format: With an MP4 container, you will typically find AAC audio. We can use ffmpeg's build-in AAC encoder.
    ffmpeg -i video.mp4 -af "volume=5dB" -c:v copy -c:a aac -b:a 192k output.mp4
    
    Here you can also use other AAC encoders. Some of them support VBR, too. See this answer and the AAC encoding guide for some tips.
In the above examples, the video stream will be copied over using -c:v copy. If there are subtitles in your input file, or multiple video streams, use the option -map 0 before the output filename.

步驟摘要整理

找出聲音的大小,峰值為 -7.5 dB
ffmpeg -i v1.mp4 -af "volumedetect" -f null /dev/null
[Parsed_volumedetect_0 @ 0x1589580] n_samples: 397301760
[Parsed_volumedetect_0 @ 0x1589580] mean_volume: -40.3 dB
[Parsed_volumedetect_0 @ 0x1589580] max_volume: -7.5 dB

 將聲音提升 7.5 dB
ffmpeg -i v1.mp4 -af "volume=7.5dB" -c:v copy -c:a aac -strict experimental -b:a 128k v1n.mp4

處理後,得到的值
[Parsed_volumedetect_0 @ 0x2159720] n_samples: 397303808
[Parsed_volumedetect_0 @ 0x2159720] mean_volume: -32.9 dB
[Parsed_volumedetect_0 @ 0x2159720] max_volume: -0.2 dB

分離 mp3,另用 audacity 處理
ffmpeg -i ch1.mp4 -q:a 0 -map a ch1.mp3

https://www.youtube.com/watch?v=NgedD2zKhdc
Audacity Tutorial: Amplification, Compression, Normalizing and Exporting Your Podcast

使用 compressor 將大小聲做調整,不致動態太大。
http://manual.audacityteam.org/o/man/compressor.html

沒有留言:

張貼留言

網誌存檔