def print_stream_info(self, info, depth): caps = info.get_caps() if caps: if caps.is_fixed(): desc = GstPbutils.pb_utils_get_codec_description(caps) else: desc = caps.to_string() print('{0}: {1}'.format(2 * depth, info.get_stream_type_nick(), desc)) tags = info.get_tags() if tags: print('Tags: {0}'.format(2 * (depth + 1)))
def get_metadata(url): audio_url = get_audio_url(url) info = GstPbutils.Discoverer().discover_uri(audio_url) name = "" genres = "" web = "" tags = info.get_tags() n = tags.n_tags() for i in range(0, n): tag = tags.nth_tag_name(i) value = tags.get_string(tag)[1] print(i, ":", tag, "→", value) if tag == "organization": name = value elif tag == "genre": genres = value elif tag == "location": web = value stream_list = info.get_stream_list() audio_stream = stream_list[0] bitrate = str(int(audio_stream.get_bitrate() / 1000)) sample = str(audio_stream.get_sample_rate()) caps = audio_stream.get_caps() codec = GstPbutils.pb_utils_get_codec_description(caps) if bitrate == "0" or name == "" or genres == "" or web == "": try: req = urllib.request.urlopen(audio_url) head = req.info() req.close() except http.client.BadStatusLine: pass else: for tag in head.items(): match = re.match(r"ic[ey]-([a-z]+)", tag[0]) if match: print(match.group(1) + ": " + tag[1]) tagname = match.group(1) tagvalue = tag[1] if tagname == "br" or tagname == "bitrate": bitrate = tagvalue elif tagname == "name": name = tagvalue elif tagname == "genre": genres = tagvalue elif tagname == "url": web = tagvalue if bitrate == "0": bitrate = "N/A" if name == "": name = url dat = [name, url, genres, web, codec, bitrate, sample] return dat