对接阿里云视频点播2.0版本工具类

直接上代码,不搞虚的

package com.kindess.util;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import com.kindess.core.BaseConstants;
import com.kindess.vo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kindess
 * @date 2019-07-29
 * @return VodUtil
 */
@Slf4j
public class VodUtil {
    /**
     * uid   暂时没用,设置为0
     */
    private static final String uid = "0";
    /**
     * 密钥 阿里配置的鉴权key
     */
    private static final String privateKey = "vod控台配置的鉴权key";

    /**
     * 初始化点播服务
     *
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient() {
        DefaultProfile profile = DefaultProfile.getProfile(BaseConstants.ALI_VOD_REGION_ID,
                BaseConstants.ALI_VOD_ACCESS_KEY_ID,
                BaseConstants.ALI_VOD_ACCESS_KEY_SECRET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 播放方法
     *
     * @param videoId 视频id
     * @return
     */
    public static Map<String, String> getPlay(String videoId) {
        //阿里帮助文档  https://help.aliyun.com/document_detail/56124.html?spm=a2c4g.11186623.4.1.454d7fbfs258dS
        Map<String, String> param = new HashMap();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        //视频流格式
        //request.setFormats("mp4");
        //视频流类型
        //request.setStreamType("video");
        //视频流清晰度
        //request.setDefinition("4K");
        //输出地址类型
        //request.setOutputType("cdn");
        //返回数据类型
        //request.setResultType("Single");
        String statusMsg = "";
        try {
            response = initVodClient().getAcsResponse(request);
            statusMsg = "正常";
        } catch (ClientException e) {
            log.info("get play vod error : {}", e.getMessage());
            if (e.getMessage().contains("InvalidVideo.NotFound")) {
                statusMsg = "视频不存在";
            } else {
                statusMsg = "转码中";
            }
        }
        List<GetPlayInfoResponse.PlayInfo> infoList = response.getPlayInfoList();
        //视频时长
        param.put("vodTime", String.valueOf((int) Float.parseFloat(response.getVideoBase().getDuration())));
        //视频路径
        param.put("vodUrl", infoList.get(0).getPlayURL());
        //视频状态
        param.put("status", statusMsg);
        return param;
    }

    /**
     * 删除方法
     *
     * @param videoId
     */
    public static boolean delVideo(String videoId) {
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoId);
        try {
            initVodClient().getAcsResponse(request);
        } catch (ClientException e) {
            log.error("del vod error: {}", e.getMessage());
            return false;
        }
        log.info("==>del vod success");
        return true;
    }

    /**
     * 获取视频上传地址和凭证
     *
     * @param title
     * @param fileName
     * @return 获取视频上传地址和凭证响应数据
     */
    public static VodVo createUploadVideo(String title, String fileName) {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(title);
        request.setFileName(fileName);
        try {
            CreateUploadVideoResponse response = initVodClient().getAcsResponse(request);
            VodVo vo = new VodVo();
            vo.setVideoId(response.getVideoId());
            vo.setRequestId(response.getRequestId());
            vo.setUploadAddress(response.getUploadAddress());
            vo.setUploadAuth(response.getUploadAuth());
            return vo;
        } catch (ClientException e) {
            e.printStackTrace();
            log.error("获取视频上传地址和凭证失败 error:{}" + e.getMessage());
            return null;
        }
    }

    /**
     * 刷新视频上传凭证
     *
     * @param videoId 视频ID
     * @return 刷新视频上传凭证响应数据
     */
    public static VodVo refreshUploadVideo(String videoId) {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);
        try {
            RefreshUploadVideoResponse response = initVodClient().getAcsResponse(request);
            VodVo vo = new VodVo();
            vo.setVideoId(response.getVideoId());
            vo.setRequestId(response.getRequestId());
            vo.setUploadAddress(response.getUploadAddress());
            vo.setUploadAuth(response.getUploadAuth());
            return vo;
        } catch (ClientException e) {
            e.printStackTrace();
            log.error("刷新视频上传凭证失败 error:{}" + e.getMessage());
            return null;
        }
    }

    /**
     * 获取播放凭证函数
     *
     * @param videoId
     * @return
     */
    public static Map<String, String> getVideoPlayAuth(String videoId) {
        //阿里帮助文档 https://help.aliyun.com/document_detail/52833.html?spm=a2c4g.11186623.6.721.28703d440BjEzK
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);
        //播放凭证过期时间。取值范围:100~3000 默认值:100秒
        request.setAuthInfoTimeout(3000L);
        Map<String, String> map = new HashMap<>();
        String statusMsg = "";
        try {
            GetVideoPlayAuthResponse response = initVodClient().getAcsResponse(request);
            map.put("vodAuth", response.getPlayAuth());
            map.put("vodTime", String.valueOf(Math.round(response.getVideoMeta().getDuration())));
            String status = response.getVideoMeta().getStatus();
            if (status.equals("Uploading")) {
                statusMsg = "上传中";
            } else if (status.equals("UploadFail")) {
                statusMsg = "上传失败";
            } else if (status.equals("UploadSucc")) {
                statusMsg = "上传完成";
            } else if (status.equals("Transcoding")) {
                statusMsg = "转码中";
            } else if (status.equals("TranscodeFail")) {
                statusMsg = "转码失败";
            } else if (status.equals("Checking")) {
                statusMsg = "审核中";
            } else if (status.equals("Blocked")) {
                statusMsg = "屏蔽";
            } else if (status.equals("Normal")) {
                statusMsg = "正常";
            }
            log.info("get play vod success : {}", statusMsg);
        } catch (Exception e) {
            //e.printStackTrace();
            if (e.getMessage().contains("Forbidden.IllegalStatus")) {
                statusMsg = "视频状态无效";
            } else if (e.getMessage().contains("InvalidVideo.NotFound")) {
                statusMsg = "视频不存在";
            }
            log.info("get play vod error : {}", statusMsg);
        }
        map.put("status", statusMsg);
        return map;
    }

    //*********************************************************

    /**
     * 根据阿里云规则生成auth_key作为防盗链,播放
     *
     * @param vodId 视频id
     * @return
     */
    public static String getPlayUrl(String vodId) {
        //获取播放全路径
        String vodUrl = VodUtil.getPlay(vodId).get("vodUrl");
        //截取相对路径
        String relativeUrl = vodUrl.substring(25, vodUrl.length() - 1);
        //获取过期时间戳
        Long timestamp =
                DateUtil.strToDate(DateUtil.formatDateByPattern(new Date(), DateUtil.DATE_FULL_TIME),
                        DateUtil.DATE_FULL_TIME).getTime() / 1000L;
        //加密随机字段,也可以设置为0
        String rand = String.valueOf(new Md5Hash(RandomUtil.getRandom(32)));
        String hashValue = relativeUrl + "-" + timestamp + "-" + rand + "-" + uid + "-" + privateKey;
        String playUrl = vodUrl + "?auth_key=" + timestamp + "-" + rand + "-" + uid + "-" + new Md5Hash(hashValue);
        return playUrl;
    }

}

 

//*************************上传和刷新凭证的实体VodVo************************************

package com.kindess.vo;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


/**
 * @author kindess
 */
@Getter
@Setter
@ToString
public class VodVo {

    /**
     * 视频ID
     */
    private String videoId;

    /**
     * 上传地址
     */
    private String uploadAddress;

    /**
     * 上传凭证
     */
    private String uploadAuth;

    /**
     * 请求ID
     */
    private String requestId;

}

如中途遇到什么问题或建议欢迎联系  邮箱:kindess@aliyun.com
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值