我正在使用Python的wave
模块读取音频,并使用FFmpeg将音频从其他类型转换为wav。但是,我遇到了一些问题。
我写了v.py
来生成静音音频文件a.wav
import sys, wave, math
import numpy as np
wave_data = np.zeros(44100).astype(np.short)
f = wave.open('a.wav', 'wb')
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(96000)
f.writeframes(wave_data.tostring())
f.close()
然后,我使用FFmpeg将
a.wav
“复制”到b.wav
(尽管似乎可以对文件进行编码/解码),但是我只能使用Python读取a.wav
。 b.wav
无法打开。[user@localhost tmp]$ ffmpeg -i a.wav b.wav
Guessed Channel Layout for Input Stream #0.0 : mono
Input #0, wav, from 'a.wav':
Duration: 00:00:00.46, bitrate: 1536 kb/s
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 96000 Hz, mono, s16, 1536 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 'b.wav':
Metadata:
ISFT : Lavf57.71.100
Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 96000 Hz, mono, s16, 1536 kb/s
Metadata:
encoder : Lavc57.89.100 pcm_s16le
size= 86kB time=00:00:00.45 bitrate=1537.8kbits/s speed= 706x
video:0kB audio:86kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.115646%
[user@localhost tmp]$ python3
Python 3.6.4 (default, Jan 23 2018, 22:25:37)
[GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wave
>>> wave.open('a.wav')
<wave.Wave_read object at 0x7efea1c5e550>
>>> wave.open('b.wav')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python3.6/wave.py", line 499, in open
return Wave_read(f)
File "/usr/lib64/python3.6/wave.py", line 163, in __init__
self.initfp(f)
File "/usr/lib64/python3.6/wave.py", line 143, in initfp
self._read_fmt_chunk(chunk)
File "/usr/lib64/python3.6/wave.py", line 260, in _read_fmt_chunk
raise Error('unknown format: %r' % (wFormatTag,))
wave.Error: unknown format: 65534
>>>
如何更改FFmpeg的命令以将文件转换为WAVE_FORMAT_PCM,以便可以使用Python读取
b.wav
? 最佳答案
issue是Python的wave模块不支持导入采样率大于48 kHz的文件。 MP3中介路由之所以起作用,是因为在这种情况下,ffmpeg会将输入自动下采样到48 kHz。据报道,scipy可以导入48+ kHz的文件。
使用ffmpeg手动下采样到48 kHz的语法是
ffmpeg -i in -ar 48000 out.wav
附言要跳过解码/编码,请使用
ffmpeg -i in.wav -c copy out.wav
。关于python - 如何使用FFmpeg将音频转换为WAVE_FORMAT_PCM?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48740160/