def _notify_caps(self, pad, args): # The sink has started to receive data, so the stream is ready. # This also is our opportunity to read information about the # stream. self.got_caps = True info = pad.get_negotiated_caps()[0] # Stream attributes. self.channels = info['channels'] self.samplerate = info['rate'] # Query duration. q = gst.query_new_duration(gst.FORMAT_TIME) if pad.get_peer().query(q): # Success. format, length = q.parse_duration() if format == gst.FORMAT_TIME: self.duration = float(length) / 1000000000 else: self.read_exc = MetadataMissingError( 'duration in unknown format' ) else: # Query failed. self.read_exc = MetadataMissingError('duration not available') # Allow constructor to complete. self.ready_sem.release()
def _notify_caps(self, pad, args): # The sink has started to receive data, so the stream is ready. # This also is our opportunity to read information about the # stream. info = pad.get_negotiated_caps()[0] # Stream attributes. self.channels = info['channels'] self.samplerate = info['rate'] # Query duration. q = gst.query_new_duration(gst.FORMAT_TIME) if pad.get_peer().query(q): # Success. format, length = q.parse_duration() if format == gst.FORMAT_TIME: self.duration = float(length) / 1000000000 else: # Not sure what happened. self.duration = None else: # Failure. self.duration = None # Allow constructor to complete. self.ready_sem.release()
def _notify_caps(self, pad, args): # The sink has started to receive data, so the stream is ready. # This also is our opportunity to read information about the # stream. info = pad.get_negotiated_caps()[0] # Stream attributes. self.channels = info['channels'] self.samplerate = info['rate'] # Query duration. q = gst.query_new_duration(gst.FORMAT_TIME) if pad.get_peer().query(q): # Success. format, length = q.parse_duration() if format == gst.FORMAT_TIME: self.duration = length else: # Not sure what happened. self.duration = None else: # Failure. self.duration = None # Allow constructor to complete. self.ready_sem.release()
def _notify_caps_cb(self, pad, args): caps = pad.get_negotiated_caps() if not caps: pad.info("no negotiated caps available") return pad.info("caps:%s" % caps.to_string()) # the caps are fixed # We now get the total length of that stream q = gst.query_new_duration(gst.FORMAT_TIME) pad.info("sending position query") if pad.get_peer().query(q): format, length = q.parse_duration() pad.info("got position query answer : %d:%d" % (length, format)) else: length = -1 gst.warning("position query didn't work") # We store the caps and length in the proper location if "audio" in caps.to_string(): self.audiocaps = caps self.audiolength = length try: pos = 0 cap = caps[pos] while not cap.has_key("rate"): pos += 1 cap = caps[pos] self.audiorate = cap["rate"] self.audiowidth = cap["width"] self.audiochannels = cap["channels"] except IndexError: pass if "x-raw-float" in caps.to_string(): self.audiofloat = True else: self.audiodepth = caps[0]["depth"] if self._nomorepads and ((not self.is_video) or self.videocaps): _log.debug("called @1") self._finished(True) elif "video" in caps.to_string(): self.videocaps = caps self.videolength = length try: pos = 0 cap = caps[pos] while not cap.has_key("width"): pos += 1 cap = caps[pos] self.videowidth = cap["width"] self.videoheight = cap["height"] self.videorate = cap["framerate"] except IndexError: pass if self._nomorepads and ((not self.is_audio) or self.audiocaps): _log.debug("called @2") self._finished(True)
def _notify_caps_cb(self, pad, args): caps = pad.get_negotiated_caps() if not caps: pad.info("no negotiated caps available") return pad.info("caps:%s" % caps.to_string()) # the caps are fixed # We now get the total length of that stream q = gst.query_new_duration(gst.FORMAT_TIME) pad.info("sending position query") if pad.get_peer().query(q): format, length = q.parse_duration() pad.info("got position query answer : %d:%d" % (length, format)) else: length = -1 gst.warning("position query didn't work") # We store the caps and length in the proper location if "audio" in caps.to_string(): self.audiocaps = caps self.audiolength = length try: pos = 0 cap = caps[pos] while not cap.has_key("rate"): pos += 1 cap = caps[pos] self.audiorate = cap["rate"] self.audiowidth = cap["width"] self.audiochannels = cap["channels"] except IndexError: pass if "x-raw-float" in caps.to_string(): self.audiofloat = True else: self.audiodepth = caps[0]["depth"] if self._nomorepads and ((not self.is_video) or self.videocaps): self._finished(True) elif "video" in caps.to_string(): self.videocaps = caps self.videolength = length try: pos = 0 cap = caps[pos] while not cap.has_key("width"): pos += 1 cap = caps[pos] self.videowidth = cap["width"] self.videoheight = cap["height"] self.videorate = cap["framerate"] except IndexError: pass if self._nomorepads and ((not self.is_audio) or self.audiocaps): self._finished(True)
def getDuration(self): try: query = gst.query_new_duration(gst.FORMAT_TIME) if self.playbin.query(query): total = query.parse_duration()[1] else: return 0 except gst.QueryError: total = 0 total = total / 1000000 return total
def get_duration(self): if self.get_state() != gst.STATE_NULL: try: query = gst.query_new_duration(gst.FORMAT_TIME) encoder = self.pipeline.get_by_name("encoder") if encoder.query(query): total = query.parse_duration()[1] else: return 0 except gst.QueryError: total = 0 total //= gst.MSECOND return total else: return 0
def _notify_caps_cb(self, pad, args): self.discovered_cond.acquire() caps = pad.get_negotiated_caps() if not caps: pad.info("no negotiated caps available") self.discovered = True self.discovered_cond.notify() self.discovered_cond.release() return # the caps are fixed # We now get the total length of that stream q = gst.query_new_duration(gst.FORMAT_TIME) pad.info("sending duration query") if pad.get_peer().query(q): format, length = q.parse_duration() if format == gst.FORMAT_TIME: pad.info("got duration (time) : %s" % (gst.TIME_ARGS(length),)) else: pad.info("got duration : %d [format:%d]" % (length, format)) else: length = -1 gst.warning("duration query failed") # We store the caps and length in the proper location if "audio" in caps.to_string(): self.input_samplerate = caps[0]["rate"] if not self.output_samplerate: self.output_samplerate = self.input_samplerate self.input_channels = caps[0]["channels"] if not self.output_channels: self.output_channels = self.input_channels self.input_duration = length / gst.SECOND self.input_totalframes = int( self.input_duration * self.input_samplerate) if "x-raw-float" in caps.to_string(): self.input_width = caps[0]["width"] else: self.input_width = caps[0]["depth"] self.discovered = True self.discovered_cond.notify() self.discovered_cond.release()
def _notify_caps_cb(self, pad, args): caps = pad.get_negotiated_caps() if not caps: pad.info("no negotiated caps available") return pad.info("caps:%s" % caps.to_string()) # the caps are fixed # We now get the total length of that stream q = gst.query_new_duration(gst.FORMAT_TIME) pad.info("sending duration query") if pad.get_peer().query(q): format, length = q.parse_duration() if format == gst.FORMAT_TIME: pad.info("got duration (time) : %s" % (gst.TIME_ARGS(length), )) else: pad.info("got duration : %d [format:%d]" % (length, format)) else: length = -1 gst.warning("duration query failed") # We store the caps and length in the proper location if "audio" in caps.to_string(): self.audiocaps = caps self.audiolength = length self.audiorate = caps[0]["rate"] self.audiowidth = caps[0]["width"] self.audiochannels = caps[0]["channels"] if "x-raw-float" in caps.to_string(): self.audiofloat = True else: self.audiodepth = caps[0]["depth"] if self._nomorepads and ((not self.is_video) or self.videocaps): self._finished(True) elif "video" in caps.to_string(): self.videocaps = caps self.videolength = length self.videowidth = caps[0]["width"] self.videoheight = caps[0]["height"] self.videorate = caps[0]["framerate"] if self._nomorepads and ((not self.is_audio) or self.audiocaps): self._finished(True)
def _notify_caps_cb(self, pad, args): caps = pad.get_negotiated_caps() if not caps: pad.info("no negotiated caps available") return pad.info("caps:%s" % caps.to_string()) # the caps are fixed # We now get the total length of that stream q = gst.query_new_duration(gst.FORMAT_TIME) pad.info("sending duration query") if pad.get_peer().query(q): format, length = q.parse_duration() if format == gst.FORMAT_TIME: pad.info("got duration (time) : %s" % (gst.TIME_ARGS(length),)) else: pad.info("got duration : %d [format:%d]" % (length, format)) else: length = -1 gst.warning("duration query failed") # We store the caps and length in the proper location if "audio" in caps.to_string(): self.audiocaps = caps self.audiolength = length self.audiorate = caps[0]["rate"] self.audiowidth = caps[0]["width"] self.audiochannels = caps[0]["channels"] if "x-raw-float" in caps.to_string(): self.audiofloat = True else: self.audiodepth = caps[0]["depth"] if self._nomorepads and ((not self.is_video) or self.videocaps): self._finished(True) elif "video" in caps.to_string(): self.videocaps = caps self.videolength = length self.videowidth = caps[0]["width"] self.videoheight = caps[0]["height"] self.videorate = caps[0]["framerate"] if self._nomorepads and ((not self.is_audio) or self.audiocaps): self._finished(True)
def __read_from_remote_file(self): ''' Load song information from remote file. ''' GST_IDS = { "title": "title", "genre": "genre", "artist": "artist", "album": "album", "bitrate": "#bitrate", 'track-number': "#track" } is_finalize = False is_tagged = False def unknown_type(*param): raise "W:Song:GstTag:Gst decoder: type inconnu" def finalize(pipeline): state_ret = pipeline.set_state(gst.STATE_NULL) if state_ret != gst.STATE_CHANGE_SUCCESS: print "Failed change to null" def message(bus, message, pipeline): if message.type == gst.MESSAGE_EOS: finalize(pipeline) elif message.type == gst.MESSAGE_TAG: taglist = message.parse_tag() for key in taglist.keys(): if GST_IDS.has_key(key): if key == "bitrate": value = int(taglist[key] / 100) elif isinstance(taglist[key], long): value = int(taglist[key]) else: value = taglist[key] self[GST_IDS[key]] = utils.fix_charset(value) print key, ":", utils.fix_charset(value) is_tagged = True elif message.type == gst.MESSAGE_ERROR: err, debug = message.parse_error() finalize(pipeline) raise "W:Song:GstTag:Decoder error: %s%s" % (err, debug) try: try: url = utils.get_uri_from_path(self.get("uri").encode("utf-8")) pipeline = gst.parse_launch( "gnomevfssrc location=" + url + " ! decodebin name=decoder ! fakesink") except gobject.GError: raise "W:Song:GstTag:Failed to build pipeline to read metadata of", self.get( "uri") decoder = pipeline.get_by_name("decoder") decoder.connect("unknown_type", unknown_type) bus = pipeline.get_bus() bus.connect("message", message, pipeline) bus.add_signal_watch() state_ret = pipeline.set_state(gst.STATE_PAUSED) timeout = 10 while state_ret == gst.STATE_CHANGE_ASYNC and not is_finalize and timeout > 0: state_ret, _state, _pending_state = pipeline.get_state( 1 * gst.SECOND) timeout -= 1 if state_ret != gst.STATE_CHANGE_SUCCESS: finalize(pipeline) print "W:Song:GstTag:Failed Read Media" else: if not is_tagged: bus.poll(gst.MESSAGE_TAG, 5 * gst.SECOND) try: query = gst.query_new_duration(gst.FORMAT_TIME) if pipeline.query(query): total = query.parse_duration()[1] else: total = 0 except gst.QueryError: total = 0 total //= gst.MSECOND self["#duration"] = total if not is_tagged: print "W:Song:GstTag: Media found but no tag found" finalize(pipeline) except Exception, e: print "W: Error while loading (" + self.get( "uri") + ")Tracback :", e self.last_error = ( "Error while reading") + ": " + self.get_filename() return False
def __read_from_remote_file(self): ''' Load song information from remote file. ''' GST_IDS = {"title" : "title", "genre" : "genre", "artist" : "artist", "album" : "album", "bitrate" : "#bitrate", 'track-number':"#track"} is_finalize = False is_tagged = False def unknown_type(*param): raise "W:Song:GstTag:Gst decoder: type inconnu" def finalize(pipeline): state_ret = pipeline.set_state(gst.STATE_NULL) if state_ret != gst.STATE_CHANGE_SUCCESS: print "Failed change to null" def message(bus, message, pipeline): if message.type == gst.MESSAGE_EOS: finalize(pipeline) elif message.type == gst.MESSAGE_TAG: taglist = message.parse_tag() for key in taglist.keys(): if GST_IDS.has_key(key): if key == "bitrate": value = int(taglist[key] / 100) elif isinstance(taglist[key], long): value = int(taglist[key]) else: value = taglist[key] self[GST_IDS[key]] = utils.fix_charset(value) print key,":", utils.fix_charset(value) is_tagged = True elif message.type == gst.MESSAGE_ERROR: err, debug = message.parse_error() finalize(pipeline) raise "W:Song:GstTag:Decoder error: %s\n%s" % (err,debug) try: try: url = utils.get_uri_from_path(self.get("uri").encode("utf-8")) pipeline = gst.parse_launch("gnomevfssrc location="+url+" ! decodebin name=decoder ! fakesink") except gobject.GError: raise "W:Song:GstTag:Failed to build pipeline to read metadata of",self.get("uri") decoder = pipeline.get_by_name("decoder") decoder.connect("unknown_type", unknown_type) bus = pipeline.get_bus() bus.connect("message", message, pipeline) bus.add_signal_watch() state_ret = pipeline.set_state(gst.STATE_PAUSED) timeout = 10 while state_ret == gst.STATE_CHANGE_ASYNC and not is_finalize and timeout > 0: state_ret, _state, _pending_state = pipeline.get_state(1 * gst.SECOND) timeout -= 1 if state_ret != gst.STATE_CHANGE_SUCCESS: finalize(pipeline) print "W:Song:GstTag:Failed Read Media" else: if not is_tagged: bus.poll(gst.MESSAGE_TAG, 5 * gst.SECOND) try: query = gst.query_new_duration(gst.FORMAT_TIME) if pipeline.query(query): total = query.parse_duration()[1] else: total = 0 except gst.QueryError: total = 0 total //= gst.MSECOND self["#duration"] = total if not is_tagged: print "W:Song:GstTag: Media found but no tag found" finalize(pipeline) except Exception, e: print "W: Error while loading ("+self.get("uri")+")\nTracback :",e self.last_error = ("Error while reading") + ": " + self.get_filename() return False