php 调用mp3,使用PHP合并MP3文件的类,兼容php4、php5(2)

}

function getIdvEnd()

{

$strlen = strlen($this->str);

$str = substr($this->str,($strlen-128));

$str1 = substr($str,0,3);

if(strtolower($str1) == strtolower(‘TAG’))

{

return $str;

}

else

{

return false;

}

}

function getStart()

{

$strlen = strlen($this->str);

for($i=0;$i

{

$v = substr($this->str,$i,1);

$value = ord($v);

if($value == 255)

{

return $i;

}

}

}

function striptags()

{

//Remove start stuff…

$newStr = ”;

$s = $start = $this->getStart();

if($s===false)

{

return false;

}

else

{

$this->str = substr($this->str,$start);

}

//Remove end tag stuff

$end = $this->getIdvEnd();

if($end!==false)

{

$this->str = substr($this->str,0,(strlen($this->str)-129));

}

}

function save($path)

{

$fp = fopen($path,’w’);

fwrite($fp,$this->str);

fclose($fp);

}

//join various MP3s

function multiJoin($newpath,$array)

{

foreach ($array as $path)

{

$mp3 = new mp3($path);

$mp3->striptags();

$mp3_1 = new mp3($newpath);

$mp3->mergeBehind($mp3_1);

$mp3->save($newpath);

}

}

}

?>

在这里,我们这样使用这个MP3的类:

//Merge two files

$path = ‘path.mp3′; //要合并的文件1

$path1 = ‘path1.mp3′; //要合并的文件2

$mp3 = new mp3($path);

$newpath = ‘path.mp3′;

$mp3->striptags();

$mp3_1 = new mp3($path1);

$mp3->mergeBehind($mp3_1);

$mp3->striptags();

$mp3->setIdv3_2(’01’,’Track Title’,’Artist’,’Album’,’Year’,’Genre’,’Comments’,’Composer’,’OrigArtist’,

‘Copyright’,’url’,’encodedBy’);

$mp3->save($newpath);

//Extract 30 seconds starting after 10 seconds.

$path = ‘path.mp3′;

$mp3 = new mp3($path);

$mp3_1 = $mp3->extract(10,30);

$mp3_1->save(‘newpath.mp3′);

//Extract the exact length of time

$path = ‘path.mp3′;

$mp3 = new mp3($path);

$mp3->setFileInfoExact();

echo $mp3->time;

?>

由于这个类可能会存在于执行时间较长,无法显示所有的MP3的长度,很慢、占用很多内存的缺点。根据本人亲测,有如下修改版本可以使用:

str = file_get_contents($path);

}

}// Put an mp3 behind the first mp3

function mergeBehind($mp3)

{

$this->str .= $mp3->str;

}// Calculate where’s the end of the sound file

function getIdvEnd()

{

$strlen = strlen($this->str);

$str = substr($this->str,($strlen-128));

$str1 = substr($str,0,3);

if(strtolower($str1) == strtolower(‘TAG’))

{

return $str;

}

else

{

return false;

}

}// Calculate where’s the beginning of the sound file

function getStart()

{

$strlen = strlen($this->str);

for($i=0;$istr,$i,1);

$value = ord($v);

if($value == 255)

{

return $i;

}

}

}// Remove the ID3 tags

function striptags()

{

//Remove start stuff…

$newStr = ”;

$s = $start = $this->getStart();

if($s===false)

{

return false;

}

else

{

$this->str = substr($this->str,$start);

}

//Remove end tag stuff

$end = $this->getIdvEnd();

if($end!==false)

{

$this->str = substr($this->str,0,(strlen($this->str)-129));

}

}

// Display an error

function error($msg)

{

//Fatal error

die(‘audio file error: ‘.$msg);

}

// Send the new mp3 to the browser

function output($path)

{

//Output mp3

//Send to standard output

if(ob_get_contents())

$this->error(‘Some data has already been output, can\’t send mp3 file’);

if(php_sapi_name()!=’cli’)

{

//合并成功后提供下载

header(‘Content-Type: audio/mpeg3′);

if(headers_sent())

$this->error(‘Some data has already been output to browser, can\’t send mp3 file’);

header(‘Content-Length: ‘.strlen($this->str));

header(‘Content-Disposition: attachment; filename=”‘.$path.'”‘);

}

echo $this->str;

return ”;

}

}

?>

我们可以这样使用这个新的类:

$petersword = "cba";//这里可以合成2个、三个甚至更多,如本例,可以合成a.mp3、b.mp3、c.mp3三个音频文件为word.mp3

$word_count = strlen($petersword);

if ($word_count > 0) {

$mp3 = new mp3($cas_fontpath . 'sounds/' . substr($petersword, 0, 1) . '.mp3');

$mp3->striptags();

}

// 合成这三个mp3文件于word.mp3中

for ($i = 1; $i < $word_count; ++$i) {

$cas_character = $cas_fontpath . 'sounds/' . substr($petersword, $i, 1);

$cas_mp3equivalent = new mp3($cas_character . '.mp3');

$mp3->mergeBehind($cas_mp3equivalent);

$mp3->striptags();

}

$mp3->output('word.mp3');

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值