Python gi.repository.Gst.Pipeline() Examples

The following are 10 code examples of gi.repository.Gst.Pipeline(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module gi.repository.Gst , or try the search function .
Example #1
Source File: gstreamer.py    From python-snapcast with MIT License 5 votes vote down vote up
def __init__(self):
        """ Initialize app src. """
        self._mainloop = GObject.MainLoop()
        self._pipeline = Gst.Pipeline()

        # Make elements.
        self._src = Gst.ElementFactory.make('appsrc', 'appsrc')
        decode = Gst.ElementFactory.make("decodebin", "decode")
        self._queueaudio = Gst.ElementFactory.make('queue', 'queueaudio')
        audioconvert = Gst.ElementFactory.make('audioconvert', 'audioconvert')
        sink = Gst.ElementFactory.make('alsasink', 'sink')

        self._src.set_property('stream-type', 'stream')

        # Add to pipeline.
        self._pipeline.add(self._src)
        self._pipeline.add(decode)
        self._pipeline.add(self._queueaudio)
        self._pipeline.add(audioconvert)
        self._pipeline.add(sink)

        # Link elements.
        self._src.link(decode)
        self._queueaudio.link(audioconvert)
        audioconvert.link(sink)
        decode.connect('pad-added', self._decode_src_created) 
Example #2
Source File: video.py    From visual_dynamics with MIT License 5 votes vote down vote up
def _create_main_pipeline(self, device, size, fps, sync):
        self.pipeline = Gst.Pipeline()

        self.source = Gst.ElementFactory.make('v4l2src', 'source')
        self.source.set_property('device', device)
        self.source.set_property('do-timestamp', 'true')
        # run 'v4l2-ctl --list-ctrls' for full list of controls
        struct, _ = Gst.structure_from_string('name,\
                                               white_balance_temperature_auto=(bool){0},\
                                               backlight_compensation=(int){0},\
                                               exposure_auto=0,\
                                               focus_auto=(bool){0}')
        self.source.set_property('extra-controls', struct)

        caps = Gst.caps_from_string('video/x-raw,format=(string){BGR},width=%d,height=%d,framerate=%d/1'%(size[1], size[0], fps))
        self.sink = Gst.ElementFactory.make('appsink', 'sink')
        self.sink.set_property('emit-signals', True)
        self.sink.set_property('sync', sync)
        self.sink.set_property('drop', True)
        self.sink.set_property('max-buffers', 1)
        self.sink.set_property('caps', caps)
        self.sink.emit('pull-preroll')

        self.pipeline.add(self.source)
        self.pipeline.add(self.sink)

        Gst.Element.link(self.source, self.sink) 
Example #3
Source File: decoder2.py    From kaldi-gstreamer-server with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _on_eos(self, bus, msg):
        logger.info('%s: Pipeline received eos signal' % self.request_id)
        #self.decodebin.unlink(self.audioconvert)
        self.finish_request()
        if self.eos_handler:
            self.eos_handler[0](self.eos_handler[1]) 
Example #4
Source File: decoder.py    From kaldi-gstreamer-server with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _on_eos(self, bus, msg):
        logger.info('%s: Pipeline received eos signal' % self.request_id)
        self.finish_request()
        if self.eos_handler:
            self.eos_handler[0](self.eos_handler[1]) 
Example #5
Source File: decoder.py    From kaldi-gstreamer-server with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def init_request(self, id, caps_str):
        self.request_id = id
        if caps_str and len(caps_str) > 0:
            logger.info("%s: Setting caps to %s" % (self.request_id, caps_str))
            caps = Gst.caps_from_string(caps_str)
            self.appsrc.set_property("caps", caps)
        else:
            #caps = Gst.caps_from_string(None)
            self.appsrc.set_property("caps", None)
            #self.pipeline.set_state(Gst.State.READY)
            pass
        #self.appsrc.set_state(Gst.State.PAUSED)

        if self.outdir:
            self.pipeline.set_state(Gst.State.PAUSED)
            self.filesink.set_state(Gst.State.NULL)
            self.filesink.set_property('location', "%s/%s.raw" % (self.outdir, id))
            self.filesink.set_state(Gst.State.PLAYING)

        #self.filesink.set_state(Gst.State.PLAYING)
        #self.decodebin.set_state(Gst.State.PLAYING)
        self.pipeline.set_state(Gst.State.PLAYING)
        self.filesink.set_state(Gst.State.PLAYING)
        # push empty buffer (to avoid hang on client diconnect)
        buf = Gst.Buffer.new_allocate(None, 0, None)
        self.appsrc.emit("push-buffer", buf)
        logger.info('%s: Pipeline initialized' % (self.request_id)) 
Example #6
Source File: base.py    From AstroBox with GNU Affero General Public License v3.0 5 votes vote down vote up
def __del__(self):
		self._logger.info('Pipeline destroyed') 
Example #7
Source File: recordRadio.py    From pyAudioAnalysis with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        self._pipeline = Gst.Pipeline()
        self.uri        = None
        self.filename   = None
        self.samplerate = 44100
        self.listen     = False 
Example #8
Source File: audiotsmcli_gst.py    From audiotsm with MIT License 5 votes vote down vote up
def main():
    """Change the speed of an audio file without changing its pitch."""

    parser = argparse.ArgumentParser(description=(
        "Change the speed of an audio file without changing its pitch."))
    parser.add_argument(
        '-s', '--speed', metavar="S", type=float, default=1.,
        help="Set the speed ratio (e.g 0.5 to play at half speed)")
    parser.add_argument(
        'input_filename', metavar='INPUT_FILENAME', type=str,
        help="The audio input file")
    parser.add_argument(
        'output_filename', metavar='OUTPUT_FILENAME', type=str,
        help="The audio output file")

    args = parser.parse_args()

    if not os.path.isfile(args.input_filename):
        parser.error(
            'The input file "{}" does not exist.'.format(args.input_filename))

    pipeline = Pipeline()
    pipeline.set_speed(args.speed)
    pipeline.set_src_uri('file:///' + os.path.realpath(args.input_filename))
    pipeline.save(args.output_filename)

    loop = GObject.MainLoop()
    loop.run() 
Example #9
Source File: base.py    From AstroBox with GNU Affero General Public License v3.0 4 votes vote down vote up
def __init__(self, device, size, rotation, onFatalError, mainLoop, debugLevel):

		if not Gst.init_check(None):
			raise ImportError

		if debugLevel > 0:
			Gst.debug_set_active(True)
			Gst.debug_set_default_threshold(debugLevel)

		self._onFatalError = onFatalError
		self._mainLop = mainLoop

		self._toreDownAlready = False

		#pipeline control
		self._currentPipelineState = None
		self._pipelineStateCondition = Condition()
		## Make sure attach and detach operation wait for each other to complete ##
		self._photoBinAttachDetachLock = Lock()
		self._localVideoBinAttachDetachLock = Lock()
		###########################################################################

		self._pipeline = Gst.Pipeline()

		self._videoSrcBin = self._getVideoSrcBin(self._pipeline, device, size, rotation)
		self._videoEncBin = self._getVideoEncBin(size, rotation)
		self._photoCaptureBin = PhotoCaptureBin(self._onNoMorePhotos)
		self._localVideoBin = ImgVideoEncBin(size, rotation, self._onStopPhotoSeq)

		self._pipeline.add(self._videoEncBin.bin)
		self._pipeline.add(self._photoCaptureBin.bin)
		self._pipeline.add(self._localVideoBin.bin)

		self._bus = self._pipeline.get_bus()
		self._bus.set_flushing(True)

		self._busListener = BusListener(self._bus)
		self._busListener.addListener(Gst.MessageType.ERROR, self._onBusError)
		self._busListener.addListener(Gst.MessageType.EOS, self._onBusEos)
		self._busListener.addListener(Gst.MessageType.STATE_CHANGED, self._onBusStateChanged)
		self._busListener.addListener(Gst.MessageType.REQUEST_STATE, self._onRequestState)
		self._busListener.start() 
Example #10
Source File: gstreamer.py    From pyglet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def __init__(self, filename, file=None):
        self._pipeline = Gst.Pipeline()

        if file:
            file.seek(0)
            self._file = tempfile.NamedTemporaryFile(buffering=False)
            self._file.write(file.read())
            filename = self._file.name

        # Create the major parts of the pipeline:
        self.filesrc = Gst.ElementFactory.make("filesrc", None)
        self.decoder = Gst.ElementFactory.make("decodebin", None)
        self.audio_converter = Gst.ElementFactory.make("audioconvert", None)
        self.sink = Gst.ElementFactory.make("appsink", None)
        if not all((self.filesrc, self.decoder, self.audio_converter, self.sink)):
            raise GStreamerDecodeException("Could not initialize GStreamer.")

        # Set callbacks for EOS and errors:
        self._pipeline.bus.add_signal_watch()
        self._pipeline.bus.connect("message::eos", self._message)
        self._pipeline.bus.connect("message::error", self._message)

        # Set the file path to load:
        self.filesrc.set_property("location", filename)

        # Set decoder callback handlers:
        self.decoder.connect("pad-added", self._pad_added)
        self.decoder.connect("no-more-pads", self._no_more_pads)
        self.decoder.connect("unknown-type", self._unknown_type)

        # Set the sink's capabilities and behavior:
        self.sink.set_property('caps', Gst.Caps.from_string('audio/x-raw,format=S16LE'))
        self.sink.set_property('drop', False)
        self.sink.set_property('sync', False)
        self.sink.set_property('max-buffers', 5)
        self.sink.set_property('emit-signals', True)
        # The callback to receive decoded data:
        self.sink.connect("new-sample", self._new_sample)

        # Add all components to the pipeline:
        self._pipeline.add(self.filesrc)
        self._pipeline.add(self.decoder)
        self._pipeline.add(self.audio_converter)
        self._pipeline.add(self.sink)
        # Link together necessary components:
        self.filesrc.link(self.decoder)
        self.audio_converter.link(self.sink)

        # Callback to notify once the sink is ready:
        self.caps_handler = self.sink.get_static_pad("sink").connect("notify::caps", self._notify_caps)

        # Set by callbacks:
        self._pads = False
        self._caps = False
        self._pipeline.set_state(Gst.State.PLAYING)
        self._queue = queue.Queue(5)
        self._finished = Event()
        # Wait until the is_ready event is set by a callback:
        self._is_ready = Event()
        if not self._is_ready.wait(timeout=1):
            raise GStreamerDecodeException('Initialization Error')