Alibaba Cloud VOD video on demand

1. Activate Alibaba Cloud VOD

Novice guide document: https://help.aliyun.com/zh/vod/getting-started/getting-started?spm=a2c4g.11186623.0.0.46c751a6bH1ffm#section-ixw-emd-fye

1. Activate VOD

ApsaraVideo VoD (VOD for short) is a one-stop audio and video on demand solution integrating video collection, editing, uploading, media resource management, automatic transcoding processing, video audit analysis, and distribution acceleration. Log in to Alibaba Cloud, find VOD in the product, click to activate, and enter the management console
image.png

2. Storage management

  1. In the configuration management area on the left navigation bar of the VOD console, select Media Asset Management Configuration > Storage Management.
  2. In the service area of ​​the menu bar above the VOD console, select the area you want to subscribe to (for example, select East China 2 (Shanghai)).
  3. Click Enable to allocate storage bucket.

image.png

  1. Click Manage, and configure permissions, return-to-origin addresses, and other settings according to your needs.

image.png

3. Encoding settings

The transcoding template can be used for video transcoding processing, and the parameters set in the template will be used as parameters for video processing. You can create various types of transcoding templates for video transcoding.

  1. In the configuration management area on the left navigation bar of the VOD console, select Media Processing Configuration > Transcoding Template Group. After the storage management is activated in this service area, the system will automatically add non-transcoding template group and TranscodeTemplateGroup transcoding template group for you.
  2. Click Add Transcoding Template Group to enter the Add Transcoding Template Group page.
  3. Enter the name of the template group, click Add Template in the common transcoding template area, and you can configure the transcoding template according to the prompts.

image.png

  1. Click Save, and the interface automatically jumps to the transcoding template group page. On the Transcoding Template Group page, a newly created transcoding template group already exists. You can edit and delete transcoding template groups.

image.png

4. Video management

Uploading audio and video refers to uploading audio and video to your VOD storage bucket. After the upload is complete, the audio and video can be processed and distributed.

  1. In the Media Library area of ​​the left navigation bar of the VOD console, click Audio/Video .
  2. Click Upload Audio/Video to enter the upload audio/video page.
  3. Click Add Audio/Video to select the upload method and processing type (transcoding template or workflow).
    • Local upload: You need to select the file to be uploaded to the on-demand service by clicking or dragging the file.
    • URL pull (supported only in Shanghai region): You need to fill in the URL, extension and audio/video name of the file to be uploaded.
  4. Click Start Upload . The video upload status will be displayed on the upload audio/video page. When the status changes from uploading to uploading successfully , it means the video has been uploaded. You can return to the audio/video page to view the uploaded videos.

Note As long as transcoding is involved in the upload process, transcoding fees will be incurred. If you don't want to involve costs, it is recommended that you use a template group that does not transcode .

image.png

5. Manage AccessKey

Click the avatar -> AccessKey Management to enter the key management interface.
image.png
AccessKey ID and AccessKey Secret are your keys to access the Alibaba Cloud API. They have full permissions for this account . It is recommended to use the sub-user AccessKey.
image.png
Click 开始使用子用户AccessKeyCreate User
image.png
to create a user, specify the user name, and API permissions, click OK
image.png
After creating an account, first copy the key and use it later,
image.png
then click the user name, enter the user details page to find permission management - click Add authorization - select permissions - add VOD-related permissions to the right panel ,as follows
image.png

Here our account is created successfully.

2. Video on demand

1. Getting started code

Upload based on OSS native SDK, refer to the document: https://help.aliyun.com/zh/vod/user-guide/upload-media-files-by-using-oss-sdks?spm=a2c4g.11186623.0.0.1f02273fj4lxNJ

VOD provides a variety of upload methods for developers. The upload SDK (server and client) encapsulates the basic logic related to upload, and only needs simple configuration to realize the upload function. It is recommended to use

Uploading based on the OSS native SDK requires the developer to implement all upload logic by himself, including obtaining the upload address and certificate from the on-demand service, Base64 decoding the upload address and certificate, and calling the OSS capability to complete the upload.
Taking the integration of the on-demand server SDK to obtain the upload address and certificate as an example, the complete upload process is shown in the figure below:
image.png
Process Details

  1. The upload application server uses the RAM user AK method (AccessKey ID and AccessKey Secret) or STS temporary AK to initialize the VOD client.
  2. The upload application server uses the on-demand server SDK to call the interface related to the upload address and certificate to obtain the upload address, upload certificate and media information.
  3. The on-demand service returns Base64-encrypted upload address (UploadAddress), upload certificate (UploadAuth), and media ID in the request result. The upload application server can parse the parameters according to the returned results as input parameters for initializing the OSS native SDK.
  4. The upload application server uses the parsed upload address (UploadAddress) and authorization information (UploadAuth) to initialize the OSS client through STS .
  5. The upload application server uses the OSS SDK to call the OSS upload related interface to upload media files to the specified storage address.
  6. The OSS service returns the upload result.

Java entry case: https://help.aliyun.com/zh/vod/user-guide/upload-media-files-by-using-oss-sdks?spm=a2c4g.11186623.0.0.24222dadolkgd8#section-iny-ln9 -m7z

<dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-core</artifactId>
      <version>4.6.0</version>
  </dependency>
  <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-vod</artifactId>
      <version>2.16.10</version>
  </dependency>
  <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.83</version>
  </dependency>
  <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>aliyun-java-sdk-kms</artifactId>
      <version>2.10.1</version>
  </dependency>
  <dependency>
      <groupId>com.aliyun.oss</groupId>
      <artifactId>aliyun-sdk-oss</artifactId>
      <version>3.15.1</version>
  </dependency>

The following is the entry code for uploading videos based on OSS

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoRequest;
import com.aliyuncs.vod.model.v20170321.CreateUploadVideoResponse;
import org.apache.commons.codec.binary.Base64;

import java.io.File;

/**
 * descript
 */
public class UploadDemo {
    
    

    //初始化VOD客户端
    public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
    
    
        // 根据点播接入服务所在的Region填写,例如:接入服务在上海,则填cn-shanghai;其他区域请参见存储说明。
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    //创建视频上传结果对象
    public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient vodClient) throws ClientException {
    
    
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setFileName("vod_test.mp4");
        request.setTitle("this is title");
        //设置请求超时时间
        request.setSysReadTimeout(1000);
        request.setSysConnectTimeout(1000);
        return vodClient.getAcsResponse(request);
    }

    //初始OSS客户端
    public static OSSClient initOssClient(JSONObject uploadAuth, JSONObject uploadAddress) {
    
    
        String endpoint = uploadAddress.getString("Endpoint");
        String accessKeyId = uploadAuth.getString("AccessKeyId");
        String accessKeySecret = uploadAuth.getString("AccessKeySecret");
        String securityToken = uploadAuth.getString("SecurityToken");
        return new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
    }
    //上传文件
    public static void uploadLocalFile(OSSClient ossClient, JSONObject uploadAddress, String localFile) {
    
    
        String bucketName = uploadAddress.getString("Bucket");
        String objectName = uploadAddress.getString("FileName");
        File file = new File(localFile);
        ossClient.putObject(bucketName, objectName, file);
    }

    public static void main(String[] argv) {
    
    
        //您的AccessKeyId
        String accessKeyId = "xxxxx";
        //您的AccessKeySecret
        String accessKeySecret = "xxxxx";
        //需要上传到VOD的本地视频文件的完整路径,需要包含文件扩展名
        String localFile = "D:\\临时资料\\xxx.mp4";
        try {
    
    
            // 初始化VOD客户端并获取上传地址和凭证
            DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
            //创建一个相应对象
            CreateUploadVideoResponse createUploadVideoResponse = createUploadVideo(vodClient);
            // 执行成功会返回VideoId、UploadAddress和UploadAuth
            String videoId = createUploadVideoResponse.getVideoId();
            //解析拿到结果对象
            JSONObject uploadAuth = JSONObject.parseObject(decodeBase64(createUploadVideoResponse.getUploadAuth()));
            JSONObject uploadAddress = JSONObject.parseObject(decodeBase64(createUploadVideoResponse.getUploadAddress()));

            // 使用UploadAuth和UploadAddress初始化OSS客户端
            OSSClient ossClient = initOssClient(uploadAuth, uploadAddress);
            // 上传文件,注意是同步上传会阻塞等待,耗时与文件大小和网络上行带宽有关
            uploadLocalFile(ossClient, uploadAddress, localFile);
            System.out.println("Put local file succeed, VideoId : " + videoId);
        } catch (Exception e) {
    
    
            System.out.println("Put local file fail, ErrorMessage : " + e.getLocalizedMessage());
        }
    }

    private static String decodeBase64(String data) {
    
    
        return new String(Base64.decodeBase64(data));
    }
}

Note that there are three places that need to be modified

  • accessKeyId : your AccessKeyId
  • accessKeySecret: your AccessKeySecret
  • String regionId = “cn-shanghai”; This is the region, you need to go to the VOD console - storage management to find the upload address. For example, mine is: xxxxxxx[.oss-cn-shanghai.aliyuncs.com](https://vod.console.aliyun.com/?spm=5176.12818093_-1363046575.top-nav.22.3be916d0sNjf6B#/storage/outin-e978d136329c11ee80ad00163e1a3b4a.oss-cn-shanghai.aliyuncs.com/manage)East China Shanghai, then the regionId should be cn-shanghai

After the upload is successful, you can see the video ID from the console, which is required for playback
image.png

2. Play video

Go to the VOD console - media library - find the uploaded video
image.png
and click manage - you can see the basic situation of the video in the basic information, including the video ID, storage address, click on the web player code, the official provides the Html and JS script
image.png
Create a pay.html file on the computer desktop, then copy the player code into it, and then need to specify 2 things

  • vid : the id of the video
  • payauth : play credentials

image.png

3. Get payauth

Official note: Please copy the code and add the playauth value before using it. For how to obtain playauth, please refer to the document to obtain the playback certificate . We obtain the playback certificate according to the document. The document address: https://help.aliyun.com/zh/vod/developer -reference/api-vod-2017-03-21-getvideoplayauth , here you can try debugging first - get the playback certificate
image.png
, enter the debugging page, select the server address, specify the VideoId video ID, and then click to initiate the call to get the playback certificate.
image.png
Copy the playback credentials to the player code, refresh the page and try to play, and it can be seen from the network that the method of video-on-demand and side-by-side playback is adopted.
image.png

You can directly download the official code in the debug-SDK instance to obtain the playback certificate code,
image.png
and you need to import dependencies


<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>vod20170321</artifactId>
    <version>2.16.16</version>
</dependency>

Get play voucher code

//获取播放凭证
public static String getPayAuth(String videoId) throws Exception {
    
    
    // 请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID 和 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
    // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例使用环境变量获取 AccessKey 的方式进行调用,仅供参考,建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html
    com.aliyun.vod20170321.Client client =
            createClient("你的accessKeyId", "你的accessKeySecrt");

    com.aliyun.vod20170321.models.GetVideoPlayAuthRequest getVideoPlayAuthRequest =
            new com.aliyun.vod20170321.models.GetVideoPlayAuthRequest().setVideoId(videoId);

    com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
    // 复制代码运行请自行打印 API 的返回值
    GetVideoPlayAuthResponse videoPlayAuthWithOptions = client.getVideoPlayAuthWithOptions(getVideoPlayAuthRequest, runtime);
    String playAuth = videoPlayAuthWithOptions.getBody().getPlayAuth();
    return playAuth;
}

I believe you have finished running the process according to the document, and then you can connect the code to your project

Guess you like

Origin blog.csdn.net/u014494148/article/details/132144301