All Products
Search
Document Center

ApsaraVideo VOD:Media upload

Last Updated:Aug 22, 2023

ApsaraVideo VOD servers provide various API operations for media upload. You can call the API operations to upload media files to ApsaraVideo VOD. This topic describes the scenarios in which you can use the server operation SDK for Python to call API operations to upload media files and provides sample code.

Scenarios

Important

This topic provides only the examples of calling API operations, such as the operation for obtaining an upload credential and an upload URL. The following table describes the scenarios in which API operations can be called to upload media files.

API operation

Scenario

CreateUploadVideo

  • If you upload media files by using Object Storage Service (OSS) SDKs, you must use the server operation SDK and call an operation to obtain an upload credential and an upload URL. The obtained URL and credential are Base64-encoded and must be decoded before they are used to initialize an OSSClient instance. For more information, see Upload media files by using OSS SDKs. The preceding topic provides complete sample code for uploading media files by using OSS SDK for Java. For more information about the sample code in other languages, see Upload media files by calling the ApsaraVideo VOD API.

  • If you upload media files from clients by using the upload SDK, you need to integrate the server operation SDK and call an operation to obtain or update an upload credential and an upload URL. You can issue URLs and credentials to clients without the need to decode them. For more information, see Client upload SDKs.

RefreshUploadVideo

CreateUploadImage

CreateUploadAttachedMedia

UploadMediaByURL

  • You can call the UploadMediaByURL operation to upload media files by using the URLs of source files. This way, you do not need to download media files to your servers or devices before you use the upload SDK to upload the media files to ApsaraVideo VOD.

    Note

    The URL-based upload jobs are asynchronous. After you submit a URL-based upload job, it may take hours, even days to complete.

  • You can call the GetURLUploadInfos operation to query the progress of URL-based upload jobs.

GetURLUploadInfos

RegisterMedia

  • After you add your OSS bucket to ApsaraVideo VOD and upload media files to the bucket, you can call the RegisterMedia operation to obtain the file IDs. After you register the media files, you can submit transcoding or snapshot jobs for the media files. For more information, see Add a user-created OSS bucket.

Prerequisites

  • The server operation SDK is installed. For more information, see Installation.

  • The server operation SDK is initialized. For more information, see Initialization.

Note

In the following examples, an AccessKey pair is used to initialize the server operation SDK.

Usage notes

  • In this example, an AccessKey pair is used to initialize a client instance.

  • For more information about the request and response parameters of this operation, visit OpenAPI Explorer. You can click API Documentation in the top navigation bar to view information related to the API operation.

  • This topic provides sample code only for some complex API operations. To obtain sample code for other API operations, perform the following operations: Visit Alibaba Cloud OpenAPI Explorer. In the left-side navigation pane, find the API operation whose sample code you want to obtain and specify the required parameters on the Parameters tab. Then, click Initiate Call. On the SDK Sample Code tab, select the language to view and download the sample code.

API call examples

Obtain a URL and a credential for uploading a media file

You can call the CreateUploadVideo operation to obtain an upload URL and an upload credential for uploading a media file.

Click CreateUploadVideo to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest
def create_upload_video(clt):
    request = CreateUploadVideoRequest.CreateUploadVideoRequest()
    request.set_Title('Video Title')
    request.set_FileName('/opt/video/sample/video_file.mp4')
    request.set_Description('Video Description')
    # The thumbnail URL. Example: http://192.168.0.1/16/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png.
    request.set_CoverURL('<your Cover URL>')
    request.set_Tags('tag1,tag2')
    request.set_CateId(0)

    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client()
    uploadInfo = create_upload_video(clt)
    print(uploadInfo['UploadAuth'])
    print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Update the upload credential

You can call the RefreshUploadVideo operation to update the credential for uploading a media file.

Click RefreshUploadVideo to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import RefreshUploadVideoRequest
def refresh_upload_video(clt, videoId):
    request = RefreshUploadVideoRequest.RefreshUploadVideoRequest()
    request.set_VideoId(videoId)
    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    uploadInfo = refresh_upload_video(clt, 'VideoId')
    print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Obtain a URL and a credential for uploading an image

You can call the CreateUploadImage operation to obtain a URL and a credential for uploading an image.

Click CreateUploadImage to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import CreateUploadImageRequest
def create_upload_image(clt, imageType, imageExt):
    request = CreateUploadImageRequest.CreateUploadImageRequest()
    request.set_ImageType(imageType)
    request.set_ImageExt(imageExt)
    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    imageInfo = create_upload_image(clt, 'cover', 'jpg')
    print(json.dumps(imageInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Obtain a URL and a credential for uploading an auxiliary media asset

You can call the CreateUploadAttachedMedia operation to obtain a URL and a credential for uploading an auxiliary media asset.

Click CreateUploadAttachedMedia to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import CreateUploadAttachedMediaRequest
def create_upload_attached_media(clt):
    request = CreateUploadAttachedMediaRequest.CreateUploadAttachedMediaRequest()
    request.set_BusinessType("watermark");
    request.set_MediaExt("gif");
    request.set_Title("this is a sample");
    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    uploadInfo = create_upload_attached_media(clt)
    print(uploadInfo['UploadAuth'])
    print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Upload multiple media files by using the URLs of source files at a time

You can call the UploadMediaByURL operation to upload multiple media files by using the URLs of source files at a time.

Click UploadMediaByURL to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import UploadMediaByURLRequest
import sys,urllib
def upload_media_by_url(clt):
    request = UploadMediaByURLRequest.UploadMediaByURLRequest()
    # The URLs of source files. Examples: http://example-bucket-****.cn-shanghai.aliyuncs.com/sample1-****.mp4 and http://example-bucket-****.cn-shanghai.aliyuncs.com/sample2-****.flv.
    urls = ["<your URL1>",
            "your URL2"]
    # Encode the urls and add metadata
    uploadUrls = []
    uploadMetadatas = []
    for url in urls:
        if sys.version_info[0] == 3:
            encodeUrl = urllib.parse.quote(url)
        else:
            encodeUrl = urllib.quote(url)
        uploadUrls.append(encodeUrl)
        uploadMetadatas.append({'SourceURL': encodeUrl, 'Title': 'Sample Title'})

    request.set_UploadURLs(','.join(uploadUrls))
    request.set_UploadMetadatas(json.dumps(uploadMetadatas))

    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    uploadInfo = upload_media_by_url(clt)
    print(uploadInfo['UploadJobs'])
    print(json.dumps(uploadInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Query information about URL-based upload jobs

You can call the GetURLUploadInfos operation to query the information about URL-based upload jobs.

Sample code:

from aliyunsdkvod.request.v20170321 import GetURLUploadInfosRequest
import sys,urllib
def get_url_upload_infos(clt):
    request = GetURLUploadInfosRequest.GetURLUploadInfosRequest()
    # The URLs. Example: http://192.168.0.1/16/sample1-****.mp4 and http://192.168.0.1/16/sample2-****.flv.
    urls = ["<your URL1>",
            "<your URL2>"]

    # Encode URLs.
    uploadUrls = []
    for url in urls:
        if sys.version_info[0] == 3:
            encodeUrl = urllib.parse.quote(url)
        else:
            encodeUrl = urllib.quote(url)
        uploadUrls.append(encodeUrl)

    # The URLs from which you want to upload media files. Separate multiple URLs with commas (,).
    request.set_UploadURLs(','.join(uploadUrls))

    # The IDs of the URL-based upload jobs.
    #request.set_JobIds("jobId1,jobId2")

    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    res = get_url_upload_infos(clt)
    print(res['NonExists'])
    print(json.dumps(res, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())

Register media assets

You can call the RegisterMedia operation to register media assets.

Click RegisterMedia to learn more about this operation.

Sample code:

from aliyunsdkvod.request.v20170321 import RegisterMediaRequest
def register_media(clt):
    request = RegisterMediaRequest.RegisterMediaRequest()
    metadatas = []
    # The URL of the media file. Example: https://192.168.0.1/16/vod_sample_****.mp4.
    metadatas.append({'FileURL':"<your File URL>", "Title":"RegisterMedia sample Title"})
    request.set_RegisterMetadatas(json.dumps(metadatas))
    request.set_accept_format('JSON')
    return json.loads(clt.do_action_with_exception(request))

try:
    clt = init_vod_client()
    registerInfo = register_media(clt)
    print(registerInfo['RegisteredMediaList'])
    print(registerInfo['FailedFileURLs'])
    print(json.dumps(registerInfo, ensure_ascii=False, indent=4))

except Exception as e:
    print(e)
    print(traceback.format_exc())