PHP Function to get MP3 duration

后端 未结 11 2338
终归单人心
终归单人心 2020-11-27 17:35

Is there any PHP function that will give me the MP3 duration. I looked at ID 3 function but i don\'t see any thing there for duration and apart from this,id3 is some kind of

11条回答
  •  清酒与你
    2020-11-27 18:16

    As earlier, I provided a solution for both mp3 and WAV files, Now this solution is specifically for the only WAV file with more precision but with longer evaluation time than the earlier solution.

    function calculateWavDuration( $file ) {
    
        $fp = fopen($file, 'r');
    
        if (fread($fp, 4) == "RIFF") {
    
            fseek($fp, 20);
            $raw_header = fread($fp, 16);
            $header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits', $raw_header);
            $pos = ftell($fp);
    
            while (fread($fp, 4) != "data" && !feof($fp)) {
    
                $pos++;
                fseek($fp, $pos);
    
            }
    
            $raw_header = fread($fp, 4);
            $data = unpack('Vdatasize', $raw_header);
            $sec = $data[datasize] / $header[bytespersec];
            $minutes = intval(($sec / 60) % 60);
            $seconds = intval($sec % 60);
    
            return str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":" . str_pad($seconds, 2, "0", STR_PAD_LEFT);
    
        }
    
    }
    
    $file = '1.wav'; //Enter File wav
    calculateWavDuration($file);
    

提交回复
热议问题