如何在python中的波形文件中转换电报语音 [英] How to convert Telegram voice in a wave file in python

查看:49
本文介绍了如何在python中的波形文件中转换电报语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to save a Telegram voice file in a wave audio file using soundfile library:

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.

推荐答案

你可以尝试使用 ffmpeg通过使用 python 的模块子进程执行以下命令行,将 ogg 转换为 wav.

You could try to use ffmpeg to convert from ogg to wav by executing the following command line using python's module subprocess.

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")

这篇关于如何在python中的波形文件中转换电报语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆