侧边栏壁纸
博主头像
高大北博主等级

所有的再见中,我最喜欢明天见

  • 累计撰写 208 篇文章
  • 累计创建 151 个标签
  • 累计收到 20 条评论
标签搜索

目 录CONTENT

文章目录

阿里云视频点播服务最新版本使用方法

高大北
2022-07-18 / 0 评论 / 2 点赞 / 298 阅读 / 1,669 字 / 正在检测是否收录...

阿里云官方文档地址:

https://help.aliyun.com/product/29932.html?spm=a2c4g.11186623.6.540.3c356a58OEmVZJ

pom

有一个 需要自己引入。https://logaaaaa.oss-cn-beijing.aliyuncs.com/aliyun-java-vod-upload-1.4.13.jar
也可以去。
https://help.aliyun.com/document_detail/51992.htm?spm=a2c4g.11186623.0.0.767a4150nSBd6g#topic-1959787-table-jql-3ej-tg4

   <!--阿里云视频点播上传-->
   <dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>aliyun-java-sdk-core</artifactId>
       <version>4.5.30</version>
   </dependency>
   <dependency>
       <groupId>com.aliyun.oss</groupId>
       <artifactId>aliyun-sdk-oss</artifactId>
       <version>3.13.2</version>
   </dependency>
    <dependency>
       <groupId>com.aliyun</groupId>
       <artifactId>aliyun-java-sdk-vod</artifactId>
       <version>2.15.12</version>
   </dependency>
   <dependency>
       <groupId>com.alibaba</groupId>
       <artifactId>fastjson</artifactId>
       <version>1.2.78</version>
   </dependency>
   <dependency>
       <groupId>org.json</groupId>
       <artifactId>json</artifactId>
       <version>20170516</version>
   </dependency>
   <dependency>
       <groupId>com.google.code.gson</groupId>
       <artifactId>gson</artifactId>
       <version>2.8.6</version>
   </dependency>

控制层

package com.xiaogui.video.controller;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.xiaogui.security.common.results.ResultEnum;
import com.xiaogui.security.common.results.ResultResponse;
import com.xiaogui.video.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
* @author PC
*/
@RestController
@RequestMapping("video")
@CrossOrigin
@Api(value = "阿里云视频点播", tags = "阿里云视频点播")
@Slf4j
public class VideoController {

   @Autowired
   private VideoService videoService;

   @PostMapping("uploadAlyiVideo")
   @ApiOperation(value = "上传阿里云视频", notes = "上传阿里云视频")
   public ResultResponse uploadAlyiVideo(@RequestPart("file") MultipartFile file) {
       //返回上传视频id
       String videoId = videoService.uploadVideoAly(file);
       return ResultResponse.of(videoId, ResultEnum.SUCCESS,true);
   }

   /**
    * 根据视频id删除阿里云视频
    */
   @DeleteMapping("removeAlyVideo/{id}")
   @ApiOperation(value = "根据视频id删除阿里云视频", notes = "根据视频id删除阿里云视频")
   public ResultResponse removeAlyVideo(@PathVariable String id) {
       return videoService.removeAlyVideo(id);
   }

   /**
    * 删除多个阿里云视频的方法
    * 参数多个视频id  List videoIdList
    */
   @DeleteMapping("deleteBatch")
   @ApiOperation(value = "删除多个阿里云视频", notes = "删除多个阿里云视频")
   public ResultResponse deleteBatch(@RequestParam("videoIdList") List<String> videoIdList) {
       videoService.removeMoreAlyVideo(videoIdList);
       return ResultResponse.of(null,ResultEnum.SUCCESS,true);
   }
   /**
    * 视频id获取视频播放凭证
    */
   @GetMapping("getVideoPlayAuth/{id}")
   @ApiOperation(value = "获取视频播放凭证", notes = "获取视频播放凭证")
   public ResultResponse getVideoPlayAuth(@PathVariable String id) {
       String playAuth = videoService.getVideoPlayAuth(id);
       return ResultResponse.of(playAuth,ResultEnum.SUCCESS,true);
   }
   /**
    * 视频id获取视频播放地址
    */
   @GetMapping("getPlayInfo/{id}")
   @ApiOperation(value = "视频id获取视频播放地址", notes = "视频id获取视频播放地址")
   public ResultResponse getPlayInfo(@PathVariable String id) {
       List<GetPlayInfoResponse.PlayInfo> playUrl = videoService.getPlayInfo(id);
       return ResultResponse.of(playUrl,ResultEnum.SUCCESS,true);
   }
}

service

package com.xiaogui.video.service;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import com.xiaogui.security.common.results.ResultEnum;
import com.xiaogui.security.common.results.ResultResponse;
import com.xiaogui.video.utils.ConstantVideoUtils;
import com.xiaogui.video.utils.InitVideoClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author PC
 */
@Service
@Slf4j
public class VideoService {
    /**
     *上传阿里云视频
     */
    public String uploadVideoAly(MultipartFile file) {
        String videoId = null;
        try {
            //accessKeyId, accessKeySecret
            //fileName:上传文件原始名称 获取视频文件原始名称
            String fileName = file.getOriginalFilename();
            //title:上传之后显示名称 去除视频后缀
            assert fileName != null;
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream:上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request =
                    new UploadStreamRequest(ConstantVideoUtils.ACCESS_KEY_ID,
                            ConstantVideoUtils.ACCESS_KEY_SECRET,title,fileName,inputStream);

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。
                // 其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
                String errorMessage = "阿里云上传错误:" + "code:" + response.getCode() + ", message:" + response.getMessage();
                log.warn(errorMessage);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
        return videoId;
    }
    /**
     *根据视频id删除阿里云视频
     */
    public ResultResponse removeAlyVideo(String id) {
        try {
            //初始化对象
            DefaultAcsClient client =
                    InitVideoClient.initVideoClient(
                            ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            DeleteVideoResponse acsResponse = client.getAcsResponse(request);

            return ResultResponse.of(acsResponse.getNonExistVideoIds(), ResultEnum.SUCCESS,true);
        }catch(Exception e) {
            e.printStackTrace();
            log.error("删除视频失败异常");
            return ResultResponse.failed(ResultEnum.FAILURE,true);
        }
    }

    /**
     *删除多个阿里云视频的
     */
    public void removeMoreAlyVideo(List<String> videoIdList) {
        try {
            //初始化对象
            DefaultAcsClient client = InitVideoClient.initVideoClient(
                    ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();

            //videoIdList值转换成 1,2,3格式
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");

            //向request设置视频id
            request.setVideoIds(videoIds);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch(Exception e) {
            e.printStackTrace();
            log.error("删除视频失败异常");
        }
    }

    /**
     * 视频id获取视频播放凭证
     */
    public String getVideoPlayAuth(String id) {
        try {
            //初始化客户端、请求对象和相应对象
            DefaultAcsClient client = InitVideoClient.initVideoClient(
                    ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //设置请求参数
            request.setVideoId(id);
            //获取请求响应
            response = client.getAcsResponse(request);
            //输出请求结果
            //播放凭证
            String playAuth = response.getPlayAuth();
            return playAuth;
        }catch (Exception e){
            log.error("ErrorMessage = " + e.getLocalizedMessage());
            return null;
        }
    }

    /**
     * 视频id获取视频播放地址
     */
    public List<GetPlayInfoResponse.PlayInfo> getPlayInfo(String id) {
        try {
            //初始化客户端、请求对象和相应对象
            DefaultAcsClient client = InitVideoClient.initVideoClient(
                    ConstantVideoUtils.ACCESS_KEY_ID, ConstantVideoUtils.ACCESS_KEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            //设置请求参数
            request.setVideoId(id);
            //获取请求响应
            response = client.getAcsResponse(request);
            //输出请求结果
            //输出请求结果播放地址
            return response.getPlayInfoList();
        }catch (Exception e){
            log.error("ErrorMessage = " + e.getLocalizedMessage());
            return null;
        }
    }
}

application.properties

#阿里云 视频点播
#不同的服务器,地址不同
aliyun.video.file.keyid=your accessKeyId
aliyun.video.file.keysecret=your accessKeySecret
# 最大上传单个文件大小:默认1M 设置为1G
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB

utils

package com.xiaogui.video.utils;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 视频点播常量类
 * @author PC
 */
@Component
public class ConstantVideoUtils implements InitializingBean {

    @Value("${aliyun.video.file.keyid}")
    private String keyId;
    @Value("${aliyun.video.file.keysecret}")
    private String keySecret;

    /**
     * 定义公开静态常量
     */
    public static String ACCESS_KEY_SECRET;
    public static String ACCESS_KEY_ID;

    @Override
    public void afterPropertiesSet() throws Exception {
        ACCESS_KEY_ID = keyId;
        ACCESS_KEY_SECRET = keySecret;
    }
}

package com.xiaogui.video.utils;

import com.aliyun.oss.ClientException;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author PC
 * 初始化AccessKey
 */
public class InitVideoClient {

    public static DefaultAcsClient initVideoClient(String accessKeyId, String accessKeySecret) throws ClientException {
    	// 点播服务接入区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        return new DefaultAcsClient(profile);
    }
}

2

评论区