Today I am going to tell you about a python video/audio python processing module. It’s called PyMedia.
- official url: http://pymedia.org/
After some googling you’ll be able to install it (it’s not available for apt-get but it’s not very hard!)
pymedia can be used to parse, demultiplex, multiplex, decode and encode many of it’s supported formats, such as wav, mp3, ogg….etc.
Here’s some code I edited from the tutorial to show you the main idea for audio:
import pymedia.muxer as muxer, pymedia.audio.sound as sound, \
pymedia.audio.acodec as acodec
class playsong:
def__init__(self, sName):
self.sName = sName
splt = str.split(self.sName, ‘.’)[-1].lower()
dm = muxer.Demuxer(splt)
print ‘format = %s’ % splt
f = open(self.sName, ‘rb’)
s = f.read(8192)
frames = dm.parse(s)
song_data = dm.getHeaderInfo()
for info_type in song_data:
print ‘%-10s :%s’ % (info_type, song_data[info_type])
if frames:
for fr in frames:
dec = acodec.Decoder(dm.streams[fr[0]])
r = dec.decode(s)
print self.sName, ‘playing…’
snd = sound.Output(r.sample_rate, r.channels, sound.AFMT_S16_LE)
while len(s):
if r:
snd.play(r.data)
s = f.read(512)
r = dec.decode(s)
if __name__ == “__main__”:
p = playsong(“INSERT FILENAME HERE”)
This is it! With this you can play your music.
All you need to do is insert a filename instead of the (quite obvious) INSERT FILENAME HERE place. The first part of the code figures out it’s extension (splt) then it passes it through some parameters and some of the data gets parsed by the decoder. Then, when it finally has enough data a snd parameter gets defined and a final loop is created which reads and plays the whole file.
As you can see, pymedia is capable of all the needed audio processing, and video too (I might put something up about that later)
I will definitely work with it a bit, it would be nice to combine it with a GUI such as pyGTK to form a player of some sorts
I’ll keep you updated on any new/interesting usages of pymedia/interesting topics.