[Tutor] mp3 range violation reportage.

Tesla Coil tescoil@irtc.net
Tue, 29 May 2001 18:33:28 -0500


What I crufted up--looks to work with both 1.5.2 & 2.0.
Turning this over to the Useless repository.

A "does user have mpg123 available" test might be nice
--not certain how to go about that.  If a file to be
tested is incorrectly named or doesn't exist, it simply
doesn't print any results on that one--could stand to
return something more descriptive there, just haven't
worked that out yet.  And whatever else critique finds...

#!/usr/bin/env python
""" clippage.py -- uses mpg123 to produce a report of
    total samples clipped from an single mp3 or those
    listed in an .m3u playlist.

    usage: 
    $ clippage.py track.mp3
    $ clippage.py playlist.m3u """

__author__ = 'Tesla Coil <tescoil@irtc.net>'
__date__ = 'May 2001'
__version__ = '$Revision: 0.1 $'

from string import atoi
from sys import argv
from popen2 import popen3
from operator import add

def modusoperandi(extension):
    # Playlist or single mp3?
    command = 'mpg123 -t -c '
    if extension[-3:] == 'm3u':
        command = command+'-@ '+extension
    elif extension[-3:] == 'mp3':
        command = command+extension
    else: print ' Input filename requires .mp3 or .m3u extension'
    return command

def runtest(command):
    # The excruciatingly slow part.
    allofit = popen3(command)
    someofit = allofit[2]
    rawreport = someofit.readlines()
    for obj in allofit:
    	obj.close()
    return rawreport

def slicedown(rawreport):
    # Edit mpg123's output somewhat.
    divbegin=[]
    divend=[]
    for x in range(len(rawreport)):
        if rawreport[x][0:7]=='Playing':
            divbegin.append(x)
        if rawreport[x][-10:-2]=='finished':
     	    divend.append(x)
    firstedit = []
    for x in range(len(divbegin)):
        firstedit.append(rawreport[divbegin[x]:divend[x]])
    return firstedit

def gettracks(firstedit):   
    # Get list of the mp3s tested.
    tracklist = []
    for x in range(len(firstedit)):
        tracklist.append(firstedit[x][0][25:-1])
    return tracklist

def clipsums(firstedit):
    # Locate and total test results.
    trackclips = [0]
    allclips = []
    for x in range(len(firstedit)):
        for y in range(len(firstedit[x])):
            if firstedit[x][y][-16:-1]=='samples clipped':
	        trackclips.append(atoi(firstedit[x][y][:-17]))	    
        allclips.append(trackclips)
        trackclips = [0]
    for x in range(len(allclips)):	   
        allclips[x]=reduce(add, allclips[x])
    return allclips

def reportout(tracklist, allclips):
    # Generate the final report.
    for x in range(len(tracklist)):
        print tracklist[x], allclips[x], 'samples clipped.'

if __name__ == '__main__':
    commandline = modusoperandi(argv[1])
    mpgout = runtest(commandline)
    relevantdata = slicedown(mpgout)
    tracks = gettracks(relevantdata)
    clips = clipsums(relevantdata)
    reportout(tracks, clips)