PHP Function to get MP3 duration

后端 未结 11 2316
终归单人心
终归单人心 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:12
    $getID3 = new getID3;
    $ThisFileInfo = $getID3->analyze($pathName);
    
    // playtime in minutes:seconds, formatted string
    $len = @$ThisFileInfo['playtime_string']; 
    
    //don't get playtime_string, but get playtime_seconds
    $len = @$ThisFileInfo['playtime_seconds']*1000; //*1000 as calculate millisecond
    

    I hope this helps you.

    0 讨论(0)
  • 2020-11-27 18:13

    Finally, I developed a solution with my own calculations. This solution works best for mp3 and WAV files formats. However minor precision variations are expected. The solution is in PHP. I take little bit clue from WAV

    function calculateFileSize($file){
    
        $ratio = 16000; //bytespersec
    
        if (!$file) {
    
            exit("Verify file name and it's path");
    
        }
    
        $file_size = filesize($file);
    
        if (!$file_size)
            exit("Verify file, something wrong with your file");
    
        $duration = ($file_size / $ratio);
        $minutes = floor($duration / 60);
        $seconds = $duration - ($minutes * 60);
        $seconds = round($seconds);
        echo "$minutes:$seconds minutes";
    
    }
    
    $file = 'apple-classic.mp3'; //Enter File Name mp3/wav
    calculateFileSize($file);
    
    0 讨论(0)
  • 2020-11-27 18:14

    The MP3 length is not stored anywhere (in the "plain" MP3 format), since MP3 is designed to be "split" into frames and those frames will remain playable.

    http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm

    If you have no ID tag on which to rely, what you would need to do (there are tools and PHP classes that do this) is to read the whole MP3 file and sum the durations of each frame.

    0 讨论(0)
  • 2020-11-27 18:15

    This should work for you, notice the getduration function: http://www.zedwood.com/article/127/php-calculate-duration-of-mp3

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题