4

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()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Иван
  • 166
  • 1
  • 3

1 Answers1

4

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()
Alderven
  • 7,569
  • 5
  • 26
  • 38
  • I thought about it, but urls attached to my IP address, so I can't upload it(( – Иван Jan 10 '19 at 19:55
  • What do you mean ”urls attached to IP”? Are your URLs local and not available through the internet? – Alderven Jan 10 '19 at 20:06
  • yes, so only I can download it, and i don't want to load audio to the internet again, so i searching for converter-library – Иван Jan 11 '19 at 06:45
  • I've updated my answer. See solution #2. Your mp3 file will be storing in temp file and will be removed after conversion completed. – Alderven Jan 11 '19 at 07:15