简体   繁体   中英

How do you play ogg files in Python in Linux?

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

If you don't mind depending on numpy, my package audiolab works pretty well and supports oggfile out of the box as long as libsndfile itself supports it (it should on linux if you version is recent enough):

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

The basic API is straightforward:

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

A more complete API for controlling output dtype, range, etc... is also available. You can found releases on pypi, and the code on github

Sometime ago I tried to write some game prototype in python and i used pygame. http://www.pygame.org/news.html You should be able to find it in synaptic, and it should install all needed dependencies, if ogg would not work you would probably need libvorbis, but you most likely have it installed already. Anyway, probably best thing to do is to read pygame. Otho if it's not a game that your making you might need another library. But then all I can suggest is try searching.

I used py-gstreamer to play ogg files with following code

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

Installation

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

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