~ubuntu-branches/ubuntu/precise/gst0.10-python/precise

« back to all changes in this revision

Viewing changes to examples/audioconcat.py

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2006-06-25 19:37:45 UTC
  • Revision ID: james.westby@ubuntu.com-20060625193745-9yeg0wq56r24n57x
Tags: upstream-0.10.4
ImportĀ upstreamĀ versionĀ 0.10.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# -*- Mode: Python -*-
 
3
# vi:si:et:sw=4:sts=4:ts=4
 
4
 
 
5
# audio concat tool
 
6
# takes in one or more audio files and creates one audio file of the combination
 
7
 
 
8
# Uses the gnonlin elements (http://gnonlin.sf.net/)
 
9
 
 
10
import os
 
11
import sys
 
12
import gobject
 
13
import gst
 
14
 
 
15
from gstfile import Discoverer, time_to_string
 
16
 
 
17
class AudioSource(gst.Bin):
 
18
    """A bin for audio sources with proper audio converters"""
 
19
 
 
20
    def __init__(self, filename, caps):
 
21
        gst.Bin.__init__(self)
 
22
        self.filename = filename
 
23
        self.outcaps = caps
 
24
 
 
25
        self.filesrc = gst.element_factory_make("filesrc")
 
26
        self.filesrc.set_property("location", self.filename)
 
27
        self.dbin = gst.element_factory_make("decodebin")
 
28
        self.ident = gst.element_factory_make("identity")
 
29
        self.audioconvert = gst.element_factory_make("audioconvert")
 
30
        self.audioscale = gst.element_factory_make("audioscale")
 
31
        
 
32
        self.add_many(self.filesrc, self.dbin, self.ident,
 
33
                      self.audioconvert, self.audioscale)
 
34
        self.filesrc.link(self.dbin)
 
35
        self.audioconvert.link(self.audioscale)
 
36
        self.audioscale.link(self.ident, caps)
 
37
        self.add_ghost_pad(self.ident.get_pad("src"), "src")
 
38
        
 
39
        self.dbin.connect("new-decoded-pad", self._new_decoded_pad_cb)
 
40
 
 
41
    def _new_decoded_pad_cb(self, dbin, pad, is_last):
 
42
        if not "audio" in pad.get_caps().to_string():
 
43
            return
 
44
        pad.link(self.audioconvert.get_pad("sink"))
 
45
 
 
46
gobject.type_register(AudioSource)
 
47
 
 
48
class AudioConcat(gst.Thread):
 
49
    """A Gstreamer thread that concatenates a series of audio files to another audio file"""
 
50
 
 
51
    def __init__(self, infiles, outfile, audioenc="rawvorbisenc", muxer="oggmux"):
 
52
        gst.Thread.__init__(self)
 
53
        self.infiles = infiles
 
54
        self.outfile = outfile
 
55
        self.audioenc = gst.element_factory_make(audioenc)
 
56
        if not self.audioenc:
 
57
            raise NameError, str(audioenc + " audio encoder is not available")
 
58
        self.muxer = gst.element_factory_make(muxer)
 
59
        if not self.muxer:
 
60
            raise NameError, str(muxer + " muxer is not available")
 
61
        self.filesink = gst.element_factory_make("filesink")
 
62
        self.filesink.set_property("location", self.outfile)
 
63
 
 
64
        self.timeline = gst.element_factory_make("gnltimeline")
 
65
        self.audiocomp = gst.element_factory_make("gnlcomposition", "audiocomp")
 
66
 
 
67
        self.audioconvert = gst.element_factory_make("audioconvert")
 
68
        self.add_many(self.timeline, self.audioconvert,
 
69
                      self.audioenc, self.muxer, self.filesink)
 
70
 
 
71
        ## identity perfect stream check !
 
72
        identity = gst.element_factory_make("identity")
 
73
        identity.set_property("check-perfect", True)
 
74
        self.add(identity)
 
75
        
 
76
        #self.audioconvert.link(self.audioenc)
 
77
        if not self.audioconvert.link(identity):
 
78
            print "couldn't link audioconv -> ident"
 
79
        if not identity.link(self.audioenc):
 
80
            print "couldn't link ident -> audioenc"
 
81
        self.audioenc.link(self.muxer)
 
82
        self.muxer.link(self.filesink)
 
83
 
 
84
        self.timeline.add(self.audiocomp)
 
85
 
 
86
        caps = gst.caps_from_string("audio/x-raw-int,channels=2,rate=44100,depth=16")
 
87
        pos = 0L
 
88
        for infile in self.infiles:
 
89
            d = Discoverer(infile)
 
90
            if not d.audiolength:
 
91
                continue
 
92
            print "file", infile, "has length", time_to_string(d.audiolength)
 
93
            asource = AudioSource(infile, caps)
 
94
            gnlsource = gst.element_factory_make("gnlsource")
 
95
            gnlsource.set_property("element", asource)
 
96
            gnlsource.set_property("media-start", 0L)
 
97
            gnlsource.set_property("media-stop", d.audiolength)
 
98
            gnlsource.set_property("start", pos)
 
99
            gnlsource.set_property("stop", pos + d.audiolength)
 
100
            self.audiocomp.add(gnlsource)
 
101
            pos += d.audiolength
 
102
 
 
103
        self.timeline.get_pad("src_audiocomp").link(self.audioconvert.get_pad("sink"))
 
104
        timelineprobe = gst.Probe(False, self.timelineprobe)
 
105
        self.timeline.get_pad("src_audiocomp").add_probe(timelineprobe)
 
106
 
 
107
    def timelineprobe(self, probe, data):
 
108
        if isinstance(data, gst.Buffer):
 
109
            print "timeline outputs buffer", data.timestamp, data.duration
 
110
        else:
 
111
            print "timeline ouputs event", data.type
 
112
        return True
 
113
 
 
114
gobject.type_register(AudioConcat)
 
115
 
 
116
def eos_cb(pipeline):
 
117
    sys.exit()
 
118
 
 
119
if __name__ == "__main__":
 
120
    if len(sys.argv) < 3:
 
121
        print "Usage : %s <input file(s)> <output file>" % sys.argv[0]
 
122
        print "\tCreates an ogg file from all the audio input files"
 
123
        sys.exit()
 
124
    if not gst.element_factory_make("gnltimeline"):
 
125
        print "You need the gnonlin elements installed (http://gnonlin.sf.net/)"
 
126
        sys.exit()
 
127
    concat = AudioConcat(sys.argv[1:-1], sys.argv[-1])
 
128
    concat.connect("eos", eos_cb)
 
129
    concat.set_state(gst.STATE_PLAYING)
 
130
    gst.main()