PHP函数获取MP3持续时间

ram*_*ram 26 php mp3

是否有任何PHP功能将给我MP3持续时间.我看了ID 3功能,但我没有看到任何持续时间的东西,除此之外,id3是某种标签,它不会在所有MP3中,所以使用这将没有任何意义.

Ric*_*kew 30

这应该对你有用,请注意getduration函数:http://www.zedwood.com/article/127/php-calculate-duration-of-mp3


Tec*_*rat 26

安装getid3,但如果只需要持续时间,则可以删除除这些模块之外的所有模块:

  • module.audio.mp3.php
  • module.tag.id3v1.php
  • module.tag.apetag.php
  • module.tag.id3v2.php

使用以下代码访问持续时间:

$getID3 = new getID3;
$ThisFileInfo = $getID3->analyze($pathName);
$len= @$ThisFileInfo['playtime_string']; // playtime in minutes:seconds, formatted string
Run Code Online (Sandbox Code Playgroud)

Sourceforge获取它


awa*_*avi 8

您可以使用ffmpeg获取mp3或许多其他音频/视频文件的持续时间.

  1. 在服务器中安装ffmpeg.
  2. 确保你的php不限制php shell_exec.

        // 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;
    
        }
    
    Run Code Online (Sandbox Code Playgroud)


小智 8

我已经度过了这么多时间,但是没有getID3(http://getid3.sourceforge.net/)来获取音频文件的持续时间是不可能的.

1)首先使用以下链接下载getID3库:

 https://github.com/JamesHeinrich/getID3/archive/master.zip
Run Code Online (Sandbox Code Playgroud)

2)尝试以下代码:

    <?php
       include("getid3/getid3.php");
       $filename = 'bcd4ecc6bf521da9b9a2d8b9616d1505.wav';
       $getID3 = new getID3;
       $file = $getID3->analyze($filename);
       $playtime_seconds = $file['playtime_seconds'];
       echo gmdate("H:i:s", $playtime_seconds);
    ?>
Run Code Online (Sandbox Code Playgroud)