天天看點

Java實作阿裡雲視訊點播的功能

擷取視訊的播放位址

1.在阿裡雲中開通視訊點播功能,上傳一個測試視訊

Java實作阿裡雲視訊點播的功能

視訊基本資訊:(視訊ID)

Java實作阿裡雲視訊點播的功能

2.建立一個maven項目,引入依賴

<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.根據你自己的accessKeyId和accessKeySecret擷取client (不知到阿裡雲accessKeyId和accessKeySecret的可以參照前面的OSS文章提到過怎麼擷取)

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

4.擷取視訊的播放位址

public class testVod {
    public static void main(String[] args) {
        try {
            DefaultAcsClient client = InitObject.initVodClient("your accessKeyId","your accessKeySecret");
            //建立擷取視訊位址的request和response
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();

            //向request中傳入視訊id
            request.setVideoId("d6f12144360449b1935f01bb73e1f3fa");
            //調用初始化對象裡方法,傳遞request,擷取資料
            response = client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();

            for (GetPlayInfoResponse.PlayInfo playInfo:playInfoList
                 ) {
                System.out.println("URL= "+ playInfo.getPlayURL());
            }
            System.out.println("VidoBase title = "+response.getVideoBase().getTitle());

        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}
           

5.結果,根據擷取的URL可以直接播放

Java實作阿裡雲視訊點播的功能

(在這裡擷取的是不加密的視訊)

擷取視訊播放憑證(播放加密轉碼後的視訊)

public static void main(String[] args) {
        //根據視訊的ID擷取視訊的播放憑證
        try {
            DefaultAcsClient client = InitObject.initVodClient("accessKeyId","accessKeySecret");
            //建立擷取視訊憑證的request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();

            //向request中傳入視訊id
            request.setVideoId("c1fc08a30da742a7af6d4a891a63c9a8");
            // //調用初始化對象裡方法擷取憑證
            response = client.getAcsResponse(request);
            System.out.println("憑證= "+ response.getPlayAuth());

        } catch (ClientException e) {
            e.printStackTrace();
        }

    }
           
Java實作阿裡雲視訊點播的功能

上傳視訊到阿裡雲視訊點播服務

public static void main(String[] args) {
        //本地上傳
        UploadVideoRequest request = new UploadVideoRequest("accessKeyId", "accessKeySecret", "測試視訊", "C:/Users/jaksiont/Desktop/test.mp4");
        /* 可指定分片上傳時每個分片的大小,預設為2M位元組 */
        request.setPartSize(2 * 1024 * 1024L);
        /* 可指定分片上傳時的并發線程數,預設為1,(注:該配置會占用伺服器CPU資源,需根據伺服器情況指定)*/
        request.setTaskNum(1);

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

    }
           
Java實作阿裡雲視訊點播的功能

如果項目中做了Nginx代理一定要注意

問題:我在測試上傳視訊時,總是出現跨域問題,可是檢查了好久,跨域的配置都是正确的。一開始以為是Nginx修改了配置檔案沒有生效,可是測試了很久沒毛病啊,終于在今天早上找到了原因

配置nginx上傳檔案大小,否則上傳時會有 413 (Request Entity Too Large) 異常

Java實作阿裡雲視訊點播的功能

client_max_body_size 1024m;一定要在配置中加上這句,不加的話由于檔案限制,Nginx就不做請求轉發了!!!!!!