这篇文章还是属于对七牛云PHP SDK的使用,当我们下载了SDK后,examples目录下有非常多的例子,看这个来进行学习进步也是很快的。
今天记录一下对视频转码的操作,当然对音频也是有效的。七牛云的转码操作有两种,第一种叫做预转持久化,它是当我们上传一个视频的时候,通过上传策略设置对这个视频的转码,成功后会保留原始格式和转码后的格式;第二种叫做触发持久化,这种方式是对已经上传到七牛云的视频,通过空间名和文件名进行转码。
点击这里查看官方文档及实例
撸代码前,先到七牛云后台添加一个多媒体队列:控制台 ==》多媒体队列,然后设置名称,这里我创建了名为“line1”的队列。
下面的例子场景:操作一,前台上传一个视频文件,上传后自动将文件转码为AVI格式(预转持久化),转码成功后通过回调删除原视频。操作二:通过已经存在于七牛云空间的视频的名称(刚刚的那个AVI格式视频)来进行MP4转码(触发持久化),转码成功后,通过回调删除原视频。
前端index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin-bottom: 30px">
<span style="font-size: 30px">预转持久化转码</span>
<form action="{:url('Index/Index/yuzhuan')}" method="post" enctype="multipart/form-data">
请上传文件:<input type="file" name="video"> <br>
<input type="submit" name="提交">
</form>
</div>
<div>
<span style="font-size: 30px">触发持久化转码</span>
<form action="{:url('Index/Index/chufa')}" method="post">
填写要转码的文件名:<input type="text" name="file_name" > <br/>
<input type="submit" name="提交">
</form>
</div>
</body>
</html>
Index.php
<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
public function index()
{
return $this -> fetch();
}
//预转持久化
public function yuzhuan()
{
if(request() -> isPost()){
$file = request()->file('video');
$file_info = $file -> getInfo();
$qiniu = new UploadFile();
$res = $qiniu -> uploadOne($file_info);
dump($res);
}else{
return $this -> fetch();
}
}
//触发持久化
public function chufa()
{
$file_name = input('post.file_name');
$qiniu = new UploadFile2();
$res = $qiniu -> uploadOne2($file_name);
}
}
UploadFile.php
<?php
namespace app\index\controller;
use Qiniu\Storage\BucketManager;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class UploadFile extends Controller
{
protected $domain;
protected $bucket;
protected $token;
protected $accessKey;
protected $secretKey;
public function __construct() {
$this -> domain = '空间对应域名';
$this -> bucket = '空间名';
$this -> accessKey = 'AK';
$this -> secretKey = 'SK';
}
/**
* 上传并通过预转持久化进行转码
* @param array $file 视频参数
* @return array
*/
public function uploadOne($file) {
//转码时使用的队列名称,在后台多媒体队列添加
$pipeline = 'line1';
//转码操作,设置转码参数 https://developer.qiniu.com/dora/api/1248/audio-and-video-transcoding-avthumb
$fops = "avthumb/avi/s/640x360/vb/1.4m";
//可以对转码后的文件进行使用saveas参数自定义命名,当然也可以不指定,文件会默认命名并保存在空间。
$savekey = \Qiniu\base64_urlSafeEncode($this->bucket.':123123123.avi');
$fops = $fops.'|saveas/'.$savekey;
//设置上传策略
if(!empty($pipeline)){ //使用私有队列
$policy = array(
'persistentOps' => $fops, //资源上传成功后触发执行的预转持久化处理指令列表
'persistentPipeline' => $pipeline,
'persistentNotifyUrl' => 'http://example.com/index.php/Index/UploadFile/delete?file_name='.$file['name']
);
}else{ //使用公有队列
$policy = array(
'persistentOps' => $fops,
'persistentNotifyUrl' => 'http://example.com/index.php/Index/UploadFile/delete?file_name='.$file['name']
);
}
$auth = new Auth($this -> accessKey,$this -> secretKey);
// 生成上传Token
$this->token = $auth->uploadToken($this -> bucket,null,3600,$policy);
// 构建 UploadManager 对象
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($this->token, $file['name'], $file['tmp_name']);
if ($err !== null) {
return ['err' => 1, 'msg' => $err, 'data' => ''];
} else {
//返回图片的完整URL
return ['err' => 0, 'msg' => '上传完成', 'data' => ($this->domain . $ret['key'])];
}
}
//转码完成后的回调,这里执行删除原先的文件
public function delete($file_name)
{
$auth = new Auth($this -> accessKey,$this -> secretKey);
$bucketMgr = new BucketManager($auth);
$bucketMgr->delete($this -> bucket,$file_name);
}
}
UploadFile2.php
<?php
namespace app\index\controller;
use Qiniu\Storage\BucketManager;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Processing\PersistentFop;
use Qiniu\Config;
class UploadFile2 extends Controller
{
protected $domain;
protected $bucket;
protected $accessKey;
protected $secretKey;
public function __construct() {
$this -> domain = '空间对应域名';
$this -> bucket = '空间名';
$this -> accessKey = 'AK';
$this -> secretKey = 'SK';
}
public function uploadOne2($file_name)
{
//要转码的文件所在的空间和文件名
$bucket = $this -> bucket;
$key = $file_name;
//转码操作,设置转码参数 https://developer.qiniu.com/dora/api/1248/audio-and-video-transcoding-avthumb
$fops = "avthumb/mp4/s/640x360/vb/1.4m";
$savekey = \Qiniu\base64_urlSafeEncode($bucket.':触发持久化.mp4');
$fops = $fops.'|saveas/'.$savekey;
//转码时使用的队列名称
$pipeline = 'line1';
//转码完成后通知到你的业务服务器。
$notifyUrl = 'http://example.com/index.php/Index/UploadFile2/delete?file_name='.$file_name;
$auth = new Auth($this -> accessKey, $this -> secretKey);
$config = new Config();
$pfop = new PersistentFop($auth,$config);
list($id, $err) = $pfop->execute($bucket, $key, $fops, $pipeline, $notifyUrl);
echo "\n====> pfop avthumb result: \n";
if ($err != null) {
var_dump($err);
} else {
echo "PersistentFop Id: $id\n";
}
//查询转码的进度和状态
list($ret, $err) = $pfop->status($id);
echo "\n====> pfop avthumb status: \n";
if ($err != null) {
var_dump($err);
} else {
var_dump($ret);
}
}
//转码完成后的回调,这里执行删除原先的文件
public function delete($file_name)
{
$auth = new Auth($this -> accessKey,$this -> secretKey);
$bucketMgr = new BucketManager($auth);
$bucketMgr->delete($this -> bucket,$file_name);
}
}
以上就是视频转码的两种情况。
原创文章转载请注明:转载自:七牛云存储之视频转码
发表评论
沙发空缺中,还不快抢~