gpt4 book ai didi

pydub - 如何有效地将 gtts 音频转换为 pydub 音频片段?

转载 作者:行者123 更新时间:2023-12-04 13:41:55 32 4
gpt4 key购买 nike

我想在 pydub 中操作 gtts 音频,但我不确定如何将 gtts 文件转换为 pydub 音频。

我知道我可以将谷歌文本转换为语音音频到 mp3,我知道我可以使用 pydub 导入 mp3,但是这个过程效率低下。有没有办法跳过创建 mp3 文件并将其读回?

我尝试使用 gtts 文件作为 AudioSegment.from_mp3() 中的参数但我很确定它正在寻找一个字符串。

from gtts import gTTS
from io import BytesIO
from pydub import AudioSegment

mp3_fp = BytesIO()
tts = gTTS('hello', 'en')
tts.write_to_fp(mp3_fp)
song = AudioSegment.from_mp3(mp3_fp)

我得到了一个“CouldntDecodeError”:
  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/py/ex/gtts_test.py", line 18, in <module>
song = AudioSegment.from_mp3(mp3_fp)

File "C:\ProgramData\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 716, in from_mp3
return cls.from_file(file, 'mp3', parameters=parameters)

File "C:\ProgramData\Anaconda3\lib\site-packages\pydub\audio_segment.py", line 704, in from_file
p.returncode, p_err))

CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1

Output from ffmpeg/avlib:

b'ffmpeg version 4.1.1 Copyright (c) 2000-2019 the FFmpeg developers\r\n built with gcc 8.2.1 (GCC) 20190212\r\n configuration: --enable-gpl --enable-version3 --enable-sdl2 --enable-fontconfig --enable-gnutls --enable-iconv --enable-libass --enable-libbluray --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libopus --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libtheora --enable-libtwolame --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libzimg --enable-lzma --enable-zlib --enable-gmp --enable-libvidstab --enable-libvorbis --enable-libvo-amrwbenc --enable-libmysofa --enable-libspeex --enable-libxvid --enable-libaom --enable-libmfx --enable-amf --enable-ffnvcodec --enable-cuvid --enable-d3d11va --enable-nvenc --enable-nvdec --enable-dxva2 --enable-avisynth\r\n libavutil 56. 22.100 / 56. 22.100\r\n libavcodec 58. 35.100 / 58. 35.100\r\n libavformat 58. 20.100 / 58. 20.100\r\n libavdevice 58. 5.100 / 58. 5.100\r\n libavfilter 7. 40.101 / 7. 40.101\r\n libswscale 5. 3.100 / 5. 3.100\r\n libswresample 3. 3.100 / 3. 3.100\r\n libpostproc 55. 3.100 / 55. 3.100\r\n[mp3 @ 000001da0c1292c0] Failed to read frame size: Could not seek to 1026.\r\npipe:: Invalid argument\r\n'

最佳答案

您必须寻求 BytesIO 对象为 0!

from gtts import gTTS
from io import BytesIO
from pydub import AudioSegment

mp3_fp = BytesIO()
tts = gTTS('hello', 'en')
tts.write_to_fp(mp3_fp)
mp3_fp.seek(0)
song = AudioSegment.from_mp3(mp3_fp)

关于pydub - 如何有效地将 gtts 音频转换为 pydub 音频片段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55962939/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com