TP5.1 调用阿里云的异步视频审核方法(不需要写脚本和定时任务)
对接阿里云的异步视频方法当初为了不想写脚本调用阿里云的异步审核用了很长时间研究出下面的方法本人能力有限没有对接成功阿里云的同步视频审核,欢迎大神指导一下因为调用阿里云的视频异步需要大概延迟30s才能获取到审核结果下面我附上我自己的方法:仅供参考<?phpnamespace app\api\controller\approval;use app\common\master\Dynamic;us
·
对接阿里云的异步视频方法
- 当初为了不想写脚本调用阿里云的异步审核用了很长时间研究出下面的方法
- 本人能力有限没有对接成功阿里云的同步视频审核,欢迎大神指导一下
- 因为调用阿里云的视频异步需要大概延迟30s才能获取到审核结果
- 下面我附上我自己的方法:仅供参考
<?php
namespace app\api\controller\approval;
use think\Controller;
use think\facade\Session;
//引用阿里云核心文件
require Env::get('root_path') . 'extend/aliyuncs/aliyun-oss-php-sdk/autoload.php';
require Env::get('root_path') . 'extend/aliyuncs/aliyun-php-sdk-core/Config.php';
use Green\Request\V20180509 as Green;
use Green\Request\Extension\ClientUploader;
/**
* @date: 2018-12
* 阿里云视频审核
*/
class VideoApproval extends Controller
{
/*
* 视频异步审核
* 场景参数【porn:图片智能鉴黄,terrorism:图片暴恐涉政,ad:图文违规,live:图片不良场景,logo:图片logo】
*/
public static function video($video, $id)
{
$accessKeyId = '阿里云的accessKeyId ';
$accessSecret = '阿里云的accessSecret ';
$iClientProfile = \DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessSecret);
\DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new \DefaultAcsClient($iClientProfile);
$request = new Green\VideoAsyncScanRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
$domain = request()->domain();
// 计费按照该处传递的场景进行。
// 一次请求中可以同时检测多个视频,每个视频可以同时检测多个风险场景,计费按照场景和视频截帧数计算。
// 例如:检测2个视频,场景传递porn,terrorism,计费会按照2个视频的截帧总数乘以鉴黄场景的价格加上2个视频的截帧总数乘以暴恐检测场景的费用计算。
$task1 = array('dataId' => uniqid(),
'url' => $domain . $video
);
$request->setContent(json_encode(array("tasks" => array($task1),
"scenes" => array("porn",'terrorism','live','logo','ad'))));
try {
$response = $client->getAcsResponse($request);
if (200 == $response->code) {
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if (200 == $taskResult->code) {
// 将taskId保存下来,间隔一段时间来轮询结果。具体请参照VideoAsyncScanResultsRequest接口说明。
$taskId = $taskResult->taskId;
$dataId = $taskResult->dataId;
$url = $domain . "/api.php/up_video.json";
$data = [
'id' => $id,
'dataId' => $dataId,
'testtaskid' => $taskId,
];
//这是我实现异步请求审核结果的关键代码;
$this->curl_post3($url, $data);//延时请求
return true;
} else {
throw new ParamException(['msg' => $response->code]);
}
}
} else {
throw new ParamException(['msg' => $response->code]);
}
} catch (\Exception $e) {
throw new ParamException(['msg' => $e->getMessage()]);
}
}
//异步审核结果查询
public function up_video()
{
$id = $this->request->param('id');
$dataId = $this->request->param('dataId');
$testtaskid = $this->request->param('testtaskid');
set_time_limit(0);
sleep(30);
$accessKeyId = '阿里云的accessKeyId ';
$accessSecret = '阿里云的accessSecret ';
$iClientProfile = \DefaultProfile::getProfile("cn-shanghai", $accessKeyId, $accessSecret);
\DefaultProfile::addEndpoint("cn-shanghai", "cn-shanghai", "Green", "green.cn-shanghai.aliyuncs.com");
$client = new \DefaultAcsClient($iClientProfile);
$request = new Green\VideoAsyncScanResultsRequest();
$request->setMethod("POST");
$request->setAcceptFormat("JSON");
//提交异步检测任务返回的taskId。
$request->setContent(json_encode(array($testtaskid)));
try {
$response = $client->getAcsResponse($request);
if (200 == $response->code) {
$taskResults = $response->data;
foreach ($taskResults as $taskResult) {
if (200 == $taskResult->code) {
$sceneResults = $taskResult->results;
foreach ($sceneResults as $sceneResult) {
$scene = $sceneResult->scene;
$suggestion = $sceneResult->suggestion;
//根据scene和suggetion做相关处理。
//do something
$arr['dataId'] = $dataId;
$arr['suggestion'] = $suggestion;
//这里处理业务逻辑
//其实就是修改状态
}
return true;
} else {
throw new ParamException(['msg' => $response->code]);
}
}
} else {
//审核结果未查询成功再次调用(有点类似于递归)
$domain = request()->domain();
$url = $domain . "/api.php/up_video.json";
$data = [
'id' => $id,
'dataId' => $dataId,
'testtaskid' => $testtaskid,
];
curl_post3($url, $data);
return true;
}
} catch (\Exception $e) {
throw new ParamException(['msg' => $e->getMessage()]);
}
}
}
//延时处理请求文件
function curl_post3($url, $postData)
{
$postData = json_encode($postData);
$curl = curl_init(); //初始化
curl_setopt($curl, CURLOPT_URL, $url); //设置url
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); //设置http验证方法
curl_setopt($curl, CURLOPT_TIMEOUT, 2); //只需要设置一个秒的数量就可以
curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); //设置curl_exec获取的信息的返回方式
curl_setopt($curl, CURLOPT_POST, 1); //设置发送方式为post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); //设置post的数据
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($postData))
);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, true);
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)