Back to development
[platform/upstream/gstreamer.git] / subprojects / gst-python / old_examples / audioconcat.py
1 #!/usr/bin/env python
2 # -*- Mode: Python -*-
3 # vi:si:et:sw=4:sts=4:ts=4
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>
9 #
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.
14 #
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.
19 #
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.
24
25
26 import sys
27
28 import gobject
29 gobject.threads_init()
30
31 import pygst
32 pygst.require('0.10')
33 import gst
34 from gst.extend.discoverer import Discoverer
35
36
37
38 class AudioDec(gst.Bin):
39         '''Decodes audio file, outputs at specified caps'''
40
41         def __init__(self, location, caps):
42                 gst.Bin.__init__(self)
43
44                 # Create elements
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')
50
51                 # Set 'location' property on filesrc
52                 src.set_property('location', location)
53
54                 # Connect handler for 'new-decoded-pad' signal 
55                 dec.connect('new-decoded-pad', self.__on_new_decoded_pad)
56
57                 # Add elements to bin
58                 self.add(src, dec, conv, rsmpl, ident)
59
60                 # Link *some* elements 
61                 # This is completed in self.__on_new_decoded_pad()
62                 src.link(dec)
63                 conv.link(rsmpl)
64                 rsmpl.link(ident, caps)
65
66                 # Reference used in self.__on_new_decoded_pad()
67                 self.__apad = conv.get_pad('sink')
68
69                 # Add ghost pad
70                 self.add_pad(gst.GhostPad('src', ident.get_pad('src')))
71
72
73         def __on_new_decoded_pad(self, element, pad, last):
74                 caps = pad.get_caps()
75                 name = caps[0].get_name()
76                 print '\n__on_new_decoded_pad:', name
77                 if 'audio' in name:
78                         if not self.__apad.is_linked(): # Only link once
79                                 pad.link(self.__apad)
80
81
82
83
84 class AudioConcat:
85         '''Concatenates multiple audio files to single ogg/vorbis file'''
86
87         caps = gst.caps_from_string('audio/x-raw-float, rate=44100, channels=2, endianness=1234, width=32')
88         
89         def __init__(self, infiles, outfile):
90                 # These are used in iteration through infiles   
91                 self.infiles = infiles
92                 self.i = 0
93                 self.start = 0L
94
95                 # The pipeline
96                 self.pipeline = gst.Pipeline()
97
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)
103
104                 # Create elements
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')
109
110                 # Connect handler for 'pad-added' signal 
111                 self.comp.connect('pad-added', self.on_pad_added)       
112
113                 # Set 'location' property on filesink
114                 self.sink.set_property('location', outfile)
115
116                 # Add elements to pipeline
117                 self.pipeline.add(self.comp, self.enc, self.mux, self.sink)
118
119                 # Link *some* elements
120                 # This in completed in self.on_pad_added()
121                 gst.element_link_many(self.enc, self.mux, self.sink)
122
123                 # Reference used in self.on_pad_added()
124                 self.apad = self.enc.get_pad('sink')
125
126                 # The MainLoop
127                 self.mainloop = gobject.MainLoop()
128
129                 # Iterate through infiles
130                 gobject.idle_add(self.discover)
131                 self.mainloop.run()
132
133
134         def discover(self):
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
140
141
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')
148                         src.add(dec)
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)
153                         self.comp.add(src)
154                         self.start += discoverer.audiolength
155                 self.i += 1
156                 if self.i < len(self.infiles):
157                         gobject.idle_add(self.discover)
158                 else:
159                         if self.start > 0: # At least 1 infile is_audio and audiolength > 0
160                                 self.pipeline.set_state(gst.STATE_PLAYING)
161                         else:
162                                 self.mainloop.quit()
163
164
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
171                                 pad.link(self.apad)
172
173
174         def on_eos(self, bus, msg):
175                 print '\non_eos'
176                 self.mainloop.quit()
177
178
179         def on_error(self, bus, msg):
180                 error = msg.parse_error()
181                 print '\non_error:', error[1]
182                 self.mainloop.quit()
183
184
185
186
187 if __name__ == '__main__':
188         if len(sys.argv) >= 3:
189                 AudioConcat(sys.argv[1:-1], sys.argv[-1])
190         else:
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]