speed up mp3

Ivo Woltring TheDolphin at ivonet.nl
Tue Oct 26 21:16:31 CEST 2004


Dear Pythoneers,

I have a blind friend who listens a lot to audio books. A lot of the
material available today is in mp3 format.
For ages i have been looking for a mp3-player with speed control and haven't
found any.
Next option: write one yourself.
And I have ;-). with the PyMedia module.
It is an only sleightly adjusted version of one of the examples.

Now the Question:
Is it possible to re-Encode mp3 files with this faster speed as to make them
portable to e.g. Pocket PC (without python interpreter) or standard MP3
player (hardware)?

The files have to play at about 1.5 times the normal speed.

I tried it with PyMedia but I only get errors. Any help would be
appreciated.

#=============CODE==================
#! /bin/env python

import sys,os

def aplayer( name, codec='mp3', card=0, rate=1 ):
  import pymedia.audio.acodec as acodec
  import pymedia.audio.sound as sound
  import time
  try:  cod=os.path.splitext(name)[ 1 ].lower()[1:]
  except: cod=''
  if cod: codec = cod
  dec= acodec.Decoder( codec )
  snds= sound.getODevices()
  if card not in range( len( snds ) ):
    raise 'Cannot play sound to non existent device %d out of %d' % ( card+
1, len( snds ) )
  f= open( name, 'rb' )
  snd= None
  s= f.read( 50000 )
  while len( s ):
    r= dec.decode( s )
    if snd== None:
      print '-'*75
      print 'Playing: %s' % os.path.basename(name)
      print 'Opening sound with %d channels -> %s' % ( r.channels, snds[
card ][ 'name' ] )
      print 'Orig sample rate: %s' % r.sample_rate
      if rate<0: sr=r.sample_rate/abs(rate)
      else: sr=r.sample_rate*rate
      sr = int(sr)
      print 'Play sample rate: %s' % sr
      snd= sound.Output( sr, r.channels, sound.AFMT_S16_LE, card )
    if r:
      snd.play( r.data )

    s= f.read( 512 )

  while snd.isPlaying():
    time.sleep( .05 )

# --------------------------------------------------------------------------
--------
# Play any compressed audio file
# http://pymedia.org/
if len( sys.argv )< 2 or len( sys.argv )> 3:
  print "Usage: aplayer <filename> [ rate ]"
else:
  i= 0
  if len( sys.argv )== 3:
    i= eval(sys.argv[ 2 ])
  else: i=1
  aplayer( sys.argv[ 1 ], rate=i )
#==================END CODE==============





More information about the Python-list mailing list