Monday, 24 August 2020

How to convert a blob file to a specific format?

I am building a web application with ReactJS and Django framework.

In this web application, there is a part where I record an audio file and send it to the backend to save it.

This is the blob data from ReactJS that I send:

Blob {
size: 29535, 
type: "audio/wav; codecs=0"
}

And this is the code I am using in the backend:

@api_view(['POST'])
@csrf_exempt
def AudioModel(request):
    try:
        audio = request.FILES.get('audio')
    except KeyError:
        return Response({'audio': ['no audio ?']}, status=HTTP_400_BAD_REQUEST)

    destination = open('audio_name.wav', 'wb')
    for chunk in audio.chunks():
        destination.write(chunk)
    destination.close()  # closing the file

    return Response("Done!", status=HTTP_200_OK) 

When I play the file I saved, it plays some sound but it crashes when it achieves the end.

This problem makes me look for some information about the file I saved (extension,...).

For this reason I used fleep library:

import fleep

with open("audio_name.wav", "rb") as file:
    info = fleep.get(file.read(128))

print(info.type)
print(info.extension)
print(info.mime)

OUTPUT:

['video']
['webm']
['video/webm']

But getting video in output!

  • Am I doing something wrong?
  • How can I fix this issue?
  • Is there anything I can use to save my file in the desired format?

Any help is appreciated.



from How to convert a blob file to a specific format?

No comments:

Post a Comment