TranscodingController.class.php
3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
/**
* 对回放视频进行转码
* Created by PhpStorm.
* User: zhoutao
* Date: 2018/1/25
* Time: 上午10:43
*/
namespace Frontend\Controller\Callback;
use Com\QcloudApi\QcloudApi;
use Common\Service\StudioFileService;
use Org\Net\Snoopy;
use VcySDK\Exception;
use VcySDK\FileConvert;
use VcySDK\Service;
class TranscodingController extends AbstractController
{
public function Index()
{
\Think\Log::record('开始转码通知:::' . var_export($this->callBackData, true));
\Think\Log::record('开始转码通知POST:::' . var_export($_POST, true));
// 如果直播室结束了 进行转码
$studioFileServ = new StudioFileService();
$studioFileIdList = $studioFileServ->list_by_conds(
['lm_id' => $this->callBackData['lm_id']],
null, [], 'file_id,domain');
$studioFileIdList = array_combine_by_key($studioFileIdList, 'file_id');
if (empty($studioFileIdList)) {
exit('SUCCESS');
}
$qcloudServ = new FileConvert(Service::instance());
$vodUtilInstance = (QcloudApi::load('Vod', [
'SecretId' => cfg('TENCENT_VOD_SECREAT_ID'),
'SecretKey' => cfg('TENCENT_VOD_SECREAT_KEY'),
'DefaultRegion' => 'sh']));
foreach ($studioFileIdList as $fileItem) {
// 转码回调
try {
$qcloudServ->add([
'qtnFileId' => $fileItem['file_id'],
'qtnChannel' => 2,
'qtnNotifyUrl' => oaUrl('Frontend/Callback/TranscodingEnd/Index', [], $fileItem['domain']),
]);
} catch (Exception $e) {
\Think\Log::record('添加回调失败:::' . var_export($e, true));
}
// 腾讯云转码
$vodTranscodeUrl = $vodUtilInstance->generateUrl(
'ConvertVodFile',
['fileId' => $fileItem['file_id']]
);
$transcodeUrlRes = $this->request($vodTranscodeUrl);
\Think\Log::record('转码返回:::' . var_export($transcodeUrlRes, true));
}
exit('SUCCESS');
}
/**
* 请求接口数据
* @param string $url 请求URL
* @param string|array $reqParams 请求数据
* @param array $headers 请求头部
* @param string $method 请求方式, 如: GET/POST/DELETE/PUT
* @param mixed $files 文件路径
* @param bool $retry 是否重试
* @return bool
*/
private function request($url, $reqParams = [], $headers = [], $method = 'GET', $files = null, $retry = false)
{
$snoopy = new Snoopy();
// 使用自定义的头字段,格式为 array(字段名 => 值, ... ...)
$snoopy->rawheaders = $headers;
// 非 GET 协议, 需要设置
$method = rstrtoupper($method);
$methods = array('GET', 'POST', 'PUT', 'DELETE');
if (!in_array($method, $methods)) {
$method = 'GET';
}
// 设置协议
if (!empty($files)) {
// 如果需要传文件
$method = 'POST';
$snoopy->set_submit_multipart();
} else {
$snoopy->set_submit_normal('application/json');
}
// 判断协议
$snoopy->set_submit_method($method);
switch (rstrtoupper($method)) {
case 'POST' :
case 'PUT' :
case 'DELETE' :
$result = $snoopy->submit($url, $reqParams, $files);
break;
default :
$result = $snoopy->fetch($url);
break;
}
// 如果读取错误
if (!$result || 200 != $snoopy->status) {
\Think\Log::record('请求错误::' . var_export($snoopy, true));
}
return $snoopy->results;
}
}