SpringBoot整合阿里云視頻點(diǎn)播的過程詳解
1.準(zhǔn)備工作
首先需要在阿里云開通視頻點(diǎn)播服務(wù):
1.首先,進(jìn)入到阿里云視頻點(diǎn)播平臺,點(diǎn)擊開通服務(wù),選擇按使用流量計費(fèi)即可
2.開通之后點(diǎn)擊進(jìn)入管理控制臺即可
視頻點(diǎn)播有什么用?
視頻點(diǎn)播(ApsaraVideo for VoD)是集音視頻采集、編輯、上傳、自動化轉(zhuǎn)碼處理、媒體資源管理、分發(fā)加速于一體的一站式音視頻點(diǎn)播解決方案。
2.服務(wù)端SDK的使用
官方文檔鏈接:Java SDK
SDK
的方式將api進(jìn)行了進(jìn)一步的封裝,不用自己創(chuàng)建工具類。 我們可以基于服務(wù)端SDK
編寫代碼來調(diào)用點(diǎn)播API,實現(xiàn)對點(diǎn)播產(chǎn)品和服務(wù)的快速操作。
2.1 導(dǎo)入依賴
注意:環(huán)境必須是JDK6 及以上版本
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-kms</artifactId> <version>2.10.1</version> </dependency>
2.2 初始化類
根據(jù)官方文檔示例創(chuàng)建初始化類InitObject
:
/** * @author xppll * @date 2021/12/5 13:57 */ public class InitObject { //初始化:需要傳入accessKeyId+accessKeySecret public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException { String regionId = "cn-shanghai"; //點(diǎn)播服務(wù)接入?yún)^(qū)域 DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); return client; } }
accessKeyId
和accessKeySecret
的獲取:
這里的初始化方法需要傳入賬號Access Key
信息,可以在阿里云Access Key管理創(chuàng)建主賬號Access Key
點(diǎn)播服務(wù)接入?yún)^(qū)域
regionId
的獲?。?/p>
點(diǎn)擊點(diǎn)播控制臺的存儲管理,查看自己所選的區(qū)域:
在查看接入?yún)^(qū)域標(biāo)識對應(yīng)API/SDK的RegionId
參數(shù):
2.3 創(chuàng)建讀取公共常量的工具類
首先在配置文件application.properties
添加:
aliyun.vod.file.keyid=LTAI5tGqf41adadaxxAa aliyun.vod.file.keysecret=zE4nEzmcU7GxxxxmTgWRk0tytJKD # 最大上傳單個文件大?。耗J(rèn)1M spring.servlet.multipart.max-file-size=1024MB # 最大置總上傳的數(shù)據(jù)大小 :默認(rèn)10M spring.servlet.multipart.max-request-size=1024MB
在創(chuàng)建讀取公共常量的工具類ConstantVodUtils
:
/** * @author xppll * @date 2021/12/2 19:11 */ @Component public class ConstantVodUtils implements InitializingBean { @Value("${aliyun.vod.file.keyid}") private String keyId; @Value("${aliyun.vod.file.keysecret}") private String keySecret; //定義公共靜態(tài)常量 public static String ACCESS_KEY_ID; public static String ACCESS_KEY_SECRET; @Override public void afterPropertiesSet() throws Exception { ACCESS_KEY_ID = keyId; ACCESS_KEY_SECRET = keySecret; } }
2.4 獲取視頻播放地址
首先手動先在點(diǎn)播控制臺上傳一個視頻用于測試:
步驟:
- 創(chuàng)建初始化對象
- 創(chuàng)建獲取視頻地址
request
和response
- 向
request
對象里面設(shè)置視頻id
- 用初始化對象里面的方法
getAcsResponse
,傳遞request
,獲取數(shù)據(jù) - 打印信息
代碼如下:
//獲取視頻播放地址 private static void getPlayUrl() throws ClientException { //1.創(chuàng)建初始化對象 DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET); //2.創(chuàng)建獲取視頻地址request和response GetPlayInfoRequest request = new GetPlayInfoRequest(); GetPlayInfoResponse response = new GetPlayInfoResponse(); //3.向request對象里面設(shè)置視頻id request.setVideoId("ffe90bfaxxx94d0d722caad"); //4.調(diào)用初始化對象里面的方法,傳遞request,獲取數(shù)據(jù) response = client.getAcsResponse(request); 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"); }
測試成功:
2.5 獲取視頻播放憑證
步驟與獲取視頻播放地址差不多,區(qū)別是獲取request
和response
方式不同:
//獲取視頻播放憑證 private static void getPlayAuth() throws ClientException { //1.創(chuàng)建初始化對象 DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET); //2.創(chuàng)建獲取視頻憑證的request和response GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest(); GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse(); //3.向request設(shè)置視頻id request.setVideoId("ffe90bfaaadd4xxxx0d722caad"); //4.調(diào)用初始化對象的方法得到憑證 response = client.getAcsResponse(request); //播放憑證 System.out.println("playauth" + response.getPlayAuth()); }
測試成功:
2.6 上傳視頻到阿里云視頻點(diǎn)播服務(wù)
可以參考:Java上傳SDK
需要導(dǎo)入依賴:
<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> <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>
代碼如下:
//上傳視頻到阿里云視頻點(diǎn)播服務(wù) private static void uploadVideo() { String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID; String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET; //上傳后視頻的標(biāo)題 String title = "xpp1"; //本地文件上傳路徑 String fileName = "D:/6 - What If I Want to Move Faster.mp4"; UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName); //可指定分片上傳時每個分片的大小,默認(rèn)為2M字節(jié) request.setPartSize(2 * 1024 * 1024L); //可指定分片上傳時的并發(fā)線程數(shù),默認(rèn)為1 request.setTaskNum(1); UploadVideoImpl uploader = new UploadVideoImpl(); UploadVideoResponse response = uploader.uploadVideo(request); if (response.isSuccess()) { System.out.print("VideoId=" + response.getVideoId() + "\n"); } else { /* 如果設(shè)置回調(diào)URL無效,不影響視頻上傳,可以返回VideoId同時會返回錯誤碼。其他情況上傳失敗時,VideoId為空,此時需要根據(jù)返回錯誤碼分析具體錯誤原因 */ System.out.print("VideoId=" + response.getVideoId() + "\n"); System.out.print("ErrorCode=" + response.getCode() + "\n"); System.out.print("ErrorMessage=" + response.getMessage() + "\n"); } }
測試成功:
3.springboot項目中實踐
創(chuàng)建初始化類:
/** * @author xppll * @date 2021/12/5 13:57 */ public class InitVodCilent { //初始化:需要傳入accessKeyId+accessKeySecret public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException { String regionId = "cn-shanghai"; // 點(diǎn)播服務(wù)接入?yún)^(qū)域 DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret); DefaultAcsClient client = new DefaultAcsClient(profile); return client; } }
3.1 上傳視頻到阿里云
controller
層:
@Autowired private VodService vodService; //上傳視頻到阿里云 @PostMapping("uploadAlyiVideo") public R uploadAlyVideo(MultipartFile file){ //返回上傳視頻id String videoId=vodService.uploadVideoAly(file); //將視頻`id`返回給前端 return R.ok().data("videoId",videoId); }
service
層:
//上傳視頻到阿里云 @Override public String uploadVideoAly(MultipartFile file) { try { //fileName:上傳文件原始名稱 String fileName = file.getOriginalFilename(); //title:上傳之后顯示名稱(例子:01.mp4=>01) String title = fileName.substring(0, fileName.lastIndexOf(".")); //inputStream:上傳文件輸入流 InputStream inputStream = file.getInputStream(); //獲得request UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET, title, fileName, inputStream); UploadVideoImpl uploader = new UploadVideoImpl(); //上傳獲得response UploadStreamResponse response = uploader.uploadStream(request); String videoId = null; if (response.isSuccess()) { //得到視頻id videoId = response.getVideoId(); } else { //如果設(shè)置回調(diào)URL無效,不影響視頻上傳,可以返回VideoId同時會返回錯誤碼。其他情況上傳失敗時,VideoId為空,此時需要根據(jù)返回錯誤碼分析具體錯誤原因 videoId = response.getVideoId(); } return videoId; } catch (IOException e) { e.printStackTrace(); return null; } }
3.2 根據(jù)視頻id刪除視頻
controller
層:
//根據(jù)視頻id刪除視頻 @DeleteMapping("removeAlyVideo/{id}") public R removeAlyVideo(@PathVariable String id){ vodService.deleteAlyVideo(id); return R.ok(); }
service
層:
//根據(jù)視頻id刪除視頻 @Override public void deleteAlyVideo(String id) { try { //1.初始化對象 DefaultAcsClient client = InitVodCilent.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET); //2.創(chuàng)建刪除視頻的request對象 DeleteVideoRequest request=new DeleteVideoRequest(); //3.向request設(shè)置視頻id request.setVideoIds(id); //4.調(diào)用初始化對象的方法實現(xiàn)刪除 client.getAcsResponse(request); } catch (Exception e) { e.printStackTrace(); throw new GuliException(20001,"刪除視頻失敗"); } }
到此這篇關(guān)于SpringBoot整合阿里云視頻點(diǎn)播的文章就介紹到這了,更多相關(guān)SpringBoot阿里云視頻點(diǎn)播內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一文解決System.in關(guān)閉后無法再繼續(xù)使用流的問題
這篇文章主要給大家介紹如何解決System.in關(guān)閉后無法再繼續(xù)使用流的問題,文中有詳細(xì)的解決方法和代碼示例,具有一定的參考價值,需要的朋友可以參考下2023-07-07詳談Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因
這篇文章主要介紹了詳談Map的key、value值的數(shù)據(jù)類型不能為基本類型的原因,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09基于hibernate框架在eclipse下的配置方法(必看篇)
下面小編就為大家?guī)硪黄趆ibernate框架在eclipse下的配置方法(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09springboot結(jié)合mybatis-plus基于session模擬短信注冊功能
本文主要介紹了springboot結(jié)合mybatis-plus基于session模擬短信注冊功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11springboot實現(xiàn)全局異常處理及自定義異常類
這篇文章主要介紹了springboot實現(xiàn)全局異常處理及自定義異常類,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02如何理解Java中基類子對象的構(gòu)建過程從"基類向外"進(jìn)行擴(kuò)散的?
今天小編就為大家分享一篇關(guān)于如何理解Java中基類子對象的構(gòu)建過程從"基類向外"進(jìn)行擴(kuò)散的?,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04