简体   繁体   English

如何将mp3转换为ogg python

[英]How to convert mp3 to ogg python

I need to convert bytes mp3 data to bytes ogg. 我需要将mp3字节数据转换为ogg字节。 How a can do it in python? 如何在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 解决方案1:在线转换

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. 它具有自己的API ,并且支持直接从URL进行转换,因此您甚至不需要将文件读入内存。

Solution 2: Convert locally with temp file 解决方案2:使用临时文件进行本地转换

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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