阿里云视频点播VOD
Posted 伍妖捌
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云视频点播VOD相关的知识,希望对你有一定的参考价值。
依赖
<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.aliyun</groupId>
<artifactId>aliyun-java-vod-upload</artifactId>
<version>1.4.13</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
配置
spring:
servlet:
multipart:
max-file-size: 1024MB
max-request-size: 2048MB
cloud:
alibaba:
access-key: xxxxxxxx
secret-key: xxxxxxxxxxxx
vod:
templateGroup: aaaaaaaaaaaaaa
workflow: bbbbbbbbbbbbbb
属性类
@Data
@Component
public class VodProperties {
@Value("${spring.cloud.alibaba.access-key}")
private String accessKey;
@Value("${spring.cloud.alibaba.secret-key}")
private String secretKey;
@Value("${spring.cloud.alibaba.vod.templateGroup}")
private String templateGroup;
@Value("${spring.cloud.alibaba.vod.workflow}")
private String workflow;
}
Service
public interface VideoService {
/**
* 视频上传
*
* @param in 文件流
* @param originalFileName 文件名
* @return 视频id
*/
String videoUpload(InputStream in, String originalFileName);
}
实现类
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {
@Autowired
private VodProperties vodProperties;
@Override
public String videoUpload(InputStream in, String originalFileName) {
String title = originalFileName.substring(originalFileName.lastIndexOf("."));
UploadStreamRequest request = new UploadStreamRequest(vodProperties.getAccessKey(),
vodProperties.getSecretKey(), title, originalFileName, in);
request.setTemplateGroupId(vodProperties.getTemplateGroup());
request.setWorkflowId(vodProperties.getWorkflow());
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = response.getVideoId();
if (StringUtils.isBlank(videoId)) {
log.error("{}: {}", ResultCode.VIDEO_UPLOAD_ALIYUN_ERRROR.getMessage(), response.getMessage());
throw new CustomException(ResultCode.VIDEO_UPLOAD_ALIYUN_ERRROR);
}
return videoId;
}
public Long addCategory(Long parentId, String cateName) throws Exception {
if (StringUtils.isNotBlank(cateName)) {
return null;
}
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId,
vodProperties.getAccessKey(), vodProperties.getSecretKey());
DefaultAcsClient client = new DefaultAcsClient(profile);
AddCategoryRequest request = new AddCategoryRequest();
if (parentId != null) {
request.setParentId(parentId);
}
request.setCateName(cateName);
request.setParentId(-1L);
request.setCateName("CateName");
AddCategoryResponse response = client.getAcsResponse(request);
return response.getCategory().getCateId();
}
}
访问接口
@RestController
@RequestMapping("/admin/vod/video")
@Slf4j
@Api(description = "阿里云视频点播")
public class VideoController {
@Autowired
private VideoService videoService;
@PostMapping("upload")
public Result upload(@ApiParam(value = "视频文件", required = true) @RequestParam(value = "file") MultipartFile file) {
String videoId;
try {
videoId = videoService.videoUpload(file.getInputStream(), file.getOriginalFilename());
} catch (IOException e) {
log.error("{}: {}", ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR.getMessage(), e.getMessage());
return Result.error(ResultCode.VIDEO_UPLOAD_TOMCAT_ERROR);
}
return Result.ok().data(videoId);
}
}
以上是关于阿里云视频点播VOD的主要内容,如果未能解决你的问题,请参考以下文章
Vue+java:阿里云OSS 实现视频上传,前端Vue使用vueMiniPlayer动态地址视频播放,以及发布时遇到aliyun-java-vod-upload时依赖报错问题的解决方法