我有一个从http请求上传的aac文件。如何将其转换为wav文件,而不是先将其写入磁盘上的aac文件。
这是我的代码:
with open(filepath, 'wb') as up:
up.write(file_metas[0]['body'])
cf = AudioSegment.from_file(filepath)
cf.export(wav_filepath, format="wav")
如何在不将file_metas [0] ['body']写入文件的情况下获取cf。
答案 0 :(得分:1)
我自己找到了解决方案。我可以使用cStringIO模型。它是一个与文件具有相同接口的对象,但其数据在内存中。
output = cStringIO.StringIO()
output.write(file_metas[0]['body'])
output.seek(0)
cf = AudioSegment.from_file(output)
cf.export(wav_filepath, format="wav")