Blame view

VodUtil.java 5.27 KB
涂亚平 committed
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 119 120 121 122 123
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());
        }
    }
}