解决方案 »

  1.   

    转帖(未经测试)php剪切或分割mp3文件的一个类并带实例
    类文件
    mp3_cut_class.php
    <?php
    /*
    作者:周春
    电子邮件:xxfaxy#qq.com #换成@
    微博:http://www.weibo.com/407497716
    */
    class mp3_cut{
    var $data_buffer=1048576;
    var $error_message;
    var $old_file;
    var $new_file;
    var $start_time;
    var $file_size;
    var $time_length;
    var $byte_per_second;
    var $input_data;
    var $output_data;
    var $type;
    function mp3_cut($old_file,$time_length,$new_file,$start_time,$type){
    $this->old_file=$old_file;
    $this->new_file=$new_file;
    $this->start_time=$start_time;
    $this->time_length=$time_length;
    $this->type=$type;
    }
    function cut(){
    $this->set_input_file_size();
    $this->byte_per_second();
    $this->open_input_mp3();
    $this->open_output_mp3();
    $this->make_new_mp3($this->type);
    $this->close_output_mp3();
    $this->close_input_mp3();
    }
    function set_input_file_size(){
    $this->file_size=@filesize($this->old_file);
    }
    function byte_per_second(){
    $this->byte_per_second=(integer)($this->file_size/$this->time_length);
    }
    function make_new_mp3($type){
    $start_position=$this->start_time*$this->byte_per_second;
    if($type==1){
    fseek($this->input_data,$start_position);
    while(!@feof($this->input_data)){
    @fwrite($this->output_data,@fread($this->input_data,$this->data_buffer));
    }
    }
    else{
    while(@ftell($this->input_data)<=((integer)$start_position)){
    @fwrite($this->output_data,@fread($this->input_data,($this->byte_per_second/2)));
    }
    }
    }
    function open_input_mp3(){
    if(file_exists($this->old_file)){
    $this->input_data=fopen($this->old_file,"r");
    $result=true;
    }
    else{
    $this->error("打开文件错误");
    $result=false;
    }
    return $result;
    }
    function close_input_mp3(){
    if(@fclose($this->input_data)){$result=true;}
    else{
    $this->error("操作输入文件错误");
    $result=false;
    }
    return $result;
    }
    function open_output_mp3(){
    $this->output_data=@fopen($this->new_file,"w");
    if($this->output_data){$result=true;}
    else{
    $this->error("创建文件错误");
    $result=false;
    }
    return $result;
    }
    function close_output_mp3(){
    if(@fclose($this->output_data)){$result=true;}
    else{
    $this->error("操作输出文件错误");
    $result=false;
    }
    return $result;
    }
    function error($error_message){
    $this->error_message.=$error_message."<br>";
    }
    }
    ?>
    例子文件
    index.php
    <?php
    require_once("mp3_cut_class.php");
    $mp3_cut=new mp3_cut("input.mp3",265,"output.mp3",120,0);
    //输入文件名,输入文件总时间,输出文件名,分割点时间,模式[0保存前段1保存后段]
    $mp3_cut->cut();
    echo $mp3_cut->error_message;
    ?>