TranscodingController.class.php 3.79 KB
<?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;
    }
}