Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert mp3 to ogg in Python without writing to disk?

I need to convert bytes mp3 data to bytes ogg. How can I do it in Python? I've seen many examples to convert it from a file, but I don't want to write it to disk.

from urllib.request import urlopen
bytes = urlopen("https://url.com/file.mp3").read()
like image 814
Иван Avatar asked Oct 28 '25 05:10

Иван


1 Answers

Solution 1: Convert online

You can use online-convert service. It has it's own API and it supports conversion directly from URL, so you don't even need to read the file into memory.

Solution 2: Convert locally with temp file

import tempfile
from pydub import AudioSegment
from urllib.request import urlopen

data = urlopen('https://sample-videos.com/audio/mp3/crowd-cheering.mp3').read()
f = tempfile.NamedTemporaryFile(delete=False)
f.write(data)
AudioSegment.from_mp3(f.name).export('result.ogg', format='ogg')
f.close()
like image 60
Alderven Avatar answered Oct 31 '25 02:10

Alderven



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!