examples/: Closes #362290 and #362272

Original commit message from CVS:
* examples/audioconcat.py:
* examples/cp.py:
Port to 0.10 by Jason Gerard DeRose <jderose@jasonderose.org>
Closes #362290 and #362272
* examples/bps.py:
Indentation fixes by Jason Gerard DeRose <jderose@jasonderose.org>
Closes #362011
Also small fix for Usage string
This commit is contained in:
Edward Hervey 2006-10-20 09:27:43 +00:00
parent 6d8f3a459f
commit 71f58a44b4
4 changed files with 198 additions and 110 deletions

View file

@ -1,3 +1,14 @@
2006-10-20 Edward Hervey <edward@fluendo.com>
* examples/audioconcat.py:
* examples/cp.py:
Port to 0.10 by Jason Gerard DeRose <jderose@jasonderose.org>
Closes #362290 and #362272
* examples/bps.py:
Indentation fixes by Jason Gerard DeRose <jderose@jasonderose.org>
Closes #362011
Also small fix for Usage string
2006-10-16 Tim-Philipp Müller <tim at centricular dot net> 2006-10-16 Tim-Philipp Müller <tim at centricular dot net>
* gst/gst.defs: * gst/gst.defs:

View file

@ -2,129 +2,191 @@
# -*- Mode: Python -*- # -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4 # vi:si:et:sw=4:sts=4:ts=4
# audio concat tool # audioconcat.py - Concatenates multiple audio files to single ogg/vorbis file
# takes in one or more audio files and creates one audio file of the combination
# Uses the gnonlin elements (http://gnonlin.sf.net/) # Uses the gnonlin elements (http://gnonlin.sf.net/)
# Copyright (C) 2005 Edward Hervey <edward@fluendo.com>
# 2006 Jason Gerard DeRose <jderose@jasonderose.org>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
import os
import sys import sys
import gobject import gobject
gobject.threads_init()
import pygst
pygst.require('0.10')
import gst import gst
from gst.extend.discoverer import Discoverer
from gstfile import Discoverer, time_to_string
class AudioSource(gst.Bin):
"""A bin for audio sources with proper audio converters"""
def __init__(self, filename, caps): class AudioDec(gst.Bin):
gst.Bin.__init__(self) '''Decodes audio file, outputs at specified caps'''
self.filename = filename
self.outcaps = caps
self.filesrc = gst.element_factory_make("filesrc") def __init__(self, location, caps):
self.filesrc.set_property("location", self.filename) gst.Bin.__init__(self)
self.dbin = gst.element_factory_make("decodebin")
self.ident = gst.element_factory_make("identity")
self.audioconvert = gst.element_factory_make("audioconvert")
self.audioscale = gst.element_factory_make("audioscale")
self.add_many(self.filesrc, self.dbin, self.ident, # Create elements
self.audioconvert, self.audioscale) src = gst.element_factory_make('filesrc')
self.filesrc.link(self.dbin) dec = gst.element_factory_make('decodebin')
self.audioconvert.link(self.audioscale) conv = gst.element_factory_make('audioconvert')
self.audioscale.link(self.ident, caps) rsmpl = gst.element_factory_make('audioresample')
self.add_ghost_pad(self.ident.get_pad("src"), "src") ident = gst.element_factory_make('identity')
self.dbin.connect("new-decoded-pad", self._new_decoded_pad_cb) # Set 'location' property on filesrc
src.set_property('location', location)
def _new_decoded_pad_cb(self, dbin, pad, is_last): # Connect handler for 'new-decoded-pad' signal
if not "audio" in pad.get_caps().to_string(): dec.connect('new-decoded-pad', self.__on_new_decoded_pad)
return
pad.link(self.audioconvert.get_pad("sink"))
gobject.type_register(AudioSource) # Add elements to bin
self.add(src, dec, conv, rsmpl, ident)
class AudioConcat(gst.Thread): # Link *some* elements
"""A Gstreamer thread that concatenates a series of audio files to another audio file""" # This is completed in self.__on_new_decoded_pad()
src.link(dec)
conv.link(rsmpl)
rsmpl.link(ident, caps)
def __init__(self, infiles, outfile, audioenc="rawvorbisenc", muxer="oggmux"): # Reference used in self.__on_new_decoded_pad()
gst.Thread.__init__(self) self.__apad = conv.get_pad('sink')
self.infiles = infiles
self.outfile = outfile
self.audioenc = gst.element_factory_make(audioenc)
if not self.audioenc:
raise NameError, str(audioenc + " audio encoder is not available")
self.muxer = gst.element_factory_make(muxer)
if not self.muxer:
raise NameError, str(muxer + " muxer is not available")
self.filesink = gst.element_factory_make("filesink")
self.filesink.set_property("location", self.outfile)
self.timeline = gst.element_factory_make("gnltimeline") # Add ghost pad
self.audiocomp = gst.element_factory_make("gnlcomposition", "audiocomp") self.add_pad(gst.GhostPad('src', ident.get_pad('src')))
self.audioconvert = gst.element_factory_make("audioconvert")
self.add_many(self.timeline, self.audioconvert,
self.audioenc, self.muxer, self.filesink)
## identity perfect stream check ! def __on_new_decoded_pad(self, element, pad, last):
identity = gst.element_factory_make("identity") caps = pad.get_caps()
identity.set_property("check-perfect", True) name = caps[0].get_name()
self.add(identity) print '\n__on_new_decoded_pad:', name
if 'audio' in name:
if not self.__apad.is_linked(): # Only link once
pad.link(self.__apad)
#self.audioconvert.link(self.audioenc)
if not self.audioconvert.link(identity):
print "couldn't link audioconv -> ident"
if not identity.link(self.audioenc):
print "couldn't link ident -> audioenc"
self.audioenc.link(self.muxer)
self.muxer.link(self.filesink)
self.timeline.add(self.audiocomp)
caps = gst.caps_from_string("audio/x-raw-int,channels=2,rate=44100,depth=16")
pos = 0L
for infile in self.infiles:
d = Discoverer(infile)
if not d.audiolength:
continue
print "file", infile, "has length", time_to_string(d.audiolength)
asource = AudioSource(infile, caps)
gnlsource = gst.element_factory_make("gnlsource")
gnlsource.set_property("element", asource)
gnlsource.set_property("media-start", 0L)
gnlsource.set_property("media-stop", d.audiolength)
gnlsource.set_property("start", pos)
gnlsource.set_property("stop", pos + d.audiolength)
self.audiocomp.add(gnlsource)
pos += d.audiolength
self.timeline.get_pad("src_audiocomp").link(self.audioconvert.get_pad("sink")) class AudioConcat:
timelineprobe = gst.Probe(False, self.timelineprobe) '''Concatenates multiple audio files to single ogg/vorbis file'''
self.timeline.get_pad("src_audiocomp").add_probe(timelineprobe)
def timelineprobe(self, probe, data): caps = gst.caps_from_string('audio/x-raw-float, rate=44100, channels=2, endianness=1234, width=32')
if isinstance(data, gst.Buffer):
print "timeline outputs buffer", data.timestamp, data.duration
else:
print "timeline ouputs event", data.type
return True
gobject.type_register(AudioConcat) def __init__(self, infiles, outfile):
# These are used in iteration through infiles
self.infiles = infiles
self.i = 0
self.start = 0L
def eos_cb(pipeline): # The pipeline
sys.exit() self.pipeline = gst.Pipeline()
if __name__ == "__main__": # Create bus and connect 'eos' and 'error' handlers
if len(sys.argv) < 3: self.bus = self.pipeline.get_bus()
print "Usage : %s <input file(s)> <output file>" % sys.argv[0] self.bus.add_signal_watch()
print "\tCreates an ogg file from all the audio input files" self.bus.connect('message::eos', self.on_eos)
sys.exit() self.bus.connect('message::error', self.on_error)
if not gst.element_factory_make("gnltimeline"):
print "You need the gnonlin elements installed (http://gnonlin.sf.net/)" # Create elements
sys.exit() self.comp = gst.element_factory_make('gnlcomposition')
concat = AudioConcat(sys.argv[1:-1], sys.argv[-1]) self.enc = gst.element_factory_make('vorbisenc')
concat.connect("eos", eos_cb) self.mux = gst.element_factory_make('oggmux')
concat.set_state(gst.STATE_PLAYING) self.sink = gst.element_factory_make('filesink')
gst.main()
# Connect handler for 'pad-added' signal
self.comp.connect('pad-added', self.on_pad_added)
# Set 'location' property on filesink
self.sink.set_property('location', outfile)
# Add elements to pipeline
self.pipeline.add(self.comp, self.enc, self.mux, self.sink)
# Link *some* elements
# This in completed in self.on_pad_added()
gst.element_link_many(self.enc, self.mux, self.sink)
# Reference used in self.on_pad_added()
self.apad = self.enc.get_pad('sink')
# The MainLoop
self.mainloop = gobject.MainLoop()
# Iterate through infiles
gobject.idle_add(self.discover)
self.mainloop.run()
def discover(self):
infile = self.infiles[self.i]
discoverer = Discoverer(infile)
discoverer.connect('discovered', self.on_discovered, infile)
discoverer.discover()
return False # Don't repeat idle call
def on_discovered(self, discoverer, ismedia, infile):
print '\non_discovered:', infile
discoverer.print_info()
if discoverer.is_audio:
dec = AudioDec(infile, self.caps)
src = gst.element_factory_make('gnlsource')
src.add(dec)
src.set_property('media-start', 0L)
src.set_property('media-duration', discoverer.audiolength)
src.set_property('start', self.start)
src.set_property('duration', discoverer.audiolength)
self.comp.add(src)
self.start += discoverer.audiolength
self.i += 1
if self.i < len(self.infiles):
gobject.idle_add(self.discover)
else:
if self.start > 0: # At least 1 infile is_audio and audiolength > 0
self.pipeline.set_state(gst.STATE_PLAYING)
else:
self.mainloop.quit()
def on_pad_added(self, element, pad):
caps = pad.get_caps()
name = caps[0].get_name()
print '\non_pad_added:', name
if name == 'audio/x-raw-float':
if not self.apad.is_linked(): # Only link once
pad.link(self.apad)
def on_eos(self, bus, msg):
print '\non_eos'
self.mainloop.quit()
def on_error(self, bus, msg):
error = msg.parse_error()
print '\non_error:', error[1]
self.mainloop.quit()
if __name__ == '__main__':
if len(sys.argv) >= 3:
AudioConcat(sys.argv[1:-1], sys.argv[-1])
else:
print 'Usage: %s <input_file(s)> <output_file>' % sys.argv[0]
print 'Example: %s song1.mp3 song2.ogg output.ogg' % sys.argv[0]

View file

@ -111,8 +111,8 @@ def main(args):
buffers = int(args[1]) buffers = int(args[1])
if buffers < 1: if buffers < 1:
print 'buffers must be higher than 0' print 'buffers must be higher than 0'
return return
bps.run(buffers) bps.run(buffers)

View file

@ -26,15 +26,27 @@
import sys import sys
import gobject
gobject.threads_init()
import pygst
pygst.require('0.10')
import gst import gst
mainloop = gobject.MainLoop()
def on_eos(bus, msg):
mainloop.quit()
def filter(input, output): def filter(input, output):
"A GStreamer copy pipeline which can add arbitrary filters" "A GStreamer copy pipeline which can add arbitrary filters"
# create a new bin to hold the elements # create a new bin to hold the elements
bin = gst.parse_launch('filesrc name=source ! ' + bin = gst.parse_launch('filesrc name=source ! ' +
'statistics silent=false buffer-update-freq=1 ' + # This 'statistics' element is depreciated in 0.10
'update_on_eos=true ! ' + #'statistics silent=false buffer-update-freq=1 ' +
#'update_on_eos=true ! ' +
'filesink name=sink') 'filesink name=sink')
filesrc = bin.get_by_name('source') filesrc = bin.get_by_name('source')
filesrc.set_property('location', input) filesrc.set_property('location', input)
@ -42,12 +54,15 @@ def filter(input, output):
filesink = bin.get_by_name('sink') filesink = bin.get_by_name('sink')
filesink.set_property('location', output) filesink.set_property('location', output)
bus = bin.get_bus()
bus.add_signal_watch()
bus.connect('message::eos', on_eos)
# start playing # start playing
bin.set_state(gst.STATE_PLAYING); bin.set_state(gst.STATE_PLAYING)
try: try:
while bin.iterate(): mainloop.run()
pass
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass