首页 > 其他 > 详细

阿里云视频点播

时间:2020-09-12 22:48:02      阅读:52      评论:0      收藏:0      [点我收藏+]

阿里云视频点播

  • 注册阿里云开通视频点播服务(免费),获取key和keysecret

1、导依赖

  • aliyun-java-vod-upload这个包没有开源,需要自己导入jar
<dependency>          //这个包在maven中没有,需要自己导入
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-vod-upload</artifactId>
    <version>1.4.12</version>
    <scope>compile</scope>
</dependency>

<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.1.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-vod</artifactId>
    <version>2.15.10</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>

2、配置基本信息

server.port=8006

spring.application.name=service_vod


aliyun.vod.file.keyid=LTAI4G8**********8J7x6x8xs
aliyun.vod.file.keysecret=hWMX*************LJvDj6


spring.servlet.multipart.max-file-size =100MB		//限制视频大小
spring.servlet.multipart.max-request-size=100MB   

3、读取配置的基本信息

package com.wagn.vod.utils;

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

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

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

    public static String KEY_ID;

    public static String KEY_SECRET;

    @Override
    public void afterPropertiesSet() throws Exception {
        KEY_ID = keyid;
        KEY_SECRET = keysecret;
    }
}

4、接口接受file流

package com.wagn.vod.controller;

import com.wagn.vod.service.VodService;
import org.springframework.beans.factory.annotation.Autowired;
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("/vod")
public class VodController {

    @Autowired
    private VodService vodService;

    @PostMapping("upload")
    public String upload(MultipartFile file){
        String rs = vodService.upload(file);
        return rs;
    }
}

5、执行上传file,成功则返回视频ID

  • 返回视频ID
package com.wagn.vod.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.wagn.vod.service.VodService;
import com.wagn.vod.utils.ReadUtil;
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 upload(MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename();
            String title = fileName.substring(0,fileName.lastIndexOf(‘.‘));
            InputStream inputStream = file.getInputStream();

            UploadStreamRequest request = new UploadStreamRequest(ReadUtil.KEY_ID, ReadUtil.KEY_SECRET, title, fileName, inputStream);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            if (response.isSuccess()) {
                //保存id到小节
                return response.getVideoId();
            } else { //如果设置回调URL无效,不影响视频上传,可以返回VideoId同时会返回错误码。其他情况上传失败时,VideoId为空,此时需要根据返回错误码分析具体错误原因
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

阿里云视频点播

原文:https://www.cnblogs.com/yizhixiang/p/13658933.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!