how pydub loads video data from memory

333 views Asked by At

I have a aac file uploaded from http request.How can I transfom it into a wav file without writing it into a aac file on disk at first.

Here is my code:

        with open(filepath, 'wb') as up:
            up.write(file_metas[0]['body'])

        cf = AudioSegment.from_file(filepath)
        cf.export(wav_filepath, format="wav")

How can i get cf without writing file_metas[0]['body'] into a file.

1

There are 1 answers

0
moon On BEST ANSWER

I find a solution myself.I can use cStringIO model. It is a object that has the same interface with file, but its data is in memory.

    output = cStringIO.StringIO()
    output.write(file_metas[0]['body'])
    output.seek(0)
    cf = AudioSegment.from_file(output)
    cf.export(wav_filepath, format="wav")