获取音频MP3长度

这篇博客主要介绍了如何通过后台代码来获取本地MP3音频文件的长度,适用于需要处理音频信息的场景。
摘要由CSDN通过智能技术生成

后台代码(只支持本地文件)

//获取音频文件时长star
    		include_once 'get_mp3_time.inc';
    		$filename = $showphpath;
    		//     	$filename = 'test.mp3';
    		$mp3 = new MP3File($filename);
    		$a = $mp3->getDurationEstimate();
    		$b = $mp3->getDuration();
    		$duration = $mp3::formatTime($b);
    		$audio_time = 0;//时长(s)
    		if(count($duration)>0){
    			$audio_time = ceil($duration['hours']*3600+$duration['minutes']*60+$duration['seconds']);
    		}
    		//获取音频文件时长end

 

 

获取长度类

<?php 
class MP3File
{
    protected $filename;
    public function __construct($filename)
    {
        $this->filename = $filename;
    }
 
    public static function formatTime($duration) //as hh:mm:ss
    {
        //return sprintf("%d:%02d", $duration/60, $duration%60);
        $hours = floor($duration / 3600);
        $minutes = floor( ($duration - ($hours * 3600)) / 60);
        $seconds = $duration - ($hours * 3600) - ($minutes * 60);
        // return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
        return array('hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$seconds);
    }
 
    //Read first mp3 frame only...  use for CBR constant bit rate MP3s
    public function getDurationEstimate()
    {
        return $this->getDuration($use_cbr_estimate=true);
    }
 
    //Read entire file, frame by frame... ie: Variable Bit Rate (VBR)
    public function getDuration($use_cbr_estimate=false)
    {
        $fd = fopen($this->filename, "rb");
 
        $du
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值