阿里云视频上传实战

Posted 卖柴火的小伙子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云视频上传实战相关的知识,希望对你有一定的参考价值。

背景说明

    最近在做项目优化,关于阿里云视频上传方面一直存在视频上传过慢问题.由于之前采用的是服务端进行视频上传,主要的逻辑是客户端上传视频到服务端进行视频文件暂存,然后服务端上传视频到阿里云.上传过慢猜测是上传视频到服务端要受到带宽的影响,决定直接采用客户端(项目是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,可以直接下载项目复制最新的依赖信息.

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

Java代码实现阿里云视频上传

目录

视频上传

新建一个springboot项目,结构大概这样

添加依赖

   <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.5.1</version>
    </dependency>
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.10.2</version>
    </dependency>
     <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-vod</artifactId>
        <version>2.15.11</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.28</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.2</version>
    </dependency>

创建service层

package com.cjw.video.service;

import org.springframework.web.multipart.MultipartFile;

public interface VideoService 
    void uploadVideo(MultipartFile file);

package com.cjw.video.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.cjw.video.service.VideoService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;


@Service
public class VideoServiceImpl implements VideoService 

    @Override
    public void uploadVideo(MultipartFile file) 
        try 
            String originalFilename = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            testUploadStream("LTAIrNZcZiRfgSys","n9TuOpdaVjl4sZkZusepek","aa",originalFilename,inputStream);
        catch (Exception e)
            e.printStackTrace();
        
    

    /**
     * 流式上传接口
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @param title
     * @param fileName
     * @param inputStream
     */
    private static void testUploadStream(String accessKeyId, String accessKeySecret, String title, String fileName, InputStream inputStream) 
        UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
        /* 是否使用默认水印(可选),指定模板组ID时,根据模板组配置确定是否使用默认水印*/
        //request.setShowWaterMark(true);
        /* 自定义消息回调设置,参数说明参考文档 https://help.aliyun.com/document_detail/86952.html#UserData */
        //request.setUserData(""\\"Extend\\":\\"test\\":\\"www\\",\\"localId\\":\\"xxxx\\",\\"MessageCallback\\":\\"CallbackURL\\":\\"http://test.test.com\\""");
        /* 视频分类ID(可选) */
        //request.setCateId(0);
        /* 视频标签,多个用逗号分隔(可选) */
        //request.setTags("标签1,标签2");
        /* 视频描述(可选) */
        //request.setDescription("视频描述");
        /* 封面图片(可选) */
        //request.setCoverURL("http://cover.sample.com/sample.jpg");
        /* 模板组ID(可选) */
        //request.setTemplateGroupId("8c4792cbc8694e7084fd5330e56a33d");
        /* 工作流ID(可选) */
        //request.setWorkflowId("d4430d07361f0*be1339577859b0177b");
        /* 存储区域(可选) */
        //request.setStorageLocation("in-201703232118266-5sejdln9o.oss-cn-shanghai.aliyuncs.com");
        /* 开启默认上传进度回调 */
        // request.setPrintProgress(true);
        /* 设置自定义上传进度回调 (必须继承 VoDProgressListener) */
        // request.setProgressListener(new PutObjectProgressListener());
        /* 设置应用ID*/
        //request.setAppId("app-1000000");
        /* 点播服务接入点 */
        //request.setApiRegionId("cn-shanghai");
        /* ECS部署区域*/
        // request.setEcsRegionId("cn-shanghai");
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) 
            System.out.print("VideoId=" + response.getVideoId() + "\\n");
         else  //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
            System.out.print("VideoId=" + response.getVideoId() + "\\n");
            System.out.print("ErrorCode=" + response.getCode() + "\\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\\n");
        
    



复制过去会发现编译器爆红,有几个包导不了,需要去阿里云下载

下载好了解压,

只需要这一个jar包

然后去idea找到File --> project Structure --> modules


选择刚刚下载的 aliyun-java-vod-upload-1.4.14.jar ,应用,然后就可以导包了。

对于这个 testUploadStream(“LTAI4G7nRizWrNZcZ”,“n9TuOVjl4sZkZusepek”,“aa”,originalFilename,inputStream);

第一个参数accessKeyId,第二个accessKeySecret,前两个在自己阿里云里面可以找到(如果没找到,可以看我之前的一篇文章,https://blog.csdn.net/weixin_46146197/article/details/121793267?spm=1001.2014.3001.5502)里面有教程。
第三个title是表示存储在阿里云的名字(这里我是写死了aaa所以每个上传好的视频都叫aaa,你可以获取好原视频的名称再放进去String substring = originalFilename.substring(0, originalFilename.lastIndexOf("."));)。
第四个是原文件名字。
第五个是上传文件输入流。

再创建controller

package com.cjw.video.controller;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.cjw.video.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/video")
public class videoController 
    @Autowired
    private VideoService videoService;

    @PostMapping(value = "uploadVideo/interAspect",headers = "content-type=multipart/form-data")
    public void uploadVideo(@RequestParam("file") MultipartFile file) throws IOException 
        videoService.uploadVideo(file);
    



最后配置文件

#服务端口
server.port=8999
#服务名
spring.application.name=uploadVideo

#环境设置:
spring.profiles.active=dev

# 最大上传单个文件大小:默认1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置总上传的数据大小 :默认10M
spring.servlet.multipart.max-request-size=1024MB

logging.level.root = INFO

用postman测试

用postman测试的时候,接口需要再最后加interAspect(我上面代码就加了),不然通不了



上传成功返回一个videoId,然后把这个id存到数据库就可。

视频删除

controller

@DeleteMapping(value = "deleteUploadVideo/interAspect/id")
    public void deleteUploadVideo(@PathVariable("id") String id) 
        videoService.deleteUploadVideo(id);
    

serviceImpl

    @Override
    public void deleteUploadVideo(String id) 
        try 
            //初始化对象
            DefaultAcsClient client = getClient("LTAI4G7nRizWcZiRfgSys","n9TuOpZPW9QNyusepek");
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        catch(Exception e) 
            e.printStackTrace();
        
    

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

以上是关于阿里云视频上传实战的主要内容,如果未能解决你的问题,请参考以下文章

通过网页上传阿里云点播视频

SpringBoot整合阿里云视频点播

SpringBoot整合阿里云视频点播

阿里云周源:一篇文章读懂四代视频加密技术演进

阿里云周源:一篇文章读懂四代视频加密技术演进

小程序使用阿里云oss视频链接,换了视频但是小程序还是老视频