PHP Function to get MP3 duration

后端 未结 11 2449
终归单人心
终归单人心 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:09

    You can get the duration of an mp3 or many other audio/video files by using ffmpeg.

    1. Install ffmpeg in your server.
    2. Make sure that php shell_exec is not restricted in your php.

          // Discriminate only the audio/video files you want
          if(preg_match('/[^?#]+\.(?:wma|mp3|wav|mp4)/', strtolower($file))){
              $filepath = /* your file path */;
              // execute ffmpeg form linux shell and grab duration from output
              $result = shell_exec("ffmpeg -i ".$filepath.' 2>&1 | grep -o \'Duration: [0-9:.]*\'');
              $duration = str_replace('Duration: ', '', $result); // 00:05:03.25
      
              //get the duration in seconds
              $timeArr = preg_split('/:/', str_replace('s', '', $duration[0]));
              $t = $this->_times[$file] = (($timeArr[3])? $timeArr[3]*1 + $timeArr[2] * 60 + $timeArr[1] * 60 * 60 : $timeArr[2] + $timeArr[1] * 60)*1000;
      
          }
      

提交回复
热议问题