Audio levels.py

From ActiveArchives

Jump to: navigation, search

Script to dump the audio levels of an audio file.

Usage:

python audio_levels.py path/to/myaudio.mp3
import gst, pygst, struct, sys
 
(pmin, pmax) = (None, None)
 
# gboolean message_handler (GstBus * bus, GstMessage * message, gpointer data)
def listener_on_message (bus, message, data):
    global pmin, pmax
    s = message.structure
 
    if s and s.get_name() == "level":
        rms = s['rms']
        # also available, but not used here
        # peak = s['peak']; decay = s['decay']
        # numbers are in pairs (left, right) -- we use just the first one (left?)
        p = rms[0]
        # check against/update min and max
        if (pmin==None or p<pmin): pmin = p
        if (pmax==None or p>pmax): pmax = p
        print p
 
    elif message.type == gst.MESSAGE_EOS:
 
        # print "EOS"
        listener.set_state(gst.STATE_NULL)
        mainloop.quit()
 
 
    return True
 
"""
gst-launch-0.10 filesrc location=test.mp3 ! decodebin ! level ! fakesink
"""
 
src = sys.argv[1]
listener_desc = 'filesrc location=%s ! decodebin ! level ! fakesink' % src
listener = gst.parse_launch(listener_desc)
listener.get_bus().add_watch(listener_on_message, None)
 
import gobject
mainloop = gobject.MainLoop()
listener.set_state(gst.STATE_PLAYING)
 
try:
    mainloop.run()
except: # an interruption from Ctrl-C
    print "stopping"
 
listener.set_state(gst.STATE_NULL)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox