SpringBoot集成阿里云视频点播

视频点播和OSS的区别

视频点播和OSS的区别
SpringBoot集成阿里云视频点播

文档

阿里云视频点播 Java SDK:https://help.aliyun.com/document_detail/57756.html

集成

1.因为上传的视频可以加密(一些付费视频需要加密),加密后的地址无法播放,故此在数据库中存储视频的id。
2.通过视频id获取视频地址和视频凭证,既可以播放不加密视频,也可以播放加密视频

1. 依赖

<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>

2. 初始化

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;

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

3. 上传操作Demo

https://help.aliyun.com/document_detail/53406.html

注:aliyun-java-vod-upload这个jar包未开源,无法联网从Maven*仓库中下载

-1 下载VODUploadDemo-java-1.4.13.zip,并解压
-2 解压后,到lib目录下通过cmd执行:

mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.13 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.13.jar
-DgroupId 对应 groupId
-DartifactId 对应 artifactId
-Dversion 对应 version
-Dfile 对应jar包在你电脑上的路径
SpringBoot集成阿里云视频点播

-3 pom.xml引入如下包

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-vod-upload</artifactId>
    <version>1.4.13</version>
</dependency>

具体写法查看下载的VODUploadDemo-java的sample目录:
SpringBoot集成阿里云视频点播

4. 上传视频接口----流上传

application.properties

#阿里云 vod
#不同的服务器,地址不同
aliyun.vod.file.keyid=Your_Key_Id
aliyun.vod.file.keysecret=Your_Key_Secret

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

nginx配置

如果使用nginx进行请求转发,则在nginx.conf的http块中增加 :
    client_max_body_size 1024m;    # 配置请求体大小
重启nginx命令:
    nginx -s reload

Vod属性常量类

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

@Component
public class ConstantVodPropertiesUtil implements InitializingBean {
    @Value("${aliyun.vod.file.keyid}")
    private String accessKeyId;
    @Value("${aliyun.vod.file.keysecret}")
    private String accessKeySecret;

    public static String ACCESS_KEY_ID;
    public static String ACCESS_KEY_SECRET;

    @Override
    public void afterPropertiesSet() throws Exception {
        ConstantVodPropertiesUtil.ACCESS_KEY_ID = this.accessKeyId;
        ConstantVodPropertiesUtil.ACCESS_KEY_SECRET = this.accessKeySecret;
    }
}

VodService

public interface VodService {
    String uploadVideoToALiYun(MultipartFile file) throws IOException;
}

VodServiceImpl

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.jarvis.vod.service.VodService;
import com.jarvis.vod.utils.ConstantVodPropertiesUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideoToALiYun(MultipartFile file) throws IOException {
        //准备参数
        String accessKeyId = ConstantVodPropertiesUtil.ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodPropertiesUtil.ACCESS_KEY_SECRET;
        String fileName = file.getOriginalFilename();   //上传文件的原始名称
        String title = fileName.substring(0,fileName.lastIndexOf('.'));     //文件上传后要显示的名称
        InputStream inputStream = file.getInputStream();
        //
        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);
        String videoId = null;
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            videoId =  response.getVideoId();
        } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
            videoId =  response.getVideoId();
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }

        return videoId;
    }
}
上一篇:如何在虚拟直播下对视频资源循环播放?


下一篇:mavn命令将jar打入本地仓库