VodUtil.java 5.27 KB
package com.subsidy.util;

import com.subsidy.common.configure.VODConfig;
import com.subsidy.common.exception.HttpException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.vod.v20180717.VodClient;
import com.tencentcloudapi.vod.v20180717.models.DeleteMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeCDNUsageDataRequest;
import com.tencentcloudapi.vod.v20180717.models.DescribeCDNUsageDataResponse;
import com.tencentcloudapi.vod.v20180717.models.MediaProcessTaskInput;
import com.tencentcloudapi.vod.v20180717.models.ProcessMediaRequest;
import com.tencentcloudapi.vod.v20180717.models.ProcessMediaResponse;
import com.tencentcloudapi.vod.v20180717.models.StatDataItem;
import com.tencentcloudapi.vod.v20180717.models.TranscodeTaskInput;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  腾讯云 - 云点播API
 * </p>
 *
 * @author DengMin
 * @since 2022/8/1
 */
@Component
public class VodUtil {

    private static String endpoint = "vod.tencentcloudapi.com";

    /**
     * 根据视频模版进行转码
     * @param vodCode
     * @return
     */
    public static ProcessMediaResponse processMedia(VODConfig vodConfig, String vodCode) {
        try {
            //上传后直接转码
            Credential cred = new Credential(vodConfig.getSecretId(), vodConfig.getSecretKey());
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            VodClient client = new VodClient(cred, "", clientProfile);

            ProcessMediaRequest processMediaRequest = new ProcessMediaRequest();
            MediaProcessTaskInput mediaProcessTaskInput1 = new MediaProcessTaskInput();
            TranscodeTaskInput[] transcodeTaskInputs1 = new TranscodeTaskInput[1];
            TranscodeTaskInput transcodeTaskInput1 = new TranscodeTaskInput();
            transcodeTaskInput1.setDefinition(ConstantUtils.TEMPLATE_VOD);
            transcodeTaskInputs1[0] = transcodeTaskInput1;
            mediaProcessTaskInput1.setTranscodeTaskSet(transcodeTaskInputs1);
            processMediaRequest.setMediaProcessTask(mediaProcessTaskInput1);
            processMediaRequest.setFileId(vodCode);
            return client.ProcessMedia(processMediaRequest);
        } catch (Exception ex) {
            throw new HttpException(50001);
        }
    }

    /**
     * 删除腾讯云上原视频
     * @param vodCode
     */
    public static void deleteMedia(VODConfig vodConfig, String vodCode) {
        try {
            //删除原视频
            Credential cred = new Credential(vodConfig.getSecretId(), vodConfig.getSecretKey());
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            VodClient client = new VodClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DeleteMediaRequest req = new DeleteMediaRequest();
            req.setFileId(vodCode);
            // 返回的resp是一个DeleteMediaResponse的实例,与请求对象对应
            client.DeleteMedia(req);
        } catch (TencentCloudSDKException e) {
            throw new HttpException(99999, e.getMessage());
        }
    }

    /**
     * 云点播域名的CDN统计数据
     * @param vodConfig
     * @param DataType
     * @param startTime
     * @param endTime
     * @return
     */
    public static StatDataItem[] DescribeCDNStatDetails(VODConfig vodConfig, String DataType, String startTime, String endTime) {
        try {
            Credential cred = new Credential(vodConfig.getSecretId(), vodConfig.getSecretKey());
            // 实例化一个http选项,可选的,没有特殊需求可以跳过
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint(endpoint);
            // 实例化一个client选项,可选的,没有特殊需求可以跳过
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            // 实例化要请求产品的client对象,clientProfile是可选的
            VodClient client = new VodClient(cred, "", clientProfile);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            DescribeCDNUsageDataRequest req = new DescribeCDNUsageDataRequest();
            req.setStartTime(startTime);
            req.setEndTime(endTime);
            req.setDataType(DataType);
            // 返回的resp是一个DescribeCDNUsageDataResponse的实例,与请求对象对应
            DescribeCDNUsageDataResponse resp = client.DescribeCDNUsageData(req);
            // 返回的resp是一个DescribeCDNStatDetailsResponse的实例,与请求对象对应
            return resp.getData();
        } catch (TencentCloudSDKException e) {
            e.printStackTrace();
            throw new HttpException(99999, e.getMessage());
        }
    }
}