我们使用的是PHP?VodUploadSDK-PHP_1.0.2
sdk参考文档地址:
https://help.aliyun.com/document_detail/100976.html?spm=5176.12672711.help.dexternal.24641fa3fJKRcO#multiTask9090
sdk下载地址:
https://help.aliyun.com/document_detail/51992.htm?spm=a2c4g.11186623.0.0.6f8a4116cx2VuU#topic-1959787
下面,我们使用form表单的形式将本地视频文件上传到阿里云 视频点播。
一:首先,下载php sdk,并放在项目的合适目录下,此处,我放在了项目根目录的sdk文件夹下,并且取名为aliyunvod。
表单部分:
相关代码:
<div class="form-group">
<label for="input-video_upload_type" class="col-sm-2 control-label"><span class="form-required">*</span>视频上传型式</label>
<div class="col-md-6 col-sm-10">
<label class="radio-inline"><input type="radio" name="video_upload_type" value="0" checked>视频链接</label>
<label class="radio-inline"><input type="radio" name="video_upload_type" value="1" >视频文件</label>
<label class="radio-inline"><input type="radio" name="video_upload_type" value="2" >视频id</label>
</div>
</div>
<div class="form-group video_url_area" >
<label for="input-href" class="col-sm-2 control-label"><span class="form-required">*</span>视频链接</label>
<div class="col-md-6 col-sm-10">
<input type="text"class="form-control" id="input-href" name="href" value=""><p class="help-block">以http://或https://开头</p>
</div>
</div>
<div class="form-group upload_video_area" style="display: none;">
<label for="input-user_login" class="col-sm-2 control-label">上传视频</label>
<div class="col-md-6 col-sm-10">
<input type="file" name="file" />
</div>
</div>
<div class="form-group video_id_area" style="display: none;">
<label for="input-videoid" class="col-sm-2 control-label"><span class="form-required">*</span>视频id</label>
<div class="col-md-6 col-sm-10">
<input type="text"class="form-control" id="input-videoid" name="videoid" value=""><p class="help-block">在阿里云视频点播控制台查看</p>
</div>
</div>
<script type="text/javascript">
$(function(){
$("input[name='video_upload_type']").click(function(){
var val=$("input[name='video_upload_type']:checked").val();
if(val==0){
$(".video_url_area").show();
$(".upload_video_area").hide();
$("input[name='videoid']").val('');
$(".video_id_area").hide();
}else if(val==1){
$(".video_url_area").hide();
$(".upload_video_area").show();
$("input[name='href']").val('');
$("input[name='videoid']").val('');
$(".video_id_area").hide();
}else{
$(".video_id_area").show();
$(".video_url_area").hide();
$(".upload_video_area").hide();
$("input[name='href']").val('');
}
});
});
</script>
服务端:
$data=$this->request->param();
$video_upload_type=$data['video_upload_type'];
$videoid=$data['videoid'];
$allow=['mp4','m3u8'];
if($video_upload_type==0){
//判断链接地址的正确性
if(strpos($url,'http')===false){
$this->error("请填写正确的视频地址");
}
$video_type=substr(strrchr($url, '.'), 1);
$video_type=strtolower($video_type);
if(!in_array($video_type, $allow)){
$this->error("请填写正确后缀的视频地址");
}
if($video_type=='mp4'){
$res=testUploadWebVideo($url,$owner_uid);
}else{
$res=testUploadWebM3u8($url,$owner_uid);
}
if($res['code']!=0){
$this->error($res['msg']);
exit;
}
$data['videoid']=$res['info']['videoid'];
}else if($video_upload_type==1){
if(!$_FILES["file"]){
$this->error("请上传视频");
}
$files["file"]=$_FILES["file"];
if (!get_file_suffix($files['file']['name'],$allow)){
$this->error("请上传正确格式的视频文件或检查上传设置中视频文件设置的文件类型");
}
$filePath = $files['file']['tmp_name'];
$name_arr = explode('.',$_FILES['file']['name']);
$count = count($name_arr);
$suffix=strtolower($name_arr[$count-1]);
$name = time().rand(1000,10000).'.'.$suffix;
$dir = CMF_ROOT.'public/upload/video/'.$name; //根据自己项目需要自行替换路径
move_uploaded_file($filePath,$dir); //重中之重的一步,先将本地文件上传到服务器上
if($suffix=='mp4'){
$res=testUploadLocalVideo($dir,$owner_uid,$name);
}else{
$res=testUploadLocalM3u8($dir,$owner_uid,$name);
}
$videoid=$res['info']['videoid'];
$data['videoid']=$videoid;
}else if($video_upload_type==2){
$data['videoid']=$videoid;
}
公共方法:
function get_file_suffix($file_name, $allow_type = array()){
$fnarray=explode('.', $file_name);
$file_suffix = strtolower(end($fnarray));
if (empty($allow_type)){
return true;
}else{
if (in_array($file_suffix, $allow_type)){
return true;
}else{
return false;
}
}
}
文件上传方法:
// 测试上传网络视频
function testUploadWebVideo($fileURL,$uid){
$rs=array('code'=>0,'msg'=>'','info'=>array());
$name_arr = explode('.',$fileURL);
$count = count($name_arr);
$name = time().'.'.$name_arr[$count-1];
$path= CMF_ROOT.'sdk/aliyunvod/Autoloader.php'; //根据自己的项目要求定义路径
require_once $path;
$accessKeyId=''; //阿里云accessKeyId【参考https://blog.csdn.net/salestina/article/details/119861854中创建子用户并赋予权限后获取ak和as】
$accessKeySecret=''; //阿里云accessKeySecret
$transcode_id=''; //阿里云视频点播转码组id
try {
$uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new \UploadVideoRequest($fileURL, $uid.'_'.$name);
$uploadVideoRequest->setTemplateGroupId($transcode_id);
$res = $uploader->uploadWebVideo($uploadVideoRequest);
if(!$res){
$rs['code']=1001;
$rs['msg']='视频上传失败';
return $rs;
}
$rs['info']['videoid']=$res;
return $rs;
} catch (Exception $e) {
//printf("testUploadWebVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
//$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
$rs['code']=1001;
$rs['msg']='文件上传失败';
return $rs;
exit;
}
}
// 测试上传网络m3u8视频
function testUploadWebM3u8($m3u8FileUrl,$uid){
$rs=array('code'=>0,'msg'=>'','info'=>array());
$name_arr = explode('.',$m3u8FileUrl);
$count = count($name_arr);
$name = time().'.'.$name_arr[$count-1];
$path= CMF_ROOT.'sdk/aliyunvod/Autoloader.php'; //根据自己的项目要求定义路径
require_once $path;
$accessKeyId=''; //阿里云accessKeyId【参考https://blog.csdn.net/salestina/article/details/119861854中创建子用户并赋予权限后获取ak和as】
$accessKeySecret=''; //阿里云accessKeySecret
$transcode_id=''; //阿里云视频点播转码组id
try {
$uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new \UploadVideoRequest($m3u8FileUrl, $uid.'_'.$name);
// 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
$uploadVideoRequest->setTemplateGroupId($transcode_id);
$sliceFileUrls = $uploader->parseM3u8File($m3u8FileUrl);
//print_r($sliceFileUrls);
$res = $uploader->uploadWebM3u8($uploadVideoRequest, $sliceFileUrls);
//print_r($res);
if(!$res){
$rs['code']=1001;
$rs['msg']='视频上传失败';
return $rs;
}
$rs['info']['videoid']=$res;
return $rs;
} catch (Exception $e) {
//printf("testUploadWebM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
//$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
$rs['code']=1001;
$rs['msg']='文件上传失败';
return $rs;
exit;
}
}
// 测试上传本地视频
function testUploadLocalVideo($filePath,$uid,$name){
$rs=array('code'=>0,'msg'=>'','info'=>array());
$path= CMF_ROOT.'sdk/aliyunvod/Autoloader.php'; //根据自己的项目要求定义路径
require_once $path;
$accessKeyId=''; //阿里云accessKeyId【参考https://blog.csdn.net/salestina/article/details/119861854中创建子用户并赋予权限后获取ak和as】
$accessKeySecret=''; //阿里云accessKeySecret
$transcode_id=''; //阿里云视频点播转码组id
try {
$uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new \UploadVideoRequest($filePath, $uid.'_'.$name);
$uploadVideoRequest->setTemplateGroupId($transcode_id);
$userData = array(
"MessageCallback"=>array("CallbackURL"=>""),
"Extend"=>array("localId"=>"", "test"=>"")
);
$uploadVideoRequest->setUserData(json_encode($userData));
$res = $uploader->uploadLocalVideo($uploadVideoRequest);
unlink($filePath);
if(!$res){
$rs['code']=1001;
$rs['msg']='视频上传失败';
return $rs;
}
$rs['info']['videoid']=$res;
return $rs;
} catch (Exception $e) {
/*printf("testUploadLocalVideo Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());*/
$rs['code']=1001;
$rs['msg']='文件上传失败';
return $rs;
exit;
}
}
// 测试上传本地m3u8视频
function testUploadLocalM3u8($m3u8FilePath,$uid,$name){
$rs=array('code'=>0,'msg'=>'','info'=>array());
$path= CMF_ROOT.'sdk/aliyunvod/Autoloader.php'; //根据自己的项目要求定义路径
require_once $path;
$accessKeyId=''; //阿里云accessKeyId【参考https://blog.csdn.net/salestina/article/details/119861854中创建子用户并赋予权限后获取ak和as】
$accessKeySecret=''; //阿里云accessKeySecret
$transcode_id=''; //阿里云视频点播转码组id
try {
$uploader = new \AliyunVodUploader($accessKeyId, $accessKeySecret);
$uploadVideoRequest = new \UploadVideoRequest($m3u8FilePath, $uid.'_'.$name);
// 调用接口解析m3u8的分片地址列表,如果解析结果不准确,请自行拼接地址列表(默认分片文件和m3u8文件位于同一目录)
$uploadVideoRequest->setTemplateGroupId($transcode_id);
$sliceFiles = $uploader->parseM3u8File($m3u8FilePath);
//print_r($sliceFiles);
$res = $uploader->uploadLocalM3u8($uploadVideoRequest, $sliceFiles);
//print_r($res);
unlink($m3u8FilePath);
if(!$res){
$rs['code']=1001;
$rs['msg']='视频上传失败';
return $rs;
}
$rs['info']['videoid']=$res;
return $rs;
} catch (Exception $e) {
//printf("testUploadLocalM3u8 Failed, ErrorMessage: %s\n Location: %s %s\n Trace: %s\n",
//$e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
$rs['code']=1001;
$rs['msg']='文件上传失败';
return $rs;
exit;
}
}
这样,通过php sdk上传文件,在阿里云视频点播上就可以查看到上传的文件了。
|