【问题标题】:How to convert Telegram voice in a wave file in python如何在 python 的波形文件中转换 Telegram 语音
【发布时间】:2018-04-16 07:55:02
【问题描述】:

我正在尝试使用声音文件库将 Telegram 语音文件保存在波形音频文件中:

def ReceiveVoice(bot, update, user_data):

   voice = bot.getFile(update.message.voice.file_id)
   voice.download('file.ogg')
   data, samplerate = sf.read('file.ogg')
   sf.write('file.wav', data, samplerate)

但我收到以下错误:

File "C:\Python27\lib\site-packages\soundfile.py", line 257, in read
subtype, endian, format, closefd) as f:
File "C:\Python27\lib\site-packages\soundfile.py", line 624, in __init__
self._file = self._open(file, mode_int, closefd)
File "C:\Python27\lib\site-packages\soundfile.py", line 1179, in _open
"Error opening {0!r}: ".format(self.name))
File "C:\Python27\lib\site-packages\soundfile.py", line 1352, in _error_check
raise RuntimeError(prefix + _ffi.string(err_str).decode('utf-8', 'replace'))
RuntimeError: Error opening 'file.ogg': File contains data in an unimplemented format.

【问题讨论】:

  • 你解决了吗?遇到同样的问题

标签: python telegram-bot python-telegram-bot


【解决方案1】:

您可以尝试使用ffmpegogg 转换为wav,方法是使用python 的模块子进程执行以下命令行。

import subprocess
src_filename = 'captured.ogg'
dest_filename = 'output.wav'

process = subprocess.run(['ffmpeg', '-i', src_filename, dest_filename])
if process.returncode != 0:
    raise Exception("Something went wrong")

【讨论】:

  • FileNotFoundError: [WinError 2] 输出文件需要存在吗?
【解决方案2】:

试试这个

import soundfile as sf
data, samplerate = sf.read(input_ogg)
sf.write(output_wav, data, samplerate)

【讨论】:

  • 但它不适用于 oga(电报音频)文件格式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 2012-11-17
相关资源
最近更新 更多