阿里云视频上传实战

背景说明

    最近在做项目优化,关于阿里云视频上传方面一直存在视频上传过慢问题.由于之前采用的是服务端进行视频上传,主要的逻辑是客户端上传视频到服务端进行视频文件暂存,然后服务端上传视频到阿里云.上传过慢猜测是上传视频到服务端要受到带宽的影响,决定直接采用客户端(项目是web项目)直接上传到阿里云,不经过服务端这个环节,猜测上传速度会快一些.两种上传方案流程图如下:
在这里插入图片描述
下面对客户端上传视频流程简要梳理,其中罗列服务端需要支持的api
在这里插入图片描述

    web客户端上传视频链接:https://help.aliyun.com/document_detail/52204.html,这里选择上传地址和凭证方式,也是官方推荐方式.简单对服务端涉及接口进行简单说明:
    CreateUploadVideo-获取音视频上传地址和凭证:
主要作用是客户端上传api中需要服务端传递指定的上传凭证、videoId等信息。官方文档地址:https://help.aliyun.com/document_detail/436543.html
注意返回的参数中官方文档中需要进行base解密,这里踩过坑,实际上不需要进行解密.
在这里插入图片描述
另外上传的音视频源文件不需要绝对路径,直接用文件名.mp4即可,这里前端的小伙伴也踩过坑,不清楚怎么获取客户端盘符路径,文档的示例确实有误导.
在这里插入图片描述

    RefreshUploadVideo - 刷新视频上传凭证:
主要作用视频文件上传超时后重新获取视频上传凭证。官方文档地址:https://help.aliyun.com/document_detail/436550.html
    GetUploadDetails - 获取媒体上传详情:
主要作用:获取媒体上传详情(如上传时间、已上传比例、上传来源等信息),用于决定前端调用获取视频播放地址接口的触发时机.就是说客户端调用阿里云上传接口响应成功之后不代表视频上传成功,其中可能会在转码,所以需要调用此接口查询一下上传完成的状态、上传完成的百分比或是转码是否完成。
官方地址:https://help.aliyun.com/document_detail/436548.html?spm=5176.8465980.help.dexternal.35f21450u0vJif&scm=20140722.S_help%40%40%E6%96%87%E6%A1%A3%40%40436548.S_os%2Bhot.ID_436548-RL_GetUploadDetails-LOC_consoleUNDhelp-OR_ser-V_2-P0_0
接口判断逻辑如下:
在这里插入图片描述
关于视频转码问题,如果代码中没有关于转码的设置,一般是音视频点播控制台中进行了设置,具体查看路径如下:
在这里插入图片描述
对播放格式无特殊要求,可以不进行转码或是仅支持部分播放格式,原因是视频上传完成之后如果有预览的需求,进行转码的时间会很长。所以调用获取视频详情接口会有问题,并非视频上传失败,而是处于转码中。
    GetPlayInfo - 获取音视频播放地址:
主要作用是用于视频播放,如果上传视频支持转码操作,会返回标清、流畅、高清等格式的视频信息。调用顺序是在GetUploadDetails - 获取媒体上传详情之后。

服务端相关代码

客户端配置类:

@Component
@Data
public class TrainConfig {
    
    

    // 视频相关参数
    @Value("${aliyun.videoAccessKeyId}")
    private String videoAccessKeyId;
    @Value("${aliyun.videoAccessKeySecret}")
    private String videoAccessKeySecret;

    @Value("${aliyun.videoEndpoint}")
    private String videoEndpoint;

    @Bean
    public Client initUploadVideoClient() throws Exception {
    
    
        Config config = new Config()
                .setAccessKeyId(videoAccessKeyId)
                .setAccessKeySecret(videoAccessKeySecret);
        config.endpoint = videoEndpoint;
        Client client = new Client(config);
        return client;
    }
}

配置文件:application-dev.yml

aliyun:
  #视频上传相关参数
  videoAccessKeyId: xxxxxxxxxxxxxxxxxx
  videoAccessKeySecret: xxxxxxxxxxxxxxxxxx
  videoEndpoint: vod.cn-xxx.aliyuncs.com

视频上传逻辑:

@RequestMapping("/upload")
@RestController
@Validated
public class UploadFile {
    
    

    @Autowired
    private Client client;


    @ApiOperation("根据videoId获取视频信息(视频上传成功之后调用,响应参数说明:duration--视频时长,单位秒;playInfoList中playURL视频播放地址,支持根据不同清晰度获取)")
    @ApiImplicitParams({
    
    
            @ApiImplicitParam(name = "videoId", value = "视频videoId", required = true, dataType = "String", paramType = "query",example = "1"),
    })
    @GetMapping("/findVideoInfo")
    public ResultVo<com.aliyun.vod20170321.models.GetPlayInfoResponseBody> findVideoInfo(@NotBlank(message = "视频id不允许为空!") String videoId) throws Exception {
    
    

        com.aliyun.vod20170321.models.GetPlayInfoRequest getPlayInfoRequest = new GetPlayInfoRequest().setVideoId(videoId);
        RuntimeOptions runtime = new RuntimeOptions();
        GetPlayInfoResponseBody getPlayInfoResponseBody=null;
        com.aliyun.vod20170321.models.GetPlayInfoResponse getPlayInfoResponse = client.getPlayInfoWithOptions(getPlayInfoRequest, runtime);

        if(ObjectUtil.isNotNull(getPlayInfoResponse)){
    
    
            getPlayInfoResponseBody  = getPlayInfoResponse.getBody();
        }


        return ResultVoUtil.success(getPlayInfoResponseBody);

    }


    @ApiOperation("获取上传凭证(web客户端上传)")
    @ApiImplicitParams({
    
    
            @ApiImplicitParam(name = "fileName", value = "文件所在路径,示例:test2.mp4", required = true, dataType = "String", paramType = "query",example = "1"),
            @ApiImplicitParam(name = "title", value = "视频标题", required = true, dataType = "String", paramType = "query",example = "1"),
    })
    @GetMapping("/getUploadAuth")
    public ResultVo getUploadAuth(@NotBlank(message = "文件名不允许为空!") String fileName,@NotBlank(message = "视频标题不允许为空!")String title) throws Exception {
    
    

        CreateUploadVideoRequest createUploadVideoRequest = new CreateUploadVideoRequest().setFileName(fileName).setTitle(title);

        RuntimeOptions runtime = new RuntimeOptions();
        CreateUploadVideoResponse uploadVideoWithOptions=null;

        try {
    
    
            uploadVideoWithOptions = client.createUploadVideoWithOptions(createUploadVideoRequest, runtime);
        } catch (Exception _error) {
    
    
            TeaException error = new TeaException(_error.getMessage(), _error);
           throw new BussinessExcption(error.message);
        }

        CreateUploadVideoResponseBody createUploadVideoResponseBody = uploadVideoWithOptions.getBody();

        return ResultVoUtil.success(createUploadVideoResponseBody);
    }

    @ApiOperation("刷新上传凭证(web客户端上传)")
    @ApiImplicitParams({
    
    
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, dataType = "String", paramType = "query",example = "1"),
    })
    @GetMapping("/refreshUploadVideo")
    public ResultVo RefreshUploadVideo(@NotBlank(message = "videoId不允许为空!") String videoId) throws Exception {
    
    


        RefreshUploadVideoRequest refreshUploadVideoRequest = new RefreshUploadVideoRequest().setVideoId(videoId);
        RuntimeOptions runtime = new RuntimeOptions();

        RefreshUploadVideoResponse refreshUploadVideoResponse=null;
        RefreshUploadVideoResponseBody refreshUploadVideoResponseBody=null;
        try {
    
    
           refreshUploadVideoResponse = client.refreshUploadVideoWithOptions(refreshUploadVideoRequest, runtime);
        }  catch (Exception _error) {
    
    
            TeaException error = new TeaException(_error.getMessage(), _error);
            throw new BussinessExcption(error.message);
        }
        if(ObjectUtil.isNotNull(refreshUploadVideoResponse)){
    
    
           refreshUploadVideoResponseBody = refreshUploadVideoResponse.getBody();
        }
        return ResultVoUtil.success(refreshUploadVideoResponseBody);
    }

    @ApiOperation("获取视频上传详情信息(web客户端上传)Status为normal即为上传成功")
    @ApiImplicitParams({
    
    
            @ApiImplicitParam(name = "videoId", value = "视频id", required = true, dataType = "String", paramType = "query",example = "1"),
    })
    @GetMapping("/getUploadDetails")
    public ResultVo getUploadDetails(@NotBlank(message = "videoId不允许为空!") String videoId) throws Exception {
    
    


        com.aliyun.vod20170321.models.GetUploadDetailsRequest getUploadDetailsRequest = new com.aliyun.vod20170321.models.GetUploadDetailsRequest().setMediaIds(videoId);
        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
        GetUploadDetailsResponse uploadDetailsWithOptions=null;
        try {
    
    
            uploadDetailsWithOptions = client.getUploadDetailsWithOptions(getUploadDetailsRequest, runtime);
        } catch (Exception _error) {
    
    
            TeaException error = new TeaException(_error.getMessage(), _error);
            throw new BussinessExcption(error.message);
        }

        return ResultVoUtil.success(uploadDetailsWithOptions);
    }
}

相关依赖没有进行展示,可以直接从api调试控制台中进行复制,里面的依赖一定是最新的,官方文档更新很不及时,已经发现多次了,也算是踩过的坑!!!官方api调试地址:https://next.api.aliyun.com/api,可以直接下载项目复制最新的依赖信息.
在这里插入图片描述

    以上是阿里云上传视频客户端上传流程,希望对有相同需求的小伙伴有所帮助!欢迎评论区留言交流!

猜你喜欢

转载自blog.csdn.net/weixin_43401380/article/details/126629778