Exception has occurred: CouldntDecodeError Decoding failed. ffmpeg returned error code:…
- User
Exception has occurred: CouldntDecodeError Decoding failed. ffmpeg returned error code: 4294967274
Output from ffmpeg/avlib:
ffmpeg version N-113433-gbab7f91c36-20240127 Copyright (c) 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 loop.run_until_complete(main()) pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 4294967274
Output from ffmpeg/avlib:
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()
- Assistant
It appears that you are encountering an issue while decoding MP3 audio using the
pydublibrary, which in turn usesffmpegfor the decoding. The error you're getting fromffmpegindicates that it failed to read and seek in the input stream.Here are a few suggestions to help troubleshoot and potentially fix the problem:
-
Check
ffmpegInstallation: Make sureffmpegis correctly installed and accessible in your system's PATH environment variable. You can test this by runningffmpeg -versionin your command line or terminal. -
File Permissions: Ensure that the file you're trying to open has the correct permissions and can be read by your Python script.
-
Update Dependencies: Outdated packages or
ffmpegbinaries might cause such issues. Make sure to updatepydub,edge_tts, and yourffmpegto the latest versions. -
Input Stream Integrity: Since it seems that you're streaming the audio from a text-to-speech service, ensure that the data stream is not corrupted or incomplete. Try to save a chunk of data to a file before decoding it with
pydubto check whether the data is valid MP3 data. -
Try Different Form of Input: Instead of using
BytesIOas the source, try saving a chunk to a temporary file and then usefrom_filewith the path to that file. If this works, the problem might be with how theBytesIOstream is being handled. -
Encoding and Container Issues: There could be an issue related to the MP3 encoding or container that's producing unsuitable data for
ffmpegto decode. This could be due to codec support or stream inconsistencies.
Here's how you might save a chunk to a file for testing:
import tempfile def save_chunk_to_file(chunk): # Save the chunk to a temporary file with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3') as tmp: tmp.write(chunk) tmp.flush() # Ensure the data is written to disk tmp.seek(0) # Reset file pointer return tmp.name # Return the file path # Inside your audio_consumer loop: if not audio_queue.empty(): chunk = audio_queue.get() tmp_file_path = save_chunk_to_file(chunk) audio_data = AudioSegment.from_file(tmp_file_path, format="mp3") # Your remaining processing logic...- Debugging Output: Add more detailed logging or print statements just before the point where the error occurs to inspect the chunk data, file paths, and any other relevant information that could help in identifying the issue.
If none of these steps resolve the issue, consider isolating the problem further by creating a minimal example that only tries to decode one specific chunk, and see if that consistently fails. It could also be helpful to open an issue on the
pydubGitHub repository or seek assistance on programming communities like Stack Overflow, providing the details you've collected and the steps you've already attempted. -
- User
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'}
- Assistant
If the script worked for the first sentence and you are receiving word boundary information, this means the text-to-speech and streaming are functioning as expected for that part. However, the decoding error suggests an issue occurs when attempting to process and play one of the chunks of audio data.
Since the problem arises after successfully processing some input, it could be a transitory issue related to a specific chunk of audio data, or a resource contention problem where system resources are not keeping up with the audio processing demands.
Here are additional steps to narrow down the issue:
-
Isolate the Failing Chunk: Try to determine if it's a specific chunk that's causing the problem by adding logging or print statements that give you an indication of which chunk is failing. If it's always the same point in the sequence, there may be an issue with the data received for that specific chunk.
-
Resource Utilization: Monitor the system's resource utilization (CPU, memory, disk I/O) to see if there are any spikes that correlate with the failure. It’s possible that resource limitations could lead to decoding problems.
-
Concurrency Issues: Since you are using asynchronous code (
asyncio), concurrency might result in contention for resources. You might want to experiment with adjusting theCHUNK_SIZEor the sleep duration inaudio_consumerto see if giving more time for each chunk to be processed helps. -
Examine the Error Code: The error
4294967274is a large unsigned number, which in some cases could be the result of interpreting a signed integer as unsigned. If it is interpreted as a signed 32-bit integer, it would correspond to-22, which in LinuxerrnoisEINVAL(Invalid argument). This might indicate that the data passed to ffmpeg isn't being understood as valid MP3 data for some reason. This could be a clue to look into the integrity of the data being streamed. -
Troubleshoot with FFmpeg Directly: If you’re able to capture a problematic audio chunk, try running
ffmpegdirectly with that chunk to see if you can replicate the error outside of your script. This could give you more insight into what is going wrong. -
Error Handling: Add error handling around the decoding operation. This way, if a single chunk fails, you can log the error, possibly save the offending chunk for later analysis, and continue processing the rest of the audio:
try: audio_data = AudioSegment.from_file(BytesIO(chunk), format="mp3") # your processing logic ... except Exception as e: print(f"Error processing chunk: {e}") # (Optionally) Save chunk to file for inspection # continue to the next chunk instead of crashing
If these steps don’t resolve the issue, consider asking on appropriate forums or reaching out to the maintainers of the libraries in use with specific details of your findings so far.
-