一、工具類
package com.yzpnb.video.util;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import java.io.InputStream;
import java.util.List;
public class AliyunVideoUtil {
private String accessKeyId="LTAI4GGf4cjh4zZdNeKqUUGw";//id
private String accessKeySecret="oQpda38y0cCUql5TOmaYiSJYDz32OP";//密鑰
private DefaultAcsClient client;
/**
* 初始化方法
* @param accessKeyId 你的證書id
* @param accessKeySecret 你的密鑰
* @return
* @throws ClientException
*/
public 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;
}
/**
* 本地檔案上傳接口,并使用模闆組轉碼(流形式)
*
* @paramT accessKeyId 已經封裝到工具類,無需聲明
* @paramT accessKeySecret 已經封裝到工具類,無需聲明
* @param title 上傳之後檔案名
* @param fileName 本地檔案的路徑和名稱,就是你要上傳的檔案路徑
* @param inputStream 上傳檔案流
*/
public String uploadVideo(String title, String fileName, InputStream inputStream) {
/**設定上傳請求頭,包含轉碼*/
UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName,inputStream);
/* 視訊分類ID(可選) */
//request.setCateId(0);
/* 模闆組ID(可選) */
request.setTemplateGroupId("fc4c9920e7332c6c8d9518d4c00aaf54");
/**執行流上傳,擷取響應體*/
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
if (response.isSuccess()) {
/* 如果設定回調URL無效,不影響視訊上傳,可以傳回VideoId同時會傳回錯誤碼。其他情況上傳失敗時,VideoId為空,此時需要根據傳回錯誤碼分析具體錯誤原因 */
if(response.getVideoId()!=null) return response.getVideoId();
}
/****如果上傳成功就将視訊id傳回****/
return response.getVideoId();
}
/**
* 送出媒體處理作業,視訊轉碼
*/
public SubmitTranscodeJobsResponse submitTranscodeJobs(String id) throws Exception {
client=initVodClient(accessKeyId,accessKeySecret);
SubmitTranscodeJobsRequest request = new SubmitTranscodeJobsRequest();
//需要轉碼的視訊ID
request.setVideoId(id);
//轉碼模闆ID
request.setTemplateGroupId("fc4c9920e7332c6c8d9518d4c00aaf54");
return client.getAcsResponse(request);
}
/*擷取播放位址函數*/
public String getPlayInfo(String id) throws Exception {
client=initVodClient(accessKeyId,accessKeySecret);
GetPlayInfoRequest request = new GetPlayInfoRequest();
GetPlayInfoResponse response = new GetPlayInfoResponse();
request.setVideoId(id);
response=client.getAcsResponse(request);
try {
List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
//播放位址
for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
System.out.print("PlayInfo.PlayURL = " + playInfo.getPlayURL() + "\n");
}
//Base資訊
System.out.print("VideoBase.Title = " + response.getVideoBase().getTitle() + "\n");
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
return response.getRequestId();
}
/*擷取播放憑證函數*/
public String getVideoPlayAuth(String id) throws Exception {
client=initVodClient(accessKeyId,accessKeySecret);
/***擷取播放憑證***/
GetVideoPlayAuthRequest requestAuth = new GetVideoPlayAuthRequest();
GetVideoPlayAuthResponse responseAuth = new GetVideoPlayAuthResponse();
requestAuth.setVideoId(id);
responseAuth=client.getAcsResponse(requestAuth);
//播放憑證
System.out.println("PlayAuth = " + responseAuth.getPlayAuth() + "\n");
return responseAuth.getPlayAuth();
}
/**
* 删除視訊
* @paramT client 發送請求用戶端
* @return DeleteVideoResponse 删除視訊響應資料
* @throws Exception
*/
public void deleteVideo(String ...idList) throws Exception {
client=initVodClient(accessKeyId,accessKeySecret);
DeleteVideoRequest request = new DeleteVideoRequest();
DeleteVideoResponse response = new DeleteVideoResponse();
StringBuffer stringBuffer=new StringBuffer();
for (String id:idList) {
stringBuffer.append(id + ",");//将所有視訊id用逗号拼接
}
stringBuffer.deleteCharAt(stringBuffer.length()-1);//删除最後多餘的逗号
//支援傳入多個視訊ID,多個用逗号分隔 request.setVideoIds("VideoId1,VideoId2");
request.setVideoIds(stringBuffer.toString());
try {
response=client.getAcsResponse(request);
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
/**
* 删除多個視訊
* @paramT client 發送請求用戶端
* @return DeleteVideoResponse 删除視訊響應資料
* @throws Exception
*/
public void deleteVideo(List<String> idList) throws Exception {
client=initVodClient(accessKeyId,accessKeySecret);
DeleteVideoRequest request = new DeleteVideoRequest();
DeleteVideoResponse response = new DeleteVideoResponse();
StringBuffer stringBuffer=new StringBuffer();
for (String id:idList) {
stringBuffer.append(id + ",");//将所有視訊id用逗号拼接
}
stringBuffer.deleteCharAt(stringBuffer.length()-1);//删除最後多餘的逗号
//支援傳入多個視訊ID,多個用逗号分隔 request.setVideoIds("VideoId1,VideoId2");
request.setVideoIds(stringBuffer.toString());
try {
response=client.getAcsResponse(request);
} catch (Exception e) {
System.out.print("ErrorMessage = " + e.getLocalizedMessage());
}
System.out.print("RequestId = " + response.getRequestId() + "\n");
}
}
二、service
package com.yzpnb.video.service.impl;
import com.yzpnb.service_base_handler.CustomExceptionHandler;
import com.yzpnb.video.service.VideoService;
import com.yzpnb.video.util.AliyunVideoUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class VideoServiceImpl implements VideoService {
/**
*
* 上傳本地視屏到阿裡雲
* @param videoFile
* @return
*/
@Override
public String uploadVideo(MultipartFile videoFile) {
/**1、上傳**/
//1、建立封裝阿裡雲視訊操作的對象
AliyunVideoUtil aliyunVideoUtil=new AliyunVideoUtil();
String videoId = null;
//2、擷取輸入流,檔案名等等
try {
InputStream inputStream = videoFile.getInputStream();
String fileName=videoFile.getOriginalFilename();//擷取檔案名
/**
* 設定上傳檔案名,擷取檔案名中第一個字元到“.”的前一個字元
* substring:字元串截取函數,截取起始索引到末尾索引的前一個字元
* lastIndexOf:從字元串最後面往前找,找我們指定的字元,找到的第一比對的字元,傳回字元的下标
*/
String title=fileName.substring(0,fileName.lastIndexOf("."));
videoId=aliyunVideoUtil.uploadVideo(title,fileName,inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return videoId;
}
/**
* 根據id删除視訊
* @param videoId
*/
@Override
public void removeVideo(String videoId) {
//1、建立封裝阿裡雲視訊操作的對象
AliyunVideoUtil aliyunVideoUtil=new AliyunVideoUtil();
//2、删除視訊
try {
aliyunVideoUtil.deleteVideo(videoId);
} catch (Exception e) {
throw new CustomExceptionHandler(20001,"删除失敗");
}
}
/**
* 根據id集合批量删除視訊
* @param videoIdList
*/
@Override
public void removeVideoList(List<String> videoIdList) {
//1、建立封裝阿裡雲視訊操作的對象
AliyunVideoUtil aliyunVideoUtil=new AliyunVideoUtil();
//2、删除視訊
try {
aliyunVideoUtil.deleteVideo(videoIdList);
} catch (Exception e) {
throw new CustomExceptionHandler(20001,"删除失敗");
}
}
}
三、controller
package com.yzpnb.video.controller;
import com.yzpnb.common_utils.Result;
import com.yzpnb.video.service.VideoService;
import com.yzpnb.video.util.AliyunVideoUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.ws.rs.Path;
import java.util.List;
@RestController
@RequestMapping("/videoservice/")
@CrossOrigin
public class VideoController {
@Autowired
VideoService videoService;
@ApiOperation("上傳視訊(以流形式上傳,并且直接轉碼)")
@PostMapping("uploadVideo")
public Result uploadVideo(@ApiParam(name = "videoFile",value = "使用者上傳的檔案") MultipartFile file){
System.out.println(file.toString());
String videoId=videoService.uploadVideo(file);
return Result.ok().data("videoId",videoId);
}
@ApiOperation("根據視訊id删除視訊")
@DeleteMapping("{videoId}")
public Result removeVideo(@ApiParam(name = "videoId", value = "雲端視訊id", required = true)
@PathVariable String videoId){
videoService.removeVideo(videoId);
return Result.ok().message("視訊删除成功");
}
@ApiOperation("批量删除視訊")
@DeleteMapping("removeVideoList")
public Result removeVideoList(@ApiParam(name = "videoId", value = "雲端視訊id", required = true)
@RequestBody List<String> videoIdList){
videoService.removeVideoList(videoIdList);
return Result.ok().message("視訊删除成功");
}
@ApiOperation("根據視訊id擷取視訊憑證")
@GetMapping("getVideoPlayAuth/{id}")
public Result getVideoPlayAuth(@ApiParam(name = "id",value = "視訊id")
@PathVariable String id){
try {
String playAuth=new AliyunVideoUtil().getVideoPlayAuth(id);
return Result.ok().message("視訊憑證擷取成功").data("playAuth",playAuth);
} catch (Exception e) {
return Result.error().message("視訊憑證擷取失敗");
}
}
}