使用PHP从音频流中提取轨道信息 [英] Pulling Track Info From an Audio Stream Using PHP

查看:130
本文介绍了使用PHP从音频流中提取轨道信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用PHP从音频流中提取曲目信息?我已经进行了一些挖掘,可以找到的最接近的函数是stream_get_transports,但是我的主机不支持通过fsockopen()进行http传输,因此,我将不得不做更多的工作来看看该函数还会返回什么.

Is it possible to pull track info from an audio stream using PHP? I've done some digging and the closest function I can find is stream_get_transports but my host doesn't support http transports via fsockopen() so I'll have to do some more tinkering to see what else that function returns.

当前,我正在尝试从AOL流中提取演出者并跟踪元数据.

Currently, I'm trying to pull artist and track metadata from an AOL stream.

推荐答案

这是一个SHOUTcast流,可以.它与ID3标签完全无关.我不久前写了一个脚本来做到这一点,但现在找不到了.就在上周,我帮助另一个拥有相当完整的脚本的人做同样的事情,但是我不能只是将源发布到它,因为它不是我的.但是,如果您通过 brad@musatcha.com 给我发送电子邮件,我会与您联系.

This is a SHOUTcast stream, and yes it is possible. It has absolutely nothing to do with ID3 tags. I wrote a script awhile ago to do this, but can't find it anymore. Just last week I helped another guy who had a fairly complete script to do the same thing, but I can't just post the source to it, as it isn't mine. I will however get you in touch with him, if you e-mail me at brad@musatcha.com.

无论如何,这是您自己执行的操作:

您需要做的第一件事是直接连接到服务器.不要使用HTTP.好吧,您可能会使用cURL,但是它可能比它的价值更麻烦.您可以使用fsockopen()( doc )连接到它.确保使用正确的端口.还要注意,许多Web主机会阻塞很多端口,但是您通常可以使用端口80.幸运的是,所有AOL托管的SHOUTcast流都使用端口80.

The first thing you need to do is connect to the server directly. Don't use HTTP. Well, you could probably use cURL, but it will likely be much more hassle than its worth. You connect to it with fsockopen() (doc). Make sure to use the correct port. Also note that many web hosts will block a lot of ports, but you can usually use port 80. Fortunately, all of the AOL-hosted SHOUTcast streams use port 80.

现在,像客户一样发出请求.

Now, make your request just like your client would.

GET /whatever HTTP/1.0

但是,在发送<CrLf><CrLf>之前,请包含下一个标头!

But, before sending <CrLf><CrLf>, include this next header!

Icy-MetaData:1

这告诉服务器您需要元数据.现在,发送您的<CrLf>对.

That tells the server that you want metadata. Now, send your pair of <CrLf>.

好的,服务器将响应一堆标题,然后开始向您发送数据.在那些标题中,将是一个icy-metaint:8192或类似名称. 8192是元间隔.这很重要,实际上是您唯一需要的价值.通常为8192,但并非总是如此,因此请确保实际读取此值!

Ok, the server will respond with a bunch of headers and then start sending you data. In those headers will be an icy-metaint:8192 or similar. That 8192 is the meta interval. This is important, and really the only value you need. It is usually 8192, but not always, so make sure to actually read this value!

基本上,这意味着您将获得8192字节的MP3数据,然后是一块meta,然后是8192字节的MP3数据,然后是一块meta.

Basically it means, you will get 8192 bytes of MP3 data and then a chunk of meta, followed by 8192 bytes of MP3 data, followed by a chunk of meta.

读取8192字节的数据(确保您未在​​此计数中包括标头),将其丢弃,然后读取下一个字节.该字节是元数据的第一个字节,并指示元数据有多长时间.取这个字节的值(带有ord()的实际字节( doc )),然后将其乘以16.结果是要读取的元数据字节数.将这些字节数读取到一个字符串变量中供您使用.

Read 8192 bytes of data (make sure you are not including the header in this count), discard them, and then read the next byte. This byte is the first byte of meta data, and indicates how long the meta data is. Take the value of this byte (the actual byte with ord() (doc)), and multiply it by 16. The result is the number of bytes to read for metadata. Read those number of bytes into a string variable for you to work with.

接下来,修剪此变量的值.为什么?因为该字符串的末尾用0x0填充(以使其均匀地适合16字节的倍数)和trim()(

Next, trim the value of this variable. Why? Because the string is padded with 0x0 at the end (to make it fit evenly into a multiple of 16 bytes), and trim() (doc) takes care of that for us.

您将看到类似这样的内容:

You will be left with something like this:

StreamTitle='Awesome Trance Mix - DI.fm';StreamUrl=''

我将让您选择解析方法.就我个人而言,我可能只是在;上分配了2个限制,但要提防包含;的标题.我不确定转义字符方法是什么.进行一些实验可以为您提供帮助.

I'll let you pick your method of choice for parsing this. Personally I'd probably just split with a limit of 2 on ;, but beware of titles that contain ;. I'm not sure what the escape character method is. A little experimentation should help you.

使用完服务器后,请不要忘记断开与服务器的连接!

Don't forget to disconnect from the server when you're done with it!

那里有很多SHOUTcast MetaData引用.这是一个很好的例子: http://www.smackfu.com/stuff/programming/shoutcast.html

There are lots of SHOUTcast MetaData references out there. This is a good one: http://www.smackfu.com/stuff/programming/shoutcast.html

这篇关于使用PHP从音频流中提取轨道信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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