Reputation: 166
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()
Upvotes: 4
Views: 8776
Reputation: 8270
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.
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()
Upvotes: 5