FFmpeg滤镜(6)
FFmpeg音频流滤镜操作
1、双声道合并单声道
将stereo转变为mono模式:
ffmpeg -i input.aac -ac 1 output.aac
Input #0, aac, from 'input.aac':
Duration: 00:00:32.14, bitrate: 154 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 154 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, adts, to 'output.aac':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: aac (LC), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc58.35.100 aac
size= 267kB time=00:00:30.32 bitrate= 72.0kbits/s speed= 127x
video:0kB audio:258kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.468710%
verhead: 3.468710%
原本双声道的音频,左耳右耳都可以听到声音,调整后依然可以左右耳都听到声音,只是布局发生了改变,为中央布局。
2、双声道提取
将音频为stereo的布局提取为两个mono流,左声道一个流,右声道一个流:
ffmpeg -i input.aac -map_channel 0.0.0 left.aac -map_channel 0.0.1 right.aac
Input #0, aac, from 'input.aac':
Duration: 00:00:32.14, bitrate: 154 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 154 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> aac (native))
Stream #0:0 -> #1:0 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
-map_channel is forwarded to lavfi similarly to -af pan=0x4|c0=c0.
[pan @ 0x7f922be08380] Pure channel mapping detected: 0
Output #0, adts, to 'left.aac':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: aac (LC), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc58.35.100 aac
-map_channel is forwarded to lavfi similarly to -af pan=0x4|c0=c1.
[pan @ 0x7f922bd0f9c0] Pure channel mapping detected: 1
Output #1, adts, to 'right.aac':
Metadata:
encoder : Lavf58.20.100
Stream #1:0: Audio: aac (LC), 44100 Hz, mono, fltp, 69 kb/s
Metadata:
encoder : Lavc58.35.100 aac
size= 266kB time=00:00:30.32 bitrate= 72.0kbits/s speed=65.9x
video:0kB audio:515kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[aac @ 0x7f922c803e00] Qavg: 395.652
[aac @ 0x7f922d001e00] Qavg: 395.069
从上述输出中可以看到,input.aac为stereo,而left.aac与right.aac为mono。
3、双声道转双音频流
FFmpeg不但可以将双声道音频提取出来生成两个音频文件,还可以将双声道音频提取出来转为一个音频文件两个音频流,每个音频流为一个声道:
ffmpeg -i input.aac -filter_complex channelsplit=channel_layout=stereo output.mka
Input #0, aac, from 'input.aac':
Duration: 00:00:32.14, bitrate: 154 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 154 kb/s
Stream mapping:
Stream #0:0 (aac) -> channelsplit
channelsplit:FL -> Stream #0:0 (ac3)
channelsplit:FR -> Stream #0:1 (ac3)
Press [q] to stop, [?] for help
Output #0, matroska, to 'output.mka':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: ac3 ([0] [0][0] / 0x2000), 44100 Hz, mono, fltp, 96 kb/s
Metadata:
encoder : Lavc58.35.100 ac3
Stream #0:1: Audio: ac3 ([0] [0][0] / 0x2000), 44100 Hz, mono, fltp, 96 kb/s
Metadata:
encoder : Lavc58.35.100 ac3
size= 724kB time=00:00:30.33 bitrate= 195.5kbits/s speed= 123x
video:0kB audio:711kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.813252%
如上信息所示,文件output.mka中的音频为两个mono的stream,大多数播放器在默认情况下会播放第一个音频stream但不会播放第二个。
4、单声道转双声道
FFmpeg可以将单声道转换为双声道,即当只有中央声道或只有mono布局时,才可以通过FFmpeg转换为stereo布局:
ffmpeg -i left.aac -ac 2 output.m4a
该命令执行完成后,将会从left.aac中,将布局为mono的音频转换为stereo布局的音频文件output.m4a。
Input #0, aac, from 'left.aac':
Duration: 00:00:30.31, bitrate: 72 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, mono, fltp, 72 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
Output #0, ipod, to 'output.m4a':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s
Metadata:
encoder : Lavc58.35.100 aac
size= 482kB time=00:00:30.34 bitrate= 130.2kbits/s speed=64.6x
video:0kB audio:477kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.235094%
[aac @ 0x7fae31804400] Qavg: 638.979
除了使用ac参数,还可以使用amerge滤镜进行处理:
ffmpeg -i left.aac -filter_complex "[0:a][0:a]amerge=inputs=2[aout]" -map "[aout]" output.m4a
Input #0, aac, from 'left.aac':
Duration: 00:00:30.31, bitrate: 72 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, mono, fltp, 72 kb/s
Stream mapping:
Stream #0:0 (aac) -> amerge:in0
Stream #0:0 (aac) -> amerge:in1
amerge -> Stream #0:0 (aac)
Press [q] to stop, [?] for help
[Parsed_amerge_0 @ 0x7ffebe607bc0] No channel layout for input 1
[Parsed_amerge_0 @ 0x7ffebe607bc0] Input channel layouts overlap: output layout will be determined by the number of distinct input channels
Output #0, ipod, to 'output.m4a':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
encoder : Lavc58.35.100 aac
size= 483kB time=00:00:30.34 bitrate= 130.3kbits/s speed= 72x
video:0kB audio:477kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.234608%
[aac @ 0x7ffebe804400] Qavg: 635.810
命令行执行后的效果与使用ac参数的效果相同。
另外,这样执行之后的双声道并不是真正的双声道,而是由单声道处理成的多声道,效果不会比原有的多声道效果好。
5、两个音频源合并双声道
前面说的将单mono处理为双声道,如果将输入的单mono转换为stereo双声道为伪双声道,则可以考虑将两个音频源合并为双声道,相对来说这样操作更容易理解些。
输入两个布局为mono的音频源,合并为一个布局为stereo双声道的音频流,输出到output文件:
ffmpeg -i left.aac -i right.aac -filter_complex "[0:a][1:a] amerge=inputs=2[aout]" -map "[aout]" output.mka
命令行执行后,会讲left.aac与right.aac两个音频为mono布局的AAC文件合并为一个布局为stereo的音频流,输出至output.mka文件。
[aac @ 0x7f97e9002200] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'left.aac':
Duration: 00:00:30.31, bitrate: 72 kb/s
Stream #0:0: Audio: aac (LC), 44100 Hz, mono, fltp, 72 kb/s
[aac @ 0x7f97ea001200] Estimating duration from bitrate, this may be inaccurate
Input #1, aac, from 'right.aac':
Duration: 00:00:32.99, bitrate: 66 kb/s
Stream #1:0: Audio: aac (LC), 44100 Hz, mono, fltp, 66 kb/s
Stream mapping:
Stream #0:0 (aac) -> amerge:in0
Stream #1:0 (aac) -> amerge:in1
amerge -> Stream #0:0 (ac3)
Press [q] to stop, [?] for help
[Parsed_amerge_0 @ 0x7f97e8c16ec0] No channel layout for input 1
[Parsed_amerge_0 @ 0x7f97e8c16ec0] Input channel layouts overlap: output layout will be determined by the number of distinct input channels
Output #0, matroska, to 'output.mka':
Metadata:
encoder : Lavf58.20.100
Stream #0:0: Audio: ac3 ([0] [0][0] / 0x2000), 44100 Hz, stereo, fltp, 192 kb/s (default)
Metadata:
encoder : Lavc58.35.100 ac3
size= 719kB time=00:00:30.36 bitrate= 193.9kbits/s speed= 119x
video:0kB audio:712kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.949075%
从以上信息可以看出,输入的两路mono转换为stereo了,输出音频为AC3。
6、多个音频合并为多声道
除了双声道音频,FFmpeg还可以支持多声道,通过ffmpeg -layouts即可看到声道布局有很多种,常见的多声道还有一种是5.1方式的多声道,也叫六声道环绕声,是由前置左、右声道,后置环绕左、右声道、中央声道(0.1声道)、重低音声道组成。
Individual channels:
NAME DESCRIPTION
FL front left
FR front right
FC front center
LFE low frequency
BL back left
BR back right
FLC front left-of-center
FRC front right-of-center
BC back center
SL side left
SR side right
TC top center
TFL top front left
TFC top front center
TFR top front right
TBL top back left
TBC top back center
TBR top back right
DL downmix left
DR downmix right
WL wide left
WR wide right
SDL surround direct left
SDR surround direct right
LFE2 low frequency 2
Standard channel layouts:
NAME DECOMPOSITION
mono FC
stereo FL+FR
2.1 FL+FR+LFE
3.0 FL+FR+FC
3.0(back) FL+FR+BC
4.0 FL+FR+FC+BC
quad FL+FR+BL+BR
quad(side) FL+FR+SL+SR
3.1 FL+FR+FC+LFE
5.0 FL+FR+FC+BL+BR
5.0(side) FL+FR+FC+SL+SR
4.1 FL+FR+FC+LFE+BC
5.1 FL+FR+FC+LFE+BL+BR
5.1(side) FL+FR+FC+LFE+SL+SR
6.0 FL+FR+FC+BC+SL+SR
6.0(front) FL+FR+FLC+FRC+SL+SR
hexagonal FL+FR+FC+BL+BR+BC
6.1 FL+FR+FC+LFE+BC+SL+SR
6.1(back) FL+FR+FC+LFE+BL+BR+BC
6.1(front) FL+FR+LFE+FLC+FRC+SL+SR
7.0 FL+FR+FC+BL+BR+SL+SR
7.0(front) FL+FR+FC+FLC+FRC+SL+SR
7.1 FL+FR+FC+LFE+BL+BR+SL+SR
7.1(wide) FL+FR+FC+LFE+BL+BR+FLC+FRC
7.1(wide-side) FL+FR+FC+LFE+FLC+FRC+SL+SR
octagonal FL+FR+FC+BL+BR+BC+SL+SR
hexadecagonal FL+FR+FC+BL+BR+BC+SL+SR+TFL+TFC+TFR+TBL+TBC+TBR+WL+WR
downmix DL+DR