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

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

问题描述

是否可以使用 PHP 从音频流中提取曲目信息?我已经做了一些挖掘,我能找到的最接近的函数是 stream_get_transsports,但我的主机不支持通过 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

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

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 数据,然后是一个元数据块,然后是 8192 字节的 MP3 数据,然后是一个元数据块.

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() (doc) 为我们解决了这个问题.

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.

你会得到这样的东西:

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天全站免登陆