问题描述:

cscms 4.x上传音频文件获取不到相关的音频、大小、时长等参数;

于是找了一圈,发现七牛有个专门获取相关文件参数的接口,这就很好办事了;

解决办法:

上传音频文件的入口合适位置加入如下代码:

$avinfo = htmlall($music['durl'].'?avinfo');
$avinfo = json_decode($avinfo, true);
$mp3yz = $avinfo['format']['bit_rate']/1000;
if($mp3yz>=257)$mp3yz2='320 Kbps';
if($mp3yz>=193 && $mp3yz<257)$mp3yz2='256 Kbps';
if($mp3yz>=129 && $mp3yz<193)$mp3yz2='192 Kbps';
if($mp3yz<129)$mp3yz2='128 Kbps';
$mp3sc2 = sprintf("%d:%02d", ($avinfo['format']['duration'] /60), $avinfo['format']['duration'] %60 );
        
$music['dx']=formatsize($avinfo['format']['size']);
$music['yz']=$mp3yz2;
$music['sc']=$mp3sc2;

 

 

新加一个批量获取七牛音频文件的方法(此方法需要给歌曲表新增一个 music_info的字段):

	public function get_music_info($pass = ''){
		
        //获取数据
		$sql = 'select * from '.CS_SqlPrefix.'dance where music_info=0 order by id desc';
	    $res = $this->db->query($sql)->result();
		foreach ($res as $key => $value) {
			$durl=$value->durl;
			if(substr($durl,0,1)=='/')$durl=substr($durl,1);
    		if($value->fid>0){
    			$rowf=$this->db->query("Select purl from ".CS_SqlPrefix."dance_server where id=".$value->fid."")->row_array();
    			if($rowf){
    			    $durl=$rowf['purl'].$value->durl;
    			}
    		}
    		$durl=annexlink($durl);
    		//将中文编码下载  七牛这里一定要记得编码中文部分,否则会400报错
	   	$durl = rawurlencode($durl);
		$durl = str_replace('%3A',':',$durl);
		$durl = str_replace('%2F','/',$durl);

			//获取七牛的参数
    		$avinfo = htmlall($durl.'?avinfo');
    		$avinfo = json_decode($avinfo, true);
    		$mp3yz = $avinfo['format']['bit_rate']/1000;
    		if($mp3yz>=257)$mp3yz2='320 Kbps';
    		if($mp3yz>=193 && $mp3yz<257)$mp3yz2='256 Kbps';
    		if($mp3yz>=129 && $mp3yz<193)$mp3yz2='192 Kbps';
    		if($mp3yz<129)$mp3yz2='128 Kbps';
    		$mp3sc2 = sprintf("%d:%02d", ($avinfo['format']['duration'] /60), $avinfo['format']['duration'] %60 );
            
    		$music['dx']=formatsize($avinfo['format']['size']);
            $music['yz']=$mp3yz2;
            $music['sc']=$mp3sc2;
            $music['music_info']=1;
            echo $value->id.'、'.$value->name.' 大小:'.$music['dx'].' 音质:'.$music['yz'].' 时长:'.$music['sc'].' ,音频参数生成成功~!<br>';
            $this->Csdb->get_update('dance',$value->id,$music);
		}
	}

 

评论 (0)

请登录

嘿,我来帮您