阿里雲視頻點播技術


一、阿里雲視頻點播技術

參考文章:

https://blog.csdn.net/qq_33857573/article/details/79564255

視頻點播(ApsaraVideo for VoD)是集音視頻采集、編輯、上傳、自動化轉碼處理、媒體資源管理、分發加速於一體的一站式音視頻點播解決方案。

1、應用場景

  • 音視頻網站:無論是初創視頻服務企業,還是已擁有海量視頻資源,可定制化的點播服務幫助客戶快速搭建擁有極致觀看體驗、安全可靠的視頻點播應用。
  • 短視頻:集音視頻拍攝、特效編輯、本地轉碼、高速上傳、自動化雲端轉碼、媒體資源管理、分發加速、播放於一體的完整短視頻解決方案。目前已幫助1000+APP快速實現手機短視頻功能。
  • 直播轉點播:將直播流同步錄制為點播視頻,用於回看。並支持媒資管理、媒體處理(轉碼及內容審核/智能首圖等AI處理)、內容制作(雲剪輯)、CDN分發加速等一系列操作。
  • 在線教育:為在線教育客戶提供簡單易用、安全可靠的視頻點播服務。可通過控制台/API等多種方式上傳教學視頻,強大的轉碼能力保證視頻可以快速發布,覆蓋全網的加速節點保證學生觀看的流暢度。防盜鏈、視頻加密等版權保護方案保護教學內容不被竊取。
  • 視頻生產制作:提供在線可視化剪輯平台及豐富的OpenAPI,幫助客戶高效處理、制作視頻內容。除基礎的剪切拼接、混音、遮標、特效、合成等一系列功能外,依托雲剪輯及點播一體化服務還可實現標准化、智能化剪輯生產,大大降低視頻制作的檻,縮短制作時間,提升內容生產效率。
  • 內容審核:應用於短視頻平台、傳媒行業審核等場景,幫助客戶從從語音、文字、視覺等多維度精准識別視頻、封面、標題或評論的違禁內容進行AI智能審核與人工審核。

2、選擇視頻點播服務

產品->企業應用->視頻雲->視頻點播

3、選擇按使用流量計費

資費說明:https://www.aliyun.com/price/product?spm=a2c4g.11186623.2.12.7fbd59b9vmXVN6#/vod/detail

4、整體流程

使用視頻點播實現音視頻上傳、存儲、處理和播放的整體流程如下:

  • 用戶獲取上傳授權。
  • VoD下發 上傳地址和憑證 及 VideoId。
  • 用戶上傳視頻保存視頻ID(VideoId)。
  • 用戶服務端獲取播放憑證。
  • VoD下發帶時效的播放憑證。
  • 用戶服務端將播放憑證下發給客戶端完成視頻播放。

二、視頻點播服務的基本使用

完整的參考文檔

https://help.aliyun.com/product/29932.html?spm=a2c4g.11186623.6.540.3c356a58OEmVZJ

1、設置轉碼格式

選擇全局設置 > 轉碼設置,單擊添加轉碼模板組。

在視頻轉碼模板組頁面,根據業務需求選擇封裝格式和清晰度。

或直接將已有的模板設置為默認即可

2、服務端SDK

sdk的方式將api進行了進一步的封裝,不用自己創建工具類。

我們可以基於服務端SDK編寫代碼來調用點播API,實現對點播產品和服務的快速操作。

  • SDK封裝了對API的調用請求和響應,避免自行計算較為繁瑣的 API簽名。
  • 支持所有點播服務的API,並提供了相應的示例代碼。
  • 支持7種開發語言,包括:Java、Python、PHP、.NET、Node.js、Go、C/C++。
  • 通常在發布新的API后,我們會及時同步更新SDK,所以即便您沒有找到對應API的示例代碼,也可以參考舊的示例自行實現調用。

1、安裝

參考文檔:https://help.aliyun.com/document_detail/57756.html

添加maven倉庫的配置和依賴到pom

  <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.3.3</version>
  </dependency>
  <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-vod</artifactId>
    <version>2.15.5</version>
  </dependency>
  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
  </dependency>

2、初始化

參考文檔:https://help.aliyun.com/document_detail/61062.html

根據文檔示例創建 AliyunVODSDKUtils.java

package com.royal.aliyunvod.util;

public class AliyunVodSDKUtils {
    
	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;
	}
}
/*
String accessKeyId = "你的accessKeyId";
String accessKeySecret = "你的accessKeySecret";
*/

三、創建測試用例

參考文檔:https://help.aliyun.com/document_detail/61064.html

1、獲取視頻播放憑證

根據文檔中的代碼,修改如下

/**
	 * 獲取視頻播放憑證
	 * @throws ClientException
	 */
@Test
public void testGetVideoPlayAuth() throws ClientException {

    //初始化客戶端、請求對象和相應對象
    DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
    GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
    GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

    try {

        //設置請求參數
        request.setVideoId("視頻ID");
        //獲取請求響應
        response = client.getAcsResponse(request);

        //輸出請求結果
        //播放憑證
        System.out.print("PlayAuth = " + response.getPlayAuth() + "\n");
        //VideoMeta信息
        System.out.print("VideoMeta.Title = " + response.getVideoMeta().getTitle() + "\n");
    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }

    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

2、獲取視頻播放地址

/**
	 * 獲取視頻播放地址
	 * @throws ClientException
	 */
@Test
public void testGetPlayInfo() throws ClientException {

    //初始化客戶端、請求對象和相應對象
    DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(accessKeyId, accessKeySecret);
    GetPlayInfoRequest request = new GetPlayInfoRequest();
    GetPlayInfoResponse response = new GetPlayInfoResponse();

    try {

        //設置請求參數
        //注意:這里只能獲取非加密視頻的播放地址
        request.setVideoId("視頻ID");
        //獲取請求響應
        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");

    } catch (Exception e) {
        System.out.print("ErrorMessage = " + e.getLocalizedMessage());
    }

    System.out.print("RequestId = " + response.getRequestId() + "\n");
}

四、文件上傳測試

參考文檔:https://help.aliyun.com/document_detail/53406.html

一、安裝SDK

1、配置pom

    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
        <version>4.3.3</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.2</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、安裝非開源jar包

在本地Maven倉庫中安裝jar包:

下載視頻上傳SDK,解壓,命令行進入lib目錄,執行以下代碼

mvn install:install-file -DgroupId=com.aliyun -DartifactId=aliyun-sdk-vod-upload -Dversion=1.4.11 -Dpackaging=jar -Dfile=aliyun-java-vod-upload-1.4.11.jar

然后在pom中引入jar包

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-sdk-vod-upload</artifactId>
    <version>1.4.11</version>
</dependency>

3、測試本地文件上傳

/**
	 * 視頻上傳
	 */
@Test
public void testUploadVideo(){

    //1.音視頻上傳-本地文件上傳
    //視頻標題(必選)
    String title = "upload by sdk";
    //本地文件上傳和文件流上傳時,文件名稱為上傳文件絕對路徑,如:/User/sample/文件名稱.mp4 (必選)
    //文件名必須包含擴展名
    String fileName = "E:/Work.mp4";
    //本地文件上傳
    UploadVideoRequest request = new UploadVideoRequest(accessKeyId, accessKeySecret, title, fileName);
    /* 可指定分片上傳時每個分片的大小,默認為1M字節 */
    request.setPartSize(1 * 1024 * 1024L);
    /* 可指定分片上傳時的並發線程數,默認為1,(注:該配置會占用服務器CPU資源,需根據服務器情況指定)*/
    request.setTaskNum(1);
    /* 是否開啟斷點續傳, 默認斷點續傳功能關閉。當網絡不穩定或者程序崩潰時,再次發起相同上傳請求,可以繼續未完成的上傳任務,適用於超時3000秒仍不能上傳完成的大文件。
        注意: 斷點續傳開啟后,會在上傳過程中將上傳位置寫入本地磁盤文件,影響文件上傳速度,請您根據實際情況選擇是否開啟*/
    request.setEnableCheckpoint(false);

    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");
    }

}

五、spring boot整合點播微服務

1、創建微服務模塊

2、引入依賴

<dependencies>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-core</artifactId>
    </dependency>
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-java-sdk-vod</artifactId>
    </dependency>
    <dependency>
        <groupId>com.aliyun</groupId>
        <artifactId>aliyun-sdk-vod-upload</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
    </dependency>
    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
    </dependency>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
    </dependency>

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>
</dependencies>

3、application.properties

# 服務端口
server.port=8003
# 服務名
spring.application.name=service-vod

# 環境設置:dev、test、prod
spring.profiles.active=dev

#阿里雲 vod
#不同的服務器,地址不同
aliyun.vod.file.keyid=your accessKeyId
aliyun.vod.file.keysecret=your accessKeySecret

# 最大上傳單個文件大小:默認1M
spring.servlet.multipart.max-file-size=1024MB
# 最大置總上傳的數據大小 :默認10M
spring.servlet.multipart.max-request-size=1024MB

4、啟動類

VodApplication.java

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan(basePackages={"com.royal"})
public class VodApplication {

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

5、創建常量類

ConstantPropertiesUtil.java

@Component
//@PropertySource("classpath:application.properties")
public class ConstantPropertiesUtil implements InitializingBean {

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

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

	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;
	}
}

6、復制工具類到項目中

AliyunVodSDKUtils.java

public class AliyunVodSDKUtils {
	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;
	}
}

7、上傳視頻

1、創建service

接口:VideoService.java

package com.guli.vod.service;
public interface VideoService {
	String uploadVideo(MultipartFile file);
}

實現:VideoServiceImpl.java


@Service
public class VideoServiceImpl implements VideoService {

	@Override
	public String uploadVideo(MultipartFile file) {

		try {
			InputStream inputStream = file.getInputStream();
			String originalFilename = file.getOriginalFilename();
			String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));

			UploadStreamRequest request = new UploadStreamRequest(
					ConstantPropertiesUtil.ACCESS_KEY_ID,
					ConstantPropertiesUtil.ACCESS_KEY_SECRET,
					title, originalFilename, inputStream);

			UploadVideoImpl uploader = new UploadVideoImpl();
			UploadStreamResponse response = uploader.uploadStream(request);

			//如果設置回調URL無效,不影響視頻上傳,可以返回VideoId同時會返回錯誤碼。
			// 其他情況上傳失敗時,VideoId為空,此時需要根據返回錯誤碼分析具體錯誤原因
			String videoId = response.getVideoId();
			if (!response.isSuccess()) {
				String errorMessage = "阿里雲上傳錯誤:" + "code:" + response.getCode() + ", message:" + response.getMessage();
				log.warn(errorMessage);
				if(StringUtils.isEmpty(videoId)){
					throw new GuliException(20001, errorMessage);
				}
			}

			return videoId;
		} catch (IOException e) {
			throw new GuliException(20001, "guli vod 服務上傳失敗");
		}
	}
}

8、創建controller

@Api(description="阿里雲視頻點播微服務")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/vod/video")
public class VideoAdminController {

	@Autowired
	private VideoService videoService;

	@PostMapping("upload")
	public R uploadVideo(
			@ApiParam(name = "file", value = "文件", required = true)
			@RequestParam("file") MultipartFile file) throws Exception {

		String videoId = videoService.uploadVideo(file);
		return R.ok().message("視頻上傳成功").data("videoId", videoId);
	}
}

啟動后端vod微服務

8、刪除雲端視頻

文檔:服務端SDK->Java SDK->媒資管理

https://help.aliyun.com/document_detail/61065.html?spm=a2c4g.11186623.6.831.654b3815cIxvma#h2--div-id-deletevideo-div-7

1、service

接口

void removeVideo(String videoId);

實現

@Override
public void removeVideo(String videoId) {
    try{
        DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
            ConstantPropertiesUtil.ACCESS_KEY_ID,
            ConstantPropertiesUtil.ACCESS_KEY_SECRET);

        DeleteVideoRequest request = new DeleteVideoRequest();

        request.setVideoIds(videoId);

        DeleteVideoResponse response = client.getAcsResponse(request);

        System.out.print("RequestId = " + response.getRequestId() + "\n");

    }catch (ClientException e){
        throw new GuliException(20001, "視頻刪除失敗");
    }
}

2、controller

@DeleteMapping("{videoId}")
public R removeVideo(@ApiParam(name = "videoId", value = "雲端視頻id", required = true)
                     @PathVariable String videoId){

    videoService.removeVideo(videoId);
    return R.ok().message("視頻刪除成功");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM