3 # vi:si:et:sw=4:sts=4:ts=4
5 # audioconcat.py - Concatenates multiple audio files to single ogg/vorbis file
6 # Uses the gnonlin elements (http://gnonlin.sf.net/)
7 # Copyright (C) 2005 Edward Hervey <edward@fluendo.com>
8 # 2006 Jason Gerard DeRose <jderose@jasonderose.org>
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Library General Public
12 # License as published by the Free Software Foundation; either
13 # version 2 of the License, or (at your option) any later version.
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 # Library General Public License for more details.
20 # You should have received a copy of the GNU Library General Public
21 # License along with this library; if not, write to the
22 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 # Boston, MA 02110-1301, USA.
29 gobject.threads_init()
34 from gst.extend.discoverer import Discoverer
38 class AudioDec(gst.Bin):
39 '''Decodes audio file, outputs at specified caps'''
41 def __init__(self, location, caps):
42 gst.Bin.__init__(self)
45 src = gst.element_factory_make('filesrc')
46 dec = gst.element_factory_make('decodebin')
47 conv = gst.element_factory_make('audioconvert')
48 rsmpl = gst.element_factory_make('audioresample')
49 ident = gst.element_factory_make('identity')
51 # Set 'location' property on filesrc
52 src.set_property('location', location)
54 # Connect handler for 'new-decoded-pad' signal
55 dec.connect('new-decoded-pad', self.__on_new_decoded_pad)
58 self.add(src, dec, conv, rsmpl, ident)
60 # Link *some* elements
61 # This is completed in self.__on_new_decoded_pad()
64 rsmpl.link(ident, caps)
66 # Reference used in self.__on_new_decoded_pad()
67 self.__apad = conv.get_pad('sink')
70 self.add_pad(gst.GhostPad('src', ident.get_pad('src')))
73 def __on_new_decoded_pad(self, element, pad, last):
75 name = caps[0].get_name()
76 print '\n__on_new_decoded_pad:', name
78 if not self.__apad.is_linked(): # Only link once
85 '''Concatenates multiple audio files to single ogg/vorbis file'''
87 caps = gst.caps_from_string('audio/x-raw-float, rate=44100, channels=2, endianness=1234, width=32')
89 def __init__(self, infiles, outfile):
90 # These are used in iteration through infiles
91 self.infiles = infiles
96 self.pipeline = gst.Pipeline()
98 # Create bus and connect 'eos' and 'error' handlers
99 self.bus = self.pipeline.get_bus()
100 self.bus.add_signal_watch()
101 self.bus.connect('message::eos', self.on_eos)
102 self.bus.connect('message::error', self.on_error)
105 self.comp = gst.element_factory_make('gnlcomposition')
106 self.enc = gst.element_factory_make('vorbisenc')
107 self.mux = gst.element_factory_make('oggmux')
108 self.sink = gst.element_factory_make('filesink')
110 # Connect handler for 'pad-added' signal
111 self.comp.connect('pad-added', self.on_pad_added)
113 # Set 'location' property on filesink
114 self.sink.set_property('location', outfile)
116 # Add elements to pipeline
117 self.pipeline.add(self.comp, self.enc, self.mux, self.sink)
119 # Link *some* elements
120 # This in completed in self.on_pad_added()
121 gst.element_link_many(self.enc, self.mux, self.sink)
123 # Reference used in self.on_pad_added()
124 self.apad = self.enc.get_pad('sink')
127 self.mainloop = gobject.MainLoop()
129 # Iterate through infiles
130 gobject.idle_add(self.discover)
135 infile = self.infiles[self.i]
136 discoverer = Discoverer(infile)
137 discoverer.connect('discovered', self.on_discovered, infile)
138 discoverer.discover()
139 return False # Don't repeat idle call
142 def on_discovered(self, discoverer, ismedia, infile):
143 print '\non_discovered:', infile
144 discoverer.print_info()
145 if discoverer.is_audio:
146 dec = AudioDec(infile, self.caps)
147 src = gst.element_factory_make('gnlsource')
149 src.set_property('media-start', 0L)
150 src.set_property('media-duration', discoverer.audiolength)
151 src.set_property('start', self.start)
152 src.set_property('duration', discoverer.audiolength)
154 self.start += discoverer.audiolength
156 if self.i < len(self.infiles):
157 gobject.idle_add(self.discover)
159 if self.start > 0: # At least 1 infile is_audio and audiolength > 0
160 self.pipeline.set_state(gst.STATE_PLAYING)
165 def on_pad_added(self, element, pad):
166 caps = pad.get_caps()
167 name = caps[0].get_name()
168 print '\non_pad_added:', name
169 if name == 'audio/x-raw-float':
170 if not self.apad.is_linked(): # Only link once
174 def on_eos(self, bus, msg):
179 def on_error(self, bus, msg):
180 error = msg.parse_error()
181 print '\non_error:', error[1]
187 if __name__ == '__main__':
188 if len(sys.argv) >= 3:
189 AudioConcat(sys.argv[1:-1], sys.argv[-1])
191 print 'Usage: %s <input_file(s)> <output_file>' % sys.argv[0]
192 print 'Example: %s song1.mp3 song2.ogg output.ogg' % sys.argv[0]