在不缓冲音频的情况下获取Icecast歌曲标题 [英] Fetch Icecast song title without buffering audio

查看:114
本文介绍了在不缓冲音频的情况下获取Icecast歌曲标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个小的JavaScript库,该库可以为Icecast流获取正在播放"数据.根据我了解的情况,我可以通过向收音机发送HTTP请求来做到这一点流如下:

I want to write a small JavaScript library that can fetch "now playing" data for Icecast streams. From what I understand, I can do this by sending a HTTP request to a radio stream as follows:

GET /radiotunes_bebop HTTP/1.1
Host: pub2.radiotunes.com
Icy-MetaData:1

然后,服务器将使用一些响应标头进行回答.其中之一是icy-metaint字段,该字段指示将元数据插入流中的时间间隔.元数据将如下所示:

The server will then answer with some reponse headers. One of them is the icy-metaint field that indicates the interval at which the metadata is inserted into the stream. The metadata will look something like this:

StreamTitle='Dexter Gordon - Jodi';StreamUrl='';

尽管这可以为我提供所需的信息,但是如果您不需要音频本身,效率就不是很高. (在不播放流的情况下可能就是这种情况.)是否可以在不缓冲音频的情况下获取流标题?我知道我可以抓取HTML来获取它,但是该方法有很大的局限性.

Although this gets me the information I need, it isn't very efficient if you don't need the audio itself. (This can be the case when the stream isn't playing.) Is there a way to fetch the stream title without buffering the audio? I know I can scrape HTML to get it, but that method has significant limitations.

推荐答案

SHOUTcast/Icecast元数据始终位于第一个音频块之后.如果没有音频块,就无法获取流内元数据.

SHOUTcast/Icecast metadata always comes after the first audio chunk. There is no way to get in-stream metadata without having an audio chunk before it.

好消息是,这并不像您想象的那样效率低下.大多数站使用8KB元数据间隔.许多使用16KB的间隔.我认为我从未见过大于32KB的元数据间隔.连接后,服务器端将缓冲音频流并刷新此缓冲区.您通常会在第一个或第二个响应数据包中收到元数据.

The good news is that this isn't as inefficient as you might think. Most stations use 8KB metadata intervals. Many use 16KB intervals. I don't think I have ever seen a metadata interval greater than 32KB. The server side will buffer the audio stream and flush this buffer as soon as you connect. You will often receive metadata within the first or second response packet.

如果对您有帮助,我有一个免费的API,可用于从流中获取元数据完全符合您的建议.它连接到流,跳过音频数据,解析元数据,然后返回JSON.可以从浏览器中访问它.

If it's helpful to you, I have a free API available for fetching metadata from streams that does exactly what you propose. It connects to a stream, skips through the audio data, parses the metadata, and returns JSON. It is accessible from within the browser.

$.getJSON('http://api.audiopump.co/metadata/getStreamMetadata', {
  url: 'http://cdn.audiopump.co/radioreddit/main_mp3_128k',
  apiKey: 'YOUR_API_KEY'
}).done(function (data) {
  console.log(data);
});

响应

{
  "streamInfo": {
    "contentType": "audio/mpeg",
    "name": "Radio Reddit - Main",
    "genres": [
      "Indie",
      "Rock",
      "Talk"
    ],
    "websiteUrl": "http://radioreddit.com",
    "isPublic": true
  },
  "current": {
    "filename": "the_Nothingdoers_(evanowe)_Things_We_Should_Forget.mp3",
    "StreamTitle": "the Nothingdoers (/u/evanowe) - Things We Should Forget"
  }
}

这篇关于在不缓冲音频的情况下获取Icecast歌曲标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆