How can I convert json files to MP3?
c# - Is there a way to store a reference to an MP3 file in a JSON file? - Stack Overflow
How do I store an audio file in an Json Object? If I can.
Microphone audio file as a json, how to convert it in to a audio file?
Does converting JSON to MP3 change the resolution?
Why is my converted MP3 file larger than the original JSON?
Is batch conversion available?
I can't find a website that works right, can anyone help me?
There are a few options:
- Store the absolute or relative path to your mp3 file in your json file. This will have the risk that the path is incorrect, is someone copies only the json file, or renames the mp3 file. To do this just add a
string musicPathfield to your class. - Store the mp3 file inside your json as a base64 encoded string. This will make your json file harder to read, and it will waste some space due to the inefficient encoding.
- Store your json and mp3 file inside a zip-archive. Store the entry name of your mp3 file inside your json file. This ensures that the user only sees a single file, but makes it a bit more cumbersome to create or edit files by hand. You can mark the mp3 file as "store" to avoid the compression overhead.
In all cases you will need to know how read and use the stream representing the mp3 data.
U can convert mp3 file to Base64String and avoiding loosing it :
var audioBytes = File.ReadAllBytes("PATHTOAudio");
var base64String = Convert.ToBase64String(audioBytes);
use This method to To play mp3 File :
static void PlaySoud(string base64String)
{
var audioBuffer = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(audioBuffer))
{
var player = new System.Media.SoundPlayer(ms);
player.Play();
}
}
HI Everyone!
I am working on learning some audio related python coding where I am translating audio from different languages to English. Locally, it's working perfectly, I have some french audio as input and it is translating perfectly.
The issue I am facing is when creating an API, I want the API to only accept the audio file instead of a file path and to return the english translated audio file. I am using FastAPI. Along with the english translated audio file, I am also sending the french text and english translated text as strings.
I am trying to return the data all in one json object as I was told that's efficient and it's easier to retrieve the response from the json object. Where I'm facing the issue is when I try to send the audio file in the json object. The strings are passing along perfectly but the audio file is not.
Things that I have tried:
I have tried encoding the audio file using base64 and store it in the json object before sending the response after that I decoded the file but was unable to convert it back into an audio file! What sucks about this option is that I want to try and use the API with some other platform like android application or web.
My Code for my API: (I am using a hugging face model for the audio translation to english!) The commented sections is what I have tried which worked but not in functionality for me.
speech = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
# sf.write("outputAudio.mp3", speech.numpy(), samplerate=16000)
# Encode audio data as base64
# audio_base64 = base64.b64encode(speech.numpy()).decode('utf-8')
response_object = {
"audio_file": speech.numpy(),
"translated_text": str(translated_text),
"recognized_text": str(recognized_text)
}
return response_objectHere's how I am running my API:
file_path = r'French Audio 2.wav'
url = "http://localhost:8000/recognize-and-translate/"
files = {'file': open(file_path, 'rb')}
language = {'language': 'French'}
response = requests.post(url, files=files, data=language)
response_json = response.json()
if response.status_code == 200:
audio_file_base64 = base64.b64decode(response_json.get('audio_file'))
translated_text = response_json.get('translated_text')
recognized_text = response_json.get('recognized_text')
# print(type(audio_file_base64))
# audio = np.load(audio_file_base64)
# sf.write("AudioAfterAPI.mp3", audio)
# print("Audio File Base64:", audio_file_base64)
print("Translated Text:", translated_text)
print("Recognized Text:", recognized_text)
else:
print(f"Error: {response.status_code} - {response.json()['detail']}"I have been stuck at this for too long :(
Any help would be appreciated!
Thanks!