顶部导航开始 -->

获取音频文件信息

    /**
     * @param $filename 音乐路径
     * @param $file_path 生成的封面图片存放路径
     * @param $mp3name  音乐名
     * @return array
     * author maritni
     */
    public function mp3info($filename,$file_path,$mp3name){
        //封面图片
        $getID3 = new \getID3();
        $ThisFileInfo = $getID3 -> analyze($filename);
        $cover      = null;
        $artist     = '';
        $title      = '';
        $album      = '';
        $bitrate    = '';
        $playtime   = '';
        $cover_name = '';
        $thumb_name = '';
        if(isset($getID3 -> info['id3v2'])){
            $minfo  =   $getID3 -> info['id3v2'];
            $artist =   isset($minfo['comments']['artist']) ? $minfo['comments']['artist'][0] : '';
            $title  =   isset($minfo['comments']['title']) ? $minfo['comments']['title'][0] : '';
            $album  =   isset($minfo['comments']['album']) ? $minfo['comments']['album'][0] : '';
            //封面信息
            if(isset($getID3 -> info['id3v2']['APIC'][0]['data'])){
                $cover = $getID3 -> info['id3v2']['APIC'][0]['data'];
            }elseif (isset($getID3 -> info['id3v2']['PIC'][0]['data'])){
                $cover = $getID3 -> info['id3v2']['PIC'][0]['data'];
            }else{
                $cover = null;
            }
            if(isset($getID3 -> info['id3v2']['APIC'][0]['image_mime'])) {
                $mimetype = $getID3 -> info['id3v2']['APIC'][0]['image_mime'];
            }else{
                $mimetype = 'image/jpeg';
            }
        }
        //将封面信息,存成图片和缩略图文件
        if(!is_null($cover)){
            $im = imagecreatefromstring($cover);
            $newwidth = $newheight = 50;
            $thumb = imagecreatetruecolor($newwidth, $newheight);
            if($im !== false){
                //输出或保存图片
                $fileName = $file_path.$mp3name.'_cover.jpeg';
                imagealphablending($im, false); //关闭图像的混色模式,
                imagesavealpha($im, true);//设置保存 PNG 图像时保存完整的 alpha 通道信息
                //生成图片,输出
                $resp = imagejpeg($im, $fileName);
                //调整图片大小
                imagealphablending($im, false); //关闭图像的混色模式,
                imagesavealpha($im, true);//设置保存 PNG 图像时保存完整的 alpha 通道信息
                $source = imagecreatefromjpeg($fileName);
                imagealphablending($source, true); //开启图像的混色模式
                list($width, $height, $type, $attr) = getimagesize($fileName);
                imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); //重采样拷贝部分图像并调整大小
                $newFilename = $file_path.$mp3name.'_thumb.jpeg';
                $resp = imagejpeg($thumb, $newFilename);
                //从内存中释放图像
                imagedestroy($im);
                imagedestroy($thumb);
                $cover_name = $fileName;
                $thumb_name = $newFilename;
            }
        }
        // 获取歌曲品质比特率
        if(isset($getID3 -> info['audio'])){
            $bt = $getID3 -> info['audio'];
            $bitrate  =  isset($bt['bitrate']) ? $bt['bitrate'] : '';
        }
        // 获取歌曲的时长
        if(isset($getID3 -> info['playtime_seconds'])){
            $playtime =  $getID3 -> info['playtime_seconds'];
        }
        return $MP3info = [
            'cover' => $cover,
            'artist' => $artist,
            'title' => $title,
            'album' => $album,
            'bitrate' =>$bitrate,
            'playtime' =>$playtime,
            'cover_name' => $cover_name,
            'thumb_name' =>$thumb_name];
    }


自学php博客
请先登陆后发表评论
  • 最新评论
  • 总共0条评论
  • © 2014-2016 zixuephp.cn 版权所有
  • ICP证:皖ICP备16012107号
  • 联系邮箱:510082587@qq.com
  • 交流请加微信:weibobf2899 或扫下方二维码