3

我是 PHP 新手。我想将 mp3 文件合并到一个 mp3 文件中。我用谷歌搜索了这个查询并找到了这段代码。它工作正常,把合并的文件还给我。但是这段代码给了我合并的文件作为保存文件。但我想将合并的文件保存在一个文件夹中。这样我就可以将文件的 URL 提供给某个应用程序。

class mp3{
    var $str;
    var $time;
    var $frames;

    // Create a new mp3
    function mp3($path="")
    {
    if($path!="")
        {
        $this->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;$i<$strlen;$i++){
            $v = substr($this->str,$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('<strong>audio file error: </strong>'.$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'){
            //We send to a browser
            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 '';
    }
}

 // First File: (Google speech)
$mp3 = new mp3('1.mp3');
$mp3->striptags();

 //Second file
$second = new mp3("2.mp3");
$mp3->mergeBehind($second);
$mp3->striptags();

$mp3->output('word.mp3'); //Output file (current a blank file)

解决方案代码将非常感谢..在此先感谢

4

1 回答 1

2

将此方法添加到您的 mp3 类中。

// Save the new mp3 into the file system
function savefile($path){
    return file_put_contents($path, $this->str);
}

然后简单地使用它......替换

$mp3->output('word.mp3'); //Output file (current a blank file)

有了这个

$mp3->savefile('/path/to/directory/file.mp3');

并确保修改文件系统中真实目录的路径。

于 2013-01-18T06:49:47.283 回答