I want to take an OGA file within a binary stream and convert it into mp3 using also another stream. I'm getting a permissions error even with running VSCode as administrator. This is my code:
from pydub import AudioSegment
AudioSegment.converter = "C:\\ProgramData\\chocolatey\\lib\\ffmpeg\\tools\\ffmpeg\\bin\\ffmpeg.exe"
input_stream = io.BytesIO()
input_stream.seek(0)
await new_file.download_to_memory(input_stream)
# Create an audio segment from the binary stream
audio = AudioSegment.from_file(input_stream, format='ogg')
# Create an output stream for the MP3 data
output_stream = io.BytesIO()
# Export the audio to MP3 using ffmpeg and write the output to the stream
audio.export(output_stream, format='mp3', codec='libmp3lame')
# Get the MP3 data from the output stream
mp3_data = output_stream.getvalue()
Error message:
File "C:\ProgramData\Anaconda3\envs\chatbot\lib\site-packages\telegram\ext\_application.py", line 1124, in process_update
await coroutine
File "C:\ProgramData\Anaconda3\envs\chatbot\lib\site-packages\telegram\ext\_handler.py", line 141, in handle_update
return await self.callback(update, context)
File "c:\Users\jdbol\OneDrive\Desktop\testbots\echobot.py", line 80, in voice_to_text
audio = AudioSegment.from_file(input_stream, format='ogg')
File "C:\ProgramData\Anaconda3\envs\chatbot\lib\site-packages\pydub\audio_segment.py", line 773, in from_file
raise CouldntDecodeError(
pydub.exceptions.CouldntDecodeError: Decoding failed. ffmpeg returned error code: 1
Output from ffmpeg/avlib:
ffmpeg version 6.0-essentials_build-www.gyan.dev Copyright (c) 2000-2023 the FFmpeg developers
built with gcc 12.2.0 (Rev10, Built by MSYS2 project)
configuration: --enable-gpl --enable-version3 --enable-static --disable-w32threads --disable-autodetect --enable-fontconfig --enable-iconv --enable-gnutls --enable-libxml2 --enable-gmp --enable-lzma --enable-zlib --enable-libsrt --enable-libssh --enable-libzmq --enable-avisynth --enable-sdl2 --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxvid --enable-libaom --enable-libopenjpeg --enable-libvpx --enable-libass --enable-libfreetype --enable-libfribidi --enable-libvidstab --enable-libvmaf --enable-libzimg --enable-amf --enable-cuda-llvm --enable-cuvid --enable-ffnvcodec --enable-nvdec --enable-nvenc --enable-d3d11va --enable-dxva2 --enable-libmfx --enable-libgme --enable-libopenmpt --enable-libopencore-amrwb --enable-libmp3lame --enable-libtheora --enable-libvo-amrwbenc --enable-libgsm --enable-libopencore-amrnb --enable-libopus --enable-libspeex --enable-libvorbis --enable-librubberband
libavutil 58. 2.100 / 58. 2.100
libavcodec 60. 3.100 / 60. 3.100
libavformat 60. 3.100 / 60. 3.100
libavdevice 60. 1.100 / 60. 1.100
libavfilter 9. 3.100 / 9. 3.100
libswscale 7. 1.100 / 7. 1.100
libswresample 4. 10.100 / 4. 10.100
libpostproc 57. 1.100 / 57. 1.100
fd:: End of file
I ran the command in a terminal and the file converted without an issue:
ffmpeg -i .\file_12.oga output.mp3
- I'm not sure if the .exe file must be included into the path. When I don't do it, What I get is a permissions error.
- What other codecs can be used here?
- Is it possible to use oga files? I tried to declare this but I got an 'Unknown input format: 'oga' message
(audio = AudioSegment.from_file(input_stream, format='oga')
)
Thanks!
UPDATE: I created a more simple version that is not using a binary stream and worked like a charm, so we know for sure that something is happening with the BytesIO object
async def voice_to_text(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
# Get the absolute path of the script
filename = 'file_9.oga'
script_dir = os.path.dirname(os.path.abspath(__file__))
# Construct the file paths for input and output files
input_file_path = os.path.join(script_dir, filename)
output_file_path = os.path.join(script_dir, os.path.splitext(filename)[0] + ".mp3")
# Load the OGA audio file
audio = AudioSegment.from_file(input_file_path, format='ogg')
# Export the audio to MP3 format
audio.export(output_file_path, format='mp3')
print("Conversion complete. MP3 file saved as:", output_file_path)
UPDATE 2: It seems like await new_file.download_to_memory(input_stream)
is the problematic line. I tried to save the file and its corrupt. Not sure how to use this method then.
https://docs.python-telegram-bot.org/en/stable/telegram.file.html#telegram.File.download_to_memory