我想在pydub中处理gtts音频,但是我不确定如何将gtts文件转换为pydub音频。
我知道我可以将Google文本转换为语音音频并转换为mp3,并且知道可以使用pydub导入mp3,但是此过程效率不高。是否可以跳过创建mp3文件并将其读回的方法?
我在AudioSegment.from_mp3()
中尝试使用gtts文件之类的参数作为参数,但是我很确定它正在寻找字符串。
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'
答案 0 :(得分:0)
您必须将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)