Иван
Иван

Reputation: 166

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()

Upvotes: 4

Views: 8776

Answers (1)

Alderven
Alderven

Reputation: 8270

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()

Upvotes: 5

Related Questions