如何使用FFmpeg将音频转换为WAVE_FORMAT_PCM?

时间:2018-02-12 05:41:58

标签: python audio ffmpeg pcm

我正在使用Python的 public MyViewHolder(View view){ super(view); txt=view.findViewById(R.id.txt_item_rec); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int pos=getAdapterPosition(); if(pos!=RecyclerView.NO_POSITION) { Toast.makeText(view.getContext(),strings.get(pos),Toast.LENGTH_SHORT).show(); APIService mapi= ApiUtils.getAPIService(); mapi.importFileExcel(strings.get(pos)).enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { if(response.isSuccessful()){ try { Toast.makeText(context,response.body().string(),Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); } } } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { } }); } } }); } } 模块来读取音频,并使用FFmpeg将其他类型的音频转换为wav。但是,我遇到了一些问题。

我写了wave来生成静音音频文件v.py

a.wav

然后我使用FFmpeg将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() “复制”到a.wav(虽然它似乎编码/解码文件),但我只能用Python读取b.wav; a.wav无法打开。

b.wav

如何更改FFmpeg的命令以将文件转换为WAVE_FORMAT_PCM,以便我可以用Python阅读[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 >>>

1 个答案:

答案 0 :(得分:1)

issue是Python的wave模块不支持导入采样率大于48 kHz的文件。 MP3中介路由有效,因为在这种情况下,ffmpeg会自动将输入下采样到48 kHz。据报道,scipy可以导入48+ kHz文件。

使用ffmpeg手动下采样到48 kHz的语法是

ffmpeg -i in -ar 48000 out.wav

P.S。要跳过解码/编码,请使用ffmpeg -i in.wav -c copy out.wav