简体   繁体   中英

How to convert mp3 to ogg python

I need to convert bytes mp3 data to bytes ogg. How a can 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM