技术资料首页 >> 程序员专区 >> PHP专栏 >> 用PHP读写音频文件的信息

用PHP读写音频文件的信息 (1)

2006-10-28 19:28:27作者: 来源:http://www.chinaunix.net/jh/27/891646.html 浏览次数:8 文字大小:【】【】【

花了不少时间和心思作了这个东西, 使用PHP去读取或修改 MP3或WMA文件的头信息(主要有专辑,歌名,歌手等) 

其实网上搜索有不少 MP3 的类似程序,但 WMA 的几乎没有,如果有也是windows平台下直接使用API的,想在 Unix/Linux 下使用简直没门。。。。

由于我的音乐站刚开通,需要这方面的功能,所以就下决心花时间去查资料搞清楚MP3和wma的数据头格式,全部代码由PHP编写,有兴趣可以仔细研究,或加以完善。

其实这东西技术上并没有难点,麻烦在于先要搞清楚它的结构和格式,这个代码也是一个使用PHP操作二进制(binary data)的例子,这几天群里刚好也有人问起这个,主要就是pack和unpack2个函数啦,呵呵,像MP3的原始头信息几乎是一个bit表示一个信息,这时就要用到大量的“按位操作”来分析它。

下面是源码照贴(含例子),呵,这也作为实习版主的一个小贡献吧,挺希望这里能见到一些原创并且对它人有用的代码(类库)出现。!!在此抛砖先了。 


<?php
// AudioExif.class.php
// 用PHP进行音频文件头部信息的读取与写入
// 目前只支持 WMA 和 MP3 两种格式, 只支持常用的几个头部信息
//
// 写入信息支持: Title(名称), Artist(艺术家), Copyright(版权), Description (描述)
//               Year(年代),  Genre (流派),   AlbumTitle (专辑标题)
// 其中 mp3 和 wma 略有不同, 具体返回的信息还可能更多, 但只有以上信息可以被写入
// mp3 还支持 Track (曲目编号写入)
// 对于 MP3 文件支持 ID3v1也支持ID3v2, 读取时优先 v2, 写入时总是会写入v1, 必要时写入v2
//
// 用法说明: (由于 wma 使用 Unicode 存取, 故还需要 mb_convert_encoding() 扩展
//           返回数据及写入数据均为 ANSI 编码, 即存什么就显示什么 (中文_GB2312)
//
// require ('AudioExif.class.php');
// $AE = new AudioExif;
// $file = '/path/to/test.mp3';
//
// 1. 检查文件是否完整 (only for wma, mp3始终返回 true)
// 
// $AE->CheckSize($file);
//
// 2. 读取信息, 返回值由信息组成的数组, 键名解释参见上方
//
// print_r($AE->GetInfo($file));
//
// 3. 写入信息, 第二参数是一个哈希数组, 键->值, 支持的参见上方的, mp3也支持 Track
//    要求第一参数的文件路径可由本程序写入
// $pa = array('Title' => '新标题', 'AlbumTitle' => '新的专辑名称');
// $AE->SetInfo($file, $pa);
//
// 版本: 0.1
// 作者: hightman
// QQ群: 17708754  (非纯PHP进阶交流群)
// 时间: 2007/01/25
// 其它: 该插件花了不少时间搜集查找 wma及mp3 的文件格式说明文档与网页, 希望对大家有用.
//       其实网上已经有不少类似的程序, 但对 wma 实在太少了, 只能在 win 平台下通过 M$ 的
//       API 来操作, 而 MP3 也很少有可以在 unix/linux 命令行操作的, 所以特意写了这个模块
//
// 如果发现 bug 或提交 patch, 或加以改进使它更加健壮, 请告诉我. 
// (关于 ID3和Wma的文件格式及结构 在网上应该都可以找到参考资料)
//

if (!extension_loaded('mbstring'))
{
  trigger_error('PHP Extension module `mbstring` is required for AudioExif', E_USER_WARNING);
  return true;
}

// the Main Class
class AudioExif
{
  // public vars
  var $_wma = false;
  var $_mp3 = false;

  // Construct
  function AudioExif()
  {
    // nothing to do
  }

  // check the filesize
  function CheckSize($file)
  {
    $handler = &$this->_get_handler($file);
    if (!$handler) return false;
    return $handler->check_size($file);
  }

  // get the infomations
  function GetInfo($file)
  {
    $handler = &$this->_get_handler($file);
    if (!$handler) return false;
    return $handler->get_info($file);
  }

  // write the infomations
  function SetInfo($file, $pa)
  {
    if (!is_writable($file))
    {
      trigger_error('AudioExif: file `' . $file . '` can not been overwritten', E_USER_WARNING);
      return false;
    }
    $handler = &$this->_get_handler($file);
    if (!$handler) return false;
    return $handler->set_info($file, $pa);
  }

  // private methods
  function &_get_handler($file)
  {
    $ext = strtolower(strrchr($file, '.'));
    $ret = false;
    if ($ext == '.mp3')
    {  // MP3
      $ret = &$this->_mp3;
      if (!$ret) $ret = new _Mp3Exif();
    }
    else if ($ext == '.wma')
    {  // wma
      $ret = &$this->_wma;
      if (!$ret) $ret = new _WmaExif();
    }
    else
    {  // unknown
      trigger_error('AudioExif not supported `' . $ext . '` file.', E_USER_WARNING);
    }
    return $ret;
  }
}

// DBCS => gb2312
function dbcs_gbk($str)
{
  // strip the last "\0\0"
  $str = substr($str, 0, -2);
  return mb_convert_encoding($str, 'GBK', 'UCS-2LE');
}

// gb2312 => DBCS
function gbk_dbcs($str)
{
  $str  = mb_convert_encoding($str, 'UCS-2LE', 'GBK');
  $str .= "\0\0";
  return $str;
}

// file exif
class _AudioExif
{
  var $fd;
  var $head;
  var $head_off;
  var $head_buf;
  
  // init the file handler
  function _file_init($fpath, $write = false)
  {
    $mode = ($write ? 'rb+' : 'rb');
    $this->fd = @fopen($fpath, $mode);
    if (!$this->fd)
    {
      trigger_error('AudioExif: `' . $fpath . '` can not be opened with mode `' . $mode . '`', E_USER_WARNING);
      return false;
    }
    $this->head = false;
    $this->head_off = 0;
    $this->head_buf = '';
    return true;
  }

  // read buffer from the head_buf & move the off pointer
  function _read_head_buf($len)
  {
    if ($len <= 0) return NULL;
    $buf = substr($this->head_buf, $this->head_off, $len);
    $this->head_off += strlen($buf);
 
[1] [2] [3] [4] [5] [6] [7]

相关文章