1.首先将七牛云的官方SDK放到TP框架的第三方类库。
2.注册七牛云账号,创建空间,绑定域名,在数据处理中建一个私有队列。
3.七牛云转码代码如下,参考七牛云官方文档
https://developer.qiniu.com/dora/manual/3685/directions-for-use-av:
// +----------------------------------------------------------------------
// | 七牛云
// +----------------------------------------------------------------------
namespace Purewechat\Controller;
use Think\Controller;
use Qiniu\Auth;
require_once VENDOR_PATH.'/Qiniu/autoload.php';
class QiniuController extends Controller {
private $paramter = array(
'accessKey'=> 'i6KwOSTE6LwvvmQs6vpdY13cSRBuNzm4LNU3g9Y9',
'secretKey'=> 'QtAtEYbj1K0A0S4Dn-_fIAE8gqj_o6ZbqRfAPSWO',
);
public function amrtomp3($filePath,$filename){
$accessKey = $this -> paramter['accessKey']; //七牛公钥
$secretKey = $this -> paramter['secretKey']; //七牛私钥
$auth = new Auth($accessKey, $secretKey);
$bucket_name = 'jucheng';//空间名
$key = $filename.'.amr'; //七牛云中保存的amr文件名
$pipeline = 'juchengcs';//数据处理队列名称,不设置代表不使用私有队列,使用公有队列。
//设置转码参数
$fops = "avthumb/mp3/ab/320k/ar/44100/acodec/libmp3lame";
//通过添加'|saveas'参数,指定处理后的文件保存的bucket和key 不指定默认保存在当前空间,bucket为目标空间,后一个参数为转码之后文件名
$saveas_key = \Qiniu\base64_urlSafeEncode($bucket_name.':'.$filename.'.mp3');
$fops = $fops.'|saveas/'.$saveas_key;
if(!empty($pipeline)){ //使用私有队列
$policy = array(
'persistentOps' => $fops,
'persistentPipeline' => $pipeline
);
}else{ //使用公有队列
$policy = array(
'persistentOps' => $fops
);
}
//指定上传转码命令
$uptoken = $auth->uploadToken($bucket_name, null, 3600, $policy);
//上传文件并转码$filePath为本地文件路径
$UploadManager = new \Qiniu\Storage\UploadManager();
list($ret, $err)= $UploadManager -> putFile($uptoken, $key, $filePath);
//dump($ret);dump($err);
if($err !== null){
return false;
}else{
//此时七牛云中同一段音频文件有amr和MP3两个格式的两个文件同时存在
$bucketMgr = new \Qiniu\Storage\BucketManager($auth);
$bucketMgr -> delete($bucket_name, $key);//为节省空间,删除amr格式文件
return $ret['key'];
}
}
}
4.在需要用的时候调用即可
//音频amr转码mp3 $dir为本地目录 $filename为文件名称
$Qiniu = A('Purewechat/Qiniu');
$Qiniu -> amrtomp3($dir,$filename);