Exception has occurred: CouldntDecodeError
Decoding failed. ffmpeg returned error code: 4294967274

Output from ffmpeg/avlib:

ffmpeg version N-113433-gbab7f91c36-20240127 Copyright © 2000-2024 the FFmpeg developers
built with gcc 13.2.0 (crosstool-NG 1.25.0.232_c175b21)
configuration: --prefix=/ffbuild/prefix --pkg-config-flags=–static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libharfbuzz --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libaribcaption --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --enable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20240127
libavutil 58. 36.101 / 58. 36.101
libavcodec 60. 38.100 / 60. 38.100
libavformat 60. 20.100 / 60. 20.100
libavdevice 60. 4.100 / 60. 4.100
libavfilter 9. 17.100 / 9. 17.100
libswscale 7. 6.100 / 7. 6.100
libswresample 4. 13.100 / 4. 13.100
libpostproc 57. 4.100 / 57. 4.100
[cache @ 000002214a1865c0] Inner protocol failed to seekback end : -40
Last message repeated 1 times
[mp3 @ 000002214a185300] Failed to read frame size: Could not seek to 1026.
[cache @ 000002214a1865c0] Statistics, cache hits:0 cache misses:0
[in#0 @ 000002214a13b300] Error opening input: Invalid argument
Error opening input file cache:pipe:0.
Error opening input files: Invalid argument
File “C:\Users\Brahim\Desktop\Python\WoW\TTS\tts-test2.py”, line 24, in audio_consumer
audio_data = AudioSegment.from_file(BytesIO(chunk), format=“mp3”)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Brahim\Desktop\Python\WoW\TTS\tts-test2.py”, line 44, in main
await asyncio.gather(
File “C:\Users\Brahim\Desktop\Python\WoW\TTS\tts-test2.py”, line 52, in <module>
loop.run_until_complete(main())
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 4294967274

But, it worked with the first sentence : WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 1000000, ‘duration’: 3750000, ‘text’: ‘Hello’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 4875000, ‘duration’: 5625000, ‘text’: ‘World’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 19250000, ‘duration’: 1875000, ‘text’: ‘How’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 21250000, ‘duration’: 750000, ‘text’: ‘are’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 22125000, ‘duration’: 2125000, ‘text’: ‘you’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 24375000, ‘duration’: 4750000, ‘text’: ‘today’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 31375000, ‘duration’: 1500000, ‘text’: “i’m”}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 33000000, ‘duration’: 3750000, ‘text’: ‘super’}
WordBoundary: {‘type’: ‘WordBoundary’, ‘offset’: 36875000, ‘duration’: 4750000, ‘text’: ‘good’}

import asyncio
from io import BytesIO
import edge_tts
import pygame
from queue import Queue
from pydub import AudioSegment

TEXT = “Hello World! How are you today, i’m super good !”
VOICE = “en-GB-SoniaNeural”
CHUNK_SIZE = 1024 # Define the size of each audio chunk

# Initialize Pygame Mixer
pygame.mixer.init(44100, -16, 2, 64)

# Create a queue to hold audio chunks
audio_queue = Queue()

async def audio_consumer():
“”“Continuously play audio chunks as they become available.”“”
while True:
if not audio_queue.empty():
chunk = audio_queue.get()
# Convertir l’audio MP3 en données WAV en mémoire
audio_data = AudioSegment.from_file(BytesIO(chunk), format=“mp3”)
wav_bytes = BytesIO()
audio_data.export(wav_bytes, format=“wav”)
wav_bytes.seek(0) # Important pour remettre le curseur au début du BytesIO
# Jouer l’audio directement à partir de la mémoire
sound = pygame.mixer.Sound(file=wav_bytes)
sound.play()
await asyncio.sleep(0.01) # Sleep briefly to yield control to the event loop

async def amain():
“”“Main function to stream audio and put it into the queue.”“”
communicate = edge_tts.Communicate(TEXT, VOICE)
async for chunk in communicate.stream():
if chunk[“type”] == “audio”:
audio_queue.put(chunk[“data”])
elif chunk[“type”] == “WordBoundary”:
print(f"WordBoundary: {chunk}")

# Run both the consumer and the main coroutine concurrently
async def main():
await asyncio.gather(
amain(),
audio_consumer()
)

if name == “main”:
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(main())
finally:
loop.close()