PHP开发

解析MP3等媒体文件的超强php类-getid3

getID3()是一款从MP3等媒体文件中提取文件信息的php类,既可以提取也能修改文件的标签信息。它能读取的文件类型非常多,例如MP3、wav、midi等,这里就不一一列举了,详细版本和可读取的文件类型可以参考官方网站http://getid3.sourceforge.net/

这里举一个获取mp3文件信息的例子

include “library/getid3/getid3.php”;   //引入所需要的类文件

$filename =’myfile.mp3’; //要处理的文件名

$path=’http://localhost/myfolder’; //文件路径

$radio = realpath($path).DIRECTORY_SEPARATOR.$filename;//获取文件完整路径和名称,注意这里要使用绝对路径, 不是http那种绝对路径,而是服务器的,如果是linux服务器,那么路径应该是类似/home/myaccount/public_html /myfolder/myfile.mp3,如果是windows服务器,可能是C:\myfolder\…,注意linux和windows使用的 斜杠不同,这也就是DIRECTORY_SEPARATOR的用处,realpath函数产生的路径是根据服务器的情况来选择反斜杠的。

$getID3 = new getID3;//创建一个类的实例

$ThisFileInfo = $getID3->analyze($radio);//分析文件

$time = $ThisFileInfo[‘playtime_seconds’];//获取mp3的长度信息

下载getid3 >>  [download id=”2″]

下面是我获取的本地MP3文件信息,打印一下$ThisFileInfo数组,可以根据需要提取需要的信息

Array

(

    [GETID3_VERSION] => 1.7.9-20090308

    [filesize] => 4577408

    [avdataoffset] => 1463

    [avdataend] => 4577280

    [fileformat] => mp3

    => Array

        (

            [dataformat] => mp3

            [channels] => 2

            [sample_rate] => 44100

            [bitrate] => 128000

            [channelmode] => stereo

            [bitrate_mode] => cbr

            [lossless] =>

            [encoder_options] => CBR128

            [compression_ratio] => 0.090702947845805

            [streams] => Array

                (

                    [0] => Array

                        (

                            [dataformat] => mp3

                            [channels] => 2

                            [sample_rate] => 44100

                            [bitrate] => 128000

                            [channelmode] => stereo

                            [bitrate_mode] => cbr

                            [lossless] =>

                            [encoder_options] => CBR128

                            [compression_ratio] => 0.090702947845805

                        )

 

                )

 

        )

 

    [tags] => Array

        (

            [id3v1] => Array

                (

                    [title] => Array

                        (

                            [0] => 逃亡

                        )

 

                    [artist] => Array

                        (

                            [0] => 孙燕姿

                        )

 

                    [album] => Array

                        (

                            [0] => 风筝

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [comment] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [track] => Array

                        (

                            [0] => 4

                        )

 

                )

 

            [id3v2] => Array

                (

                    [url_user] => Array

                        (

                            [0] => http://www.cy2002.com

                        )

 

                    [comments] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [album] => Array

                        (

                            [0] => 风筝

                        )

 

                    [artist] => Array

                        (

                            [0] => 孙燕姿

                        )

 

                    [title] => Array

                        (

                            [0] => 逃亡

                        )

 

                    [track_number] => Array

                        (

                            [0] => 4

                        )

 

                )

 

        )

 

    [encoding] => ISO-8859-1

    [filename] => CCD3CDF6_2.mp3

    [filepath] => E:/wamp/www/marketing/uploads/radioads

    [filenamepath] => E:/wamp/www/marketing/uploads/radioads/CCD3CDF6_2.mp3

    [id3v2] => Array

        (

            [header] => 1

            [flags] => Array

                (

                    [unsynch] =>

                    [exthead] =>

                    [experim] =>

                )

 

            [majorversion] => 3

            [minorversion] => 0

            [headerlength] => 1463

            [tag_offset_start] => 0

            [tag_offset_end] => 1463

            [encoding] => UTF-8

            [comments] => Array

                (

                    [url_user] => Array

                        (

                            [0] => http://www.cy2002.com

                        )

 

                    [comments] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [album] => Array

                        (

                            [0] => 路莽贸脻

                        )

 

                    [artist] => Array

                        (

                            [0] => 脣茂脩脿脳脣

                        )

 

                    [title] => Array

                        (

                            [0] => 脤脫脥枚

                        )

 

                    [track_number] => Array

                        (

                            [0] => 4

                        )

 

                )

 

            [WXXX] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => WXXX

                            [frame_flags_raw] => 0

                            [datalength] => 23

                            [dataoffset] => 10

                            [framenamelong] => User defined URL link frame

                            [framenameshort] => url_user

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                            [url] => http://www.cy2002.com

                            [description] =>

                        )

 

                )

 

            [COMM] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => COMM

                            [frame_flags_raw] => 0

                            [data] => http://51music.dhs.org

                            [datalength] => 27

                            [dataoffset] => 43

                            [framenamelong] => Comments

                            [framenameshort] => comments

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                            [language] => eng

                            [languagename] => English

                            [description] =>

                        )

 

                ) 

            [TYER] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => TYER

                            [frame_flags_raw] => 0

                            [data] => 2001

                            [datalength] => 5

                            [dataoffset] => 80

                            [framenamelong] => Year

                            [framenameshort] => year

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                        )

 

                ) 

            [TALB] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => TALB

                            [frame_flags_raw] => 0

                            [data] => 风筝

                            [datalength] => 5

                            [dataoffset] => 95

                            [framenamelong] => Album/Movie/Show title

                            [framenameshort] => album

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                        )

 

                ) 

            [TPE1] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => TPE1

                            [frame_flags_raw] => 0

                            [data] => 孙燕姿

                            [datalength] => 7

                            [dataoffset] => 110

                            [framenamelong] => Lead performer(s)/Soloist(s)

                            [framenameshort] => artist

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                        )

 

                ) 

            [TIT2] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => TIT2

                            [frame_flags_raw] => 0

                            [data] => 逃亡

                            [datalength] => 5

                            [dataoffset] => 127

                            [framenamelong] => Title/songname/content description

                            [framenameshort] => title

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                        )

 

                ) 

            [TRCK] => Array

                (

                    [0] => Array

                        (

                            [frame_name] => TRCK

                            [frame_flags_raw] => 0

                            [data] => 4

                            [datalength] => 2

                            [dataoffset] => 142

                            [framenamelong] => Track number/Position in set

                            [framenameshort] => track_number

                            [flags] => Array

                                (

                                    [TagAlterPreservation] =>

                                    [FileAlterPreservation] =>

                                    [ReadOnly] =>

                                    [compression] =>

                                    [Encryption] =>

                                    [GroupingIdentity] =>

                                )

 

                            [encodingid] => 0

                            [encoding] => ISO-8859-1

                        )

 

                ) 

            [padding] => Array

                (

                    [start] => 154

                    [length] => 1309

                    [valid] => 1

                )

 

        ) 

    [id3v1] => Array

        (

            [title] => 逃亡

            [artist] => 孙燕姿

            [album] => 风筝

            [year] => 2001

            [comment] => http://51music.dhs.org

            [track] => 4

            [comments] => Array

                (

                    [title] => Array

                        (

                            [0] => 逃亡

                        )

 

                    [artist] => Array

                        (

                            [0] => 孙燕姿

                        )

 

                    [album] => Array

                        (

                            [0] => 风筝

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [comment] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [track] => Array

                        (

                            [0] => 4

                        )

 

                )

 

            [padding_valid] => 1

            [tag_offset_end] => 4577408

            [tag_offset_start] => 4577280

            [encoding] => ISO-8859-1

        )

 

    [mime_type] => audio/mpeg

    [mpeg] => Array

        (

            => Array

                (

                    [raw] => Array

                        (

                            [synch] => 4094

                            [version] => 3

                            [layer] => 1

                            [protection] => 1

                            [bitrate] => 9

                            [sample_rate] => 0

                            [padding] => 1

                            [private] => 0

                            [channelmode] => 1

                            [modeextension] => 0

                            [copyright] => 0

                            [original] => 0

                            [emphasis] => 0

                        )

 

                    [version] => 1

                    [layer] => 3

                    [channelmode] => joint stereo

                    [channels] => 2

                    [sample_rate] => 44100

                    [protection] =>

                    [private] =>

                    [modeextension] =>

                    [copyright] =>

                    [original] =>

                    [emphasis] => none

                    [padding] => 1

                    [bitrate] => 128000

                    [framelength] => 418

                    [bitrate_mode] => cbr

                )

 

        )

 

    [playtime_seconds] => 285.9885625

    [tags_html] => Array

        (

            [id3v1] => Array

                (

                    [title] => Array

                        (

                            [0] => ÌÓÍö

                        )

 

                    [artist] => Array

                        (

                            [0] => ËïÑà×Ë

                        )

 

                    [album] => Array

                        (

                            [0] => ·çóÝ

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [comment] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [track] => Array

                        (

                            [0] => 4

                        )

 

                )

 

            [id3v2] => Array

                (

                    [url_user] => Array

                        (

                            [0] => http://www.cy2002.com

                        )

 

                    [comments] => Array

                        (

                            [0] => http://51music.dhs.org

                        )

 

                    [year] => Array

                        (

                            [0] => 2001

                        )

 

                    [album] => Array

                        (

                            [0] => ·çóÝ

                        )

 

                    [artist] => Array

                        (

                            [0] => ËïÑà×Ë

                        )

 

                    [title] => Array

                        (

                            [0] => ÌÓÍö

                        )

 

                    [track_number] => Array

                        (

                            [0] => 4

                        )

 

                )

 

        )

 

    [bitrate] => 128000

    [playtime_string] => 4:46

)