如何在Linux中用Python播放ogg文件?
有人能提供一个简单的代码示例或者伪代码,教我怎么在Linux上用Python 2.7.1或者Python 3.1.3播放ogg格式的音频文件吗?另外,能不能列出需要安装的依赖项,比如从Synaptic包管理器或者其他地方获取的?
3 个回答
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
1
之前我尝试用Python写一个游戏原型,使用了pygame这个库。你可以在这个链接找到它:http://www.pygame.org/news.html。你应该可以在synaptic里找到它,安装的时候会自动安装所有需要的东西。如果ogg格式的音频不工作,你可能需要安装libvorbis,但你大概率已经安装过了。总之,最好的办法还是去看看pygame的文档。如果你做的不是游戏,可能需要用其他的库。不过我能建议的就是去搜索一下相关的信息。
3
如果你不介意依赖numpy的话,我的包audiolab工作得很好,并且开箱即用地支持oggfile,只要libsndfile本身支持它(如果你的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可以控制输出的数据类型、范围等等。你可以在pypi上找到发布的版本,代码可以在github上找到。