I want to manipulate gtts audio in pydub but I am not sure how to translate gtts file like to pydub audio.
I know that I can convert google text to speech audio to an mp3 and I know that I can import an mp3 with pydub, but this process is inefficient. Is there a way to skip creating mp3 file and reading it back in?
I tried using the gtts file-like as the argument in AudioSegment.from_mp3()
but I am pretty sure it is looking for a string.
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)
I got a "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'