Help Center/ Video On Demand/ API Reference/ Application Examples/ Example 1: Uploading a Media File Less Than 20 MB
Updated on 2024-12-04 GMT+08:00

Example 1: Uploading a Media File Less Than 20 MB

Scenario

If you need to process (such as publish and transcode) your local media files in VOD, then you can call VOD APIs and OBS APIs to upload them to OBS buckets owned by VOD.

This section describes how to upload a video file less than 20 MB by calling APIs.

Prerequisites

  • You have specified the region where the VOD service for uploading media assets is located. See Before You Start.
  • You have obtained the ID of the project by referring to Obtaining a Project ID.

Overall Process

Figure 1 Media asset upload process
  1. Call the API for uploading media assets to VOD to create a media asset.
  2. Upload a local media asset using PUT.
  3. Call the API for confirming media asset upload.

Procedure

  1. Obtain a user token and use it to authenticate the calling of VOD APIs.

    For details, see Making an API Request. CN North-Beijing4 is used as an example. If you need to call a VOD API in another region, replace the endpoint with the IAM endpoint of the corresponding region.

    POST https://iam.cn-north-4.myhuaweicloud.com/v3/auth/tokens
    Content-Type: application/json
    
    {
      "auth": {
        "identity": {
          "methods": [
            "password"
          ],
          "password": {
            "user": {
              "name": "username", 
              "password": "password", 
              "domain": {
                "name": "domainname"   
              }
            }
          }
        },
        "scope": {
          "project": {
             "name": "projectname"
          }
        }
      }
    }
    As shown in Figure 2, information in the red box indicates the user token.
    Figure 2 Obtaining the user token

  2. Call the API for uploading media assets to VOD to create a media asset. Add the X-Auth-Token field to the request header. The value of Value is obtained in 1.

    POST https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset
    
    {
    	"title": "test",
    	"description": "test",
    	"category_id": 87748, 
    	"video_name": "test.mp4",
    	"video_type": "MP4",
    	"cover_type": "PNG",
    	"tags":"mytags,test",
    	"auto_publish": 0,    
    	"template_group_name":"test",
    	"subtitles":[      
    		{	
    			"id":1,
    			"language":"CN", 
    			"type": "SRT", 
    			"md5": "SqcyFjJZoDZaP8oKIY6rgQ==",    
    			"description":"AAAAA" ,
    			"name":"cc.srt"
    		}  
    	],
    	"thumbnail":{
    		"type":"time",
    		"cover_position":1
    	
    	},
    	"review":{
    		"interval":10,
    		"politics":-1,
    		"terrorism":-1,
    		"porn":-1
    	}	
    }
    • vod.cn-north-4.myhuaweicloud.com is the endpoint of VOD. You can obtain the endpoint from Before You Start.
    • category_id indicates the media asset category ID. You need to create a media category in advance by referring to Creating a Media Asset Category.
    • template_group_name indicates the transcoding template group name. You need to customize a transcoding template in advance by referring to Customizing a Transcoding Template.

      The region where an IAM API is called must be the same as the region where a VOD API is called. For example, the user token obtained when you call an IAM API in the CN North-Beijing4 region can only be used to authenticate the requests for calling VOD APIs in the CN North-Beijing4 region.

  3. Obtain the values of video_upload_url, cover_upload_url, and subtitle_upload_urls from the response parameters.

    {
      "asset_id": "3f40a2c7c60454f5f84381e0313ca230",
      "video_upload_url": "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cdeda86cd1b7b3dd760a3ff28a5ee497.mp4?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=9f%2BZcdD6SwjIU5ARHYiP6YY1Lyw%3D",
      "cover_upload_url": "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cover/Cover0.png?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=4Aa88NK%2By%2By1Xo0RvLpOvuaFCoE%3D",
      "subtitle_upload_urls": [
        "https://vod-bucket-81.obs.cn-north-4.myhuaweicloud.com:443/474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/subtitle/1.srt?AWSAccessKeyId=BG923RWHL4HFXOGKCVAL&Expires=1560420274&Signature=l0UclE9yfaVrxkl0kaNnr%2BemG98%3D"
      ],
      "target": {
        "bucket": "vod-bucket-81",
        "location": "cn-north-4",
        "object": "474bcff2992f4be5b883a2fb9cec7343/3f40a2c7c60454f5f84381e0313ca230/cdeda86cd1b7b3dd760a3ff28a5ee497.mp4"
      }
    }

  4. Send PUT requests to upload media assets, thumbnails, and subtitle files to video_upload_url, cover_upload_url, and subtitle_upload_urls.

    Set Content-Type based on the type of the file to be uploaded.
    • Video file: video/video format, for example, video/mp4. For details about how to fill in the request header of a video file format, see Table 1.
    • Audio file: audio/audio format, for example, audio/mp3. For details about how to fill in the request header of an audio file format, see Table 2.
    • Image file: image/image format, for example, image/png.
    • Subtitle file: application/octet-stream
    Table 1 Parameters in the request header of a video file

    File Name Extension

    Content-Type

    MP4

    video/mp4

    MOV

    video/quicktime

    MXF

    application/mxf

    TS

    video/mp2t

    MPG

    video/mpeg

    FLV

    video/flv

    WMV

    video/x-ms-wmv

    AVI

    video/x-msvideo

    M4V

    video/m4v

    F4V

    application/f4v

    MPEG

    video/mpeg

    M3U8

    application/octet-stream

    _3GP/3GP

    video/3gpp

    ASF

    video/x-ms-asf

    MKV

    video/x-matroska

    WEBM

    video/webm

    MPD

    video/dash

    Table 2 Parameters in the request header of an audio file

    File Name Extension

    Content-Type

    MP3

    audio/mp3

    WMA

    audio/wma

    APE

    audio/ape

    FLAC

    audio/flac

    AAC

    audio/aac

    AC3

    audio/ac3

    MMF

    audio/mmf

    AMR

    audio/amr

    M4A

    audio/m4a

    M4R

    audio/m4r

    OGG

    audio/ogg

    WAV

    audio/wav

    WV

    audio/wv

    MP2

    audio/mp2

  5. Call the API for confirming media asset upload.

    POST https://vod.cn-north-4.myhuaweicloud.com/v1.0/{project_id}/asset/status/uploaded
    
    {
    	 "asset_id": "0f4d3f1f32ec353d8866f2d84a036124",
    	 "status":"CREATED"
    }

After a media asset was uploaded, you can view details about the media asset on the VOD console.