How to export an "IPython.lib.display.Audio" file as a mp3 or wav file?
Asked Answered
G

3

8

I have generated a music file for the first time. The music file type is "IPython.lib.display.Audio". I wanted to export it from colab to my local drive.I couldn't find any solution from google. Please help.

Glossary answered 25/12, 2019 at 22:41 Comment(1)
Did my answer solve our problem?Bloodstock
B
8

The wave file content is stored in the data field, you may write the content to a file like this:

with open('/tmp/test.wav', 'wb') as f:
    f.write(audio.data)
Bloodstock answered 25/12, 2019 at 23:20 Comment(1)
But this saves to the remote server in which the python is running on. How to download using the browser?Grommet
A
2

You can right click on the audio player and save the audio. Example

Appassionato answered 31/8, 2023 at 23:36 Comment(1)
in my case, this works with google colab, not jupyter notebook; but anyway, thanks; I ran code in jupyter notebook to display audio, then upload file to google colab for downloadingGuano
H
1
soundfile.write(output_filename, audio_data, sample_rate) 

output_filename: The output filename or a path to the output file
audio_data: The <class 'IPython.lib.display.Audio'> object
sample_rate: Based on your need.

https://pysoundfile.readthedocs.io/en/latest/#read-write-functions also gives a brief example. This should work whether you use colab/jupyter notebook or not.

Handshaker answered 3/9, 2022 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.