I'm trying to create a Flask app that transcribes mp3 files using GCP's speech-to-text and saves the results to Cloud Storage (GCS). (Upload the mp3 file from Vue.js to Flask) In the process, I want to convert the mp3 file selected by the user to a flac file, but I do not want to save it locally and throw it to speech-to-text or save it to GCS.
Any module can be used, but I want to save the audio file converted to GCS without going through the local.
I tried to use pydub, which is often used to convert audio files, but the relative path was the only argument that could be taken when selecting the pre-conversion file and where to save the post-conversion file. I cannot convert mp3 files received by Flask to flac files and save them to GCS.
Even if you can't save to GCS, you can save the converted file to variables. I couldn't do that either.
from pydub import AudioSegment
# (1)I can convert an audio file at the local.
sound = AudioSegment.from_mp3("example.mp3")
sound.export("example.flac", format="flac")
# (2)I CANNOT pass GCS URL as an argument
sound = AudioSegment.from_mp3("https://storage.googleapis.com/<bucket-name>/example.mp3")
sound.export("https://storage.googleapis.com/<bucket-name>/example.flac", format="flac")
# (3)I CANNOT written to a variable
sound = sound.export(format="flac")
sound.export("example.flac")
Of the above source code
(1) Shows the expected behavior, and the converted example.flac is saved in the current directory
(2) FileNotFoundError: [Errno 2] No such file or directory: 'https://storage.googleapis.com//npl_speech_2.mp3'
(3) AttributeError: '_io.BufferedRandom' object has no attribute 'export'
In the end, I want to use AWS Lambda, so I want to convert files without going through local.