ffmpeg & ffmpeg-python 试用(音频)
before
ffmpeg -i test.wav
Input #0, wav, from 'test.wav':
Duration: 00:00:16.31, bitrate: 256 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
ffmpeg -i examples_sample.wav
Input #0, wav, from 'examples_sample.wav':
Duration: 00:00:03.42, bitrate: 1572 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 65536 Hz, mono, s32 (24 bit), 1572 kb/s
参数简要说明
-i
设定输入流
-f
设定输出格式
-ar
设定采样率
-ac
设定声音的channel数
-acodec
设定声音解码器
相同的输出设置:
ffmpeg命令行:ffmpeg -i example.wav -acodec pcm_s16le -ar 16000 -ac 1 out.wav
ffmpeg-python代码:
import ffmpeg
input_file='./examples_sample.wav' # 'test.wav'
(
ffmpeg
.input(input_file)
.output('./testout1.wav',**{'ar':'16000','ac':'1','acodec':'pcm_s16le'})
.run()
)
after
test.wav
似乎没什么变化..前后格式一致
Input #0, wav, from './test.wav':
Duration: 00:00:16.31, bitrate: 256 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s16le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to './testout.wav':
Metadata:
ISFT : Lavf58.76.100
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Metadata:
encoder : Lavc58.134.100 pcm_s16le
size= 510kB time=00:00:16.25 bitrate= 256.9kbits/s speed=1.07e+03x
video:0kB audio:510kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.014943%
examples_sample.wav
可以观察到采样率有变化了... 文件大小位107kb
Input #0, wav, from './examples_sample.wav':
Duration: 00:00:03.42, bitrate: 1572 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 65536 Hz, mono, s32 (24 bit), 1572 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s24le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to './testout1.wav':
Metadata:
ISFT : Lavf58.76.100
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s
Metadata:
encoder : Lavc58.134.100 pcm_s16le
size= 107kB time=00:00:03.42 bitrate= 256.3kbits/s speed= 252x
video:0kB audio:107kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.071170%
改一下采样率为18000,可以发现文件大小为120kb,相同时间,更高的采样率,音频更大
stream = ffmpeg.input(input_file)
#stream = ffmpeg.hflip(stream)
stream = ffmpeg.output(stream, './testout2.wav',**{'ar':'18000','ac':'1','acodec':'pcm_s16le'})
ffmpeg.run(stream)
run
Input #0, wav, from './examples_sample.wav':
Duration: 00:00:03.42, bitrate: 1572 kb/s
Stream #0:0: Audio: pcm_s24le ([1][0][0][0] / 0x0001), 65536 Hz, mono, s32 (24 bit), 1572 kb/s
Stream mapping:
Stream #0:0 -> #0:0 (pcm_s24le (native) -> pcm_s16le (native))
Press [q] to stop, [?] for help
Output #0, wav, to './testout2.wav':
Metadata:
ISFT : Lavf58.76.100
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 18000 Hz, mono, s16, 288 kb/s
Metadata:
encoder : Lavc58.134.100 pcm_s16le
size= 120kB time=00:00:03.42 bitrate= 288.3kbits/s speed= 258x
video:0kB audio:120kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.063262%
上面的内容,主要还是一些知识点整理和个人的一些思考,权当参考,如有错误或者更好的建议,可以在评论区指正,不胜感激!
可执行代码