你如何在Linux中用Python播放ogg文件?

时间:2011-01-23 07:48:54

标签: python python-3.x ogg

有人可以提供一个简短的代码或伪代码示例,说明如何在Linux中使用Python 2.7.1或Python 3.1.3播放ogg文件(以及来自Synaptic Package Manager或其他地方的任何依赖项列表)?

3 个答案:

答案 0 :(得分:3)

如果你不介意取决于numpy,我的包audiolab工作得很好,只要libsndfile本身支持它就支持oggfile开箱即用(如果你的版本已经足够,它应该在linux上):

# the dependencies
sudo apt-get install libsndfile-dev python-numpy cython python-setuptools
# install audiolab
cd audiolab-0.11 && python setup.py install --user

基本API很简单:

from scikits.audiolab.pysndfile.matapi import oggread
data, fs, enc = oggread("myfile.ogg")

还可以使用更完整的API来控制输出dtype,range等等。您可以在pypi上找到发布,在github上找到代码

答案 1 :(得分:1)

前段时间我曾尝试在python中编写一些游戏原型,并使用了pygame。 http://www.pygame.org/news.html 你应该能够在synaptic中找到它,它应该安装所有需要的依赖项,如果ogg不起作用你可能需要libvorbis,但你很可能已经安装了它。无论如何,最好的办法是阅读pygame。如果它不是你制作的游戏,你可能需要另一个图书馆。但是我所能建议的只是尝试搜索。

答案 2 :(得分:0)

我使用py-gstreamer播放ogg文件,代码如下

import sys, os

##FOR UBUNTU 13.04 onwards
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, Gst, Gtk
##end
GObject.threads_init()
Gst.init(None)

uri = "http://blender-podcast.org/episodes/Blender_Podcast_Episode_028.ogg"
#pipeline = Gst.Pipeline()
#delay = Gst.ElementFactory.make("audiodelay","delay")
player = Gst.ElementFactory.make("playbin", "player")
fakesink = Gst.ElementFactory.make("fakesink", "fakesink")
# pipeline.add(player)
# pipeline.add(fakesink)

player.set_property('uri', uri)
player.set_property("video-sink", fakesink)
player.set_state(Gst.State.PLAYING)
Gtk.main()

安装

sudo apt-get install libgstreamer1.0-0 libgstreamer1.0-0-dbg libgstreamer1.0-dev liborc-0.4-0 liborc-0.4-0-dbg liborc-0.4-dev liborc-0.4-doc gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gstreamer1.0-alsa gstreamer1.0-doc gstreamer1.0-omx gstreamer1.0-plugins-bad gstreamer1.0-plugins-bad-dbg gstreamer1.0-plugins-bad-doc gstreamer1.0-plugins-base gstreamer1.0-plugins-base-apps gstreamer1.0-plugins-base-dbg gstreamer1.0-plugins-base-doc gstreamer1.0-plugins-good gstreamer1.0-plugins-good-dbg gstreamer1.0-plugins-good-doc gstreamer1.0-plugins-ugly gstreamer1.0-plugins-ugly-dbg gstreamer1.0-plugins-ugly-doc gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-x libgstreamer-plugins-bad1.0-0 libgstreamer-plugins-bad1.0-dev libgstreamer-plugins-base1.0-0 libgstreamer-plugins-base1.0-dev