阿里云视频点播——企业服务

阿里云-视频点播(ApsaraVideo VoD,简称VoD)是集视频采集、编辑、上传、媒体资源管理、自动化转码处理(窄带高清™)、视频审核分析、分发加速于一体的一站式音视频点播解决方案。

  • 我的视频点播后台链接:https://vod.console.aliyun.com/#/overview
  • 个人消费明细:https://usercenter2.aliyun.com/finance/fund-flow?spm=5176.12818093.ProductAndResource–ali–widget-home-billing-overview.dfinance.3be916d0ACbqBL

一、开通视频点播服务,选择按流量计费

这边建议往账号里充个几毛钱,正常开发基本够用了,用量小于50G是不收费的

在这里插入图片描述

阿里云视频点播服务界面,未开通的点击开通,开通后可以查看价格及服务介绍,点击管理控制台,就可以来到我们的一个视频点播管理后台界面了

在这里插入图片描述
在这里插入图片描述

参考文章:https://blog.csdn.net/qq_33857573/article/details/79564255

二、使用Java代码整合阿里云视频点播服务[参考服务端和上传SDK]

  1. 获取视频播放地址
  2. 获取视频播放凭证
  3. 上传视频到阿里云视频点播服务
  • 注意:加密视频不能直接通过视频存储id查看,而需要绑定域名,数据库存储的是视频id,而不是视频地址(视频加密后,直接访问地址无效,需要获取视频播放签证,然后整合阿里云视频播放器播放)

在这里插入图片描述

服务端和客户端SDK介绍

在这里插入图片描述

  1. 引入相关依赖
<dependency>
	  <groupId>com.aliyun</groupId>
	  <artifactId>aliyun-java-sdk-core</artifactId>
	  <version>4.6.0</version>
</dependency>
<dependency>
	  <groupId>com.aliyun</groupId>
	  <artifactId>aliyun-java-sdk-vod</artifactId>
	  <version>2.16.10</version>
</dependency>
<dependency>
	  <groupId>com.alibaba</groupId>
	  <artifactId>fastjson</artifactId>
	  <version>1.2.83</version>
</dependency>
<dependency>
	  <groupId>com.aliyun</groupId>
	  <artifactId>aliyun-java-sdk-kms</artifactId>
	  <version>2.10.1</version>
</dependency>
  1. 初始化:编写视频点播 初始化对象 [AccessKey初始化] InitObject
package com.xxz.test;

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

/**
 * 视频点播 初始化对象 [AccessKey初始化]
 */
public class InitObject {

    //填入AccessKey信息
    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;
    }
    
}
  1. Vod视频点播相关测试TestVod.java
package com.xxz.test;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

import java.util.List;

/**
 *  Vod视频点播相关测试
 */
public class TestVod {

    // 测试 阿里云 存储视频 id :b45893008b5871ed808f6723a78f0102
    public static void main(String[] args) throws Exception {
        //1.根据视频id获取视频播放地址
        //(1)创建初始化对象  LTAI5tHGkW8GcwhFypERMW59  KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0
        DefaultAcsClient client = InitObject.initVodClient("LTAI5tHGkW8GcwhFypERMW59", "KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0");

        //(2)创建获取视频地址request和response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        //(3)向request对象里面设置视频id
        request.setVideoId("b45893008b5871ed808f6723a78f0102");

        //(4)调用初始化对象里面的方法,传递request,获取数据
        response = client.getAcsResponse(request);

        //获取路径集合
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
        }
        //Base信息
        System.out.println("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
    }

}

报错 “Forbidden.RAM-You are not authorized to operate this resource, or this API does not support RAM“

  • 解决方案链接:https://blog.csdn.net/KiddoKid/article/details/122993446
    在这里插入图片描述
加密视频查看方式(获取签证、整合阿里云播放器才可播放)
package com.xxz.test;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import org.apache.commons.collections4.Get;

import java.util.List;

/**
 *  Vod视频点播相关测试
 */
public class TestVod {


    public static void main(String[] args) throws Exception{
        //测试非加密视频
        notSecretVideoShow();

        //测试加密视频
        hasSecretVideoShow();
    }

    // 测试 阿里云 存储视频 id :b45893008b5871ed808f6723a78f0102 [非加密视频]
    public static void notSecretVideoShow() throws Exception {
        //1.根据视频id获取视频播放地址
        //(1)创建初始化对象  LTAI5tHGkW8GcwhFypERMW59  KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0
        DefaultAcsClient client = InitObject.initVodClient("LTAI5tHGkW8GcwhFypERMW59", "KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0");

        //(2)创建获取视频地址request和response
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        //(3)向request对象里面设置视频id
        request.setVideoId("b45893008b5871ed808f6723a78f0102");

        //(4)调用初始化对象里面的方法,传递request,获取数据
        response = client.getAcsResponse(request);

        //获取路径集合
        List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
        //播放地址
        for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
            System.out.println("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
        }
        //Base信息
        System.out.println("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
    }

    // 测试 阿里云 存储视频 id :b45893008b5871ed808f6723a78f0102 [加密视频]
    public static void hasSecretVideoShow() throws Exception {
        //1.根据视频id获取视频播放地址
        //(1)创建初始化对象  LTAI5tHGkW8GcwhFypERMW59  KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0
        DefaultAcsClient client = InitObject.initVodClient("LTAI5tHGkW8GcwhFypERMW59", "KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0");

        //(2)创建获取视频地址request和response
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
        //(3)向request对象里面设置视频id
        request.setVideoId("b45893008b5871ed808f6723a78f0102");

        //(4)调用初始化对象里面的方法,传递request,获取数据
        response = client.getAcsResponse(request);

        //Auth签证信息
        System.out.println("PlayAuth: " + response.getPlayAuth());
    }

}
进入Java SDK-媒体资源管理,定位到删除视频目录页中DeleteVideo

在这里插入图片描述
在这里插入图片描述

视频点播-文件上传SDK(客户端上传、服务端上传)

在这里插入图片描述

  1. 安装上传SDK(aliyun-java-vod-upload-1…4.9.jar)

在这里插入图片描述

  1. 下载aliyun-java-vod-upload-1…4.9.jar

在这里插入图片描述

  1. 解压后定位到lib目录中,地址栏输入cmd进入控制台,输入maven安装指令
  • 指令:mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.15 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.15.jar
    在这里插入图片描述
  1. simple目录包含实现上传案例代码,直接使用即可

在这里插入图片描述
在这里插入图片描述

  1. 上传测试UploadVod.java
package com.xxz.test;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadVideoRequest;
import com.aliyun.vod.upload.resp.UploadVideoResponse;

/**
 * 上传案例代码
 */
public class UploadVod {


    public static void main(String[] args) {


        String accessKeyId = "LTAI5tHGkW8GcwhFypERMW59";
        String accessKeySecret = "KUNfdGLjxkyvCVYS7iL7ovSIQxIJG0";

        //上传之后文件名称
        String title = "6 - What If Want to Move Faster - upload by sdk";
        //本地文件路径
        String fileName = "E:\\1672744612359.mp4";
        //调用上传视频方法
        testUploadVideo(accessKeyId, accessKeySecret, title, fileName);
    }

    /**
     * 本地文件上传接口
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @param title
     * @param fileName
     */
    private static void testUploadVideo(String accessKeyId, String accessKeySecret, String title, String fileName) {
        //通过必要参数 获取视频上传请求对象
        UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
        /* 可指定分片上传时每个分片的大小,默认为2M字节 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上传时的并发线程数,默认为1,(注:该配置会占用服务器CPU资源,需根据服务器情况指定)*/
        request.setTaskNum(1);
        //获取上传 工具类
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadVideoResponse response = uploader.uploadVideo(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");
        }
    }

}
  1. application.yml配置文件
#服务端口
server.port=8003
#服务名
spring.application.name=service-vod

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

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

#报错 The field file exceeds its maximum permitted size of 1048576 bytes.
spring.servlet.multipart.max-file-size=50MB
spring.servlet.multipart.max-request-size=50MB
  1. 视频点播启动类
package com.xxz.vod;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
 * 阿里云 视频点播 vod 启动类
 */
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages = {"com.xxz"})
public class VodApplicatioin {

    public static void main(String[] args) {
        SpringApplication.run(VodApplicatioin.class, args);
    }

}
  1. 视频点播-上传视频:常量类ConstantVodUtils.java
package com.xxz.vod.utils;

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

/**
 *  视频点播 - 视频上传 常量工具类 InitialzingBean 
 */
@Component
public class ConstantVodUtils implements InitializingBean {

    @Value("${aliyun.vod.file.keyid}")
    private String keyid;

    @Value("${aliyun.vod.file.keysecret}")
    private String keysecret;

    public static String END_POIND;

    public static String ACCESS_KEY_ID;

    /**
     * 当容器加载完之后执行
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        END_POIND = keyid;
        ACCESS_KEY_ID = keysecret;
    }
}
  1. 视频点播-上传视频:业务类VodServiceImpl.java - > uploadAliyunVideo(){}
package com.xxz.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.req.UploadURLStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyun.vod.upload.resp.UploadURLStreamResponse;
import com.xxz.vod.service.VodService;
import com.xxz.vod.utils.ConstantVodUtils;
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 {
    /**
     *  //上传视频
     * @param file
     * @return
     */
    @Override
    public String uploadAliyunVideo(MultipartFile file) {

        try{
            //accessKeyId  ,  accessKeySecret
            //fileName: 上传文件原始名称
            String fileName = file.getOriginalFilename();
            //title: 上传之后显示
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            //inputStream: 上传文件输入流
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.END_POIND,  title, fileName, inputStream);
            /* 是否显示水印(可选),指定模板组ID时,根据模板组配置确定是否显示水印*/
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            String videoId = null;
            if (response.isSuccess()) {
                videoId = response.getVideoId();
            } else {
                /* 如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因 */
                videoId = response.getVideoId();
            }
            return videoId;
        }catch (Exception e){
            return null;
        }

    }
}
  1. 视频点播-上传视频:控制器VodController.java
package com.xxz.vod.controller;

import com.xxz.util.Result;
import com.xxz.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/edu/video")
@CrossOrigin //跨域
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到 阿里云
    @PostMapping("uploadAliyunVideo")
    public Result uploadAliyunVideo(MultipartFile file){
        //上传视频
        String videoId = vodService.uploadAliyunVideo(file);
        return Result.ok().data("videoId", videoId);
    }

}
  1. SpringBoot 项目在文件上传时出现了 报错Caused by: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
    的错误,显示文件的大小超出了允许的范围。原因是 SpringBoot内嵌的 tomcat 默认的所有上传的文件大小为 1MB,超出这个大小就会报错,解决这个问题需要在application.properties配置文件更改以下两个默认。
  • multipart.maxFileSize
  • multipart.maxRequestSize
  • 解决方案呢,要根据SpringBoot的版本来确定,以下列举出了几种情况,可以根据自己项目的SpringBoot版本来对应:把默认的文件的大小限制修改一下即可。以下是我项目中的用到的文件大小限制,改成50MB即可满足我的需求了。

SpringBoot 1.3.x 之前

multipart.maxFileSize=50Mb
multipart.maxRequestSize=50Mb

SpringBoot 1.4.x

spring.http.multipart.maxFileSize=50Mb
spring.http.multipart.maxRequestSize=50Mb

SpringBoot 2.0.x之后

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

Swagger测试结果

在这里插入图片描述

  1. http状态码413,并提示Request Entity Too Large的解决办法链接:https://www.cnblogs.com/lcword/p/16067649.html

在这里插入图片描述
在这里插入图片描述

  1. 删除阿里云-视频点播SDK(编写业务方法)
  • 创建客户端
package com.xxz.vod.utils;

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

/**
 * 视频点播 初始化对象 [AccessKey初始化... 初始化操作视频点播 目录  客户端]
 */
public class InitVodClient {

    //填入AccessKey信息
    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;
    }
}
  • 阿里云视频点播-常量类
package com.xxz.vod.utils;

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

/**
 *  视频点播 - 视频上传 常量工具类
 */
@Component
public class ConstantVodUtils implements InitializingBean {

    @Value(value = "${aliyun.vod.file.keyid}")
    private String keyid;

    @Value("${aliyun.vod.file.keysecret}")
    private String keysecret;

    public static String END_POIND;

    public static String ACCESS_KEY_ID;

    /**
     * 当容器加载完之后执行
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        END_POIND =  keysecret;
        ACCESS_KEY_ID = keyid;
    }
}
  • 阿里云视频点播-删除视频 > 控制器方法
package com.xxz.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.xxz.base.exception.GuiguExcetion;
import com.xxz.util.Result;
import com.xxz.vod.service.VodService;
import com.xxz.vod.utils.ConstantVodUtils;
import com.xxz.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/vod/video")
@CrossOrigin //跨域
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到 阿里云
    @PostMapping("uploadAliyunVideo")
    public Result uploadAliyunVideo(MultipartFile file){
        //上传视频
        String videoId = vodService.uploadAliyunVideo(file);
        return Result.ok().data("videoId", videoId);
    }

    @DeleteMapping("removeAliyunVideo/{videoId}")
    public Result removeAliyunVideo(@PathVariable String videoId){
        try{
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_ID);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(videoId);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
        }catch (Exception e){
            e.printStackTrace();
            //抛出异常
            throw new GuiguExcetion(20001, "删除视频失败");
        }
        return Result.ok();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值