The ApsaraVideo VOD upload SDK allows you to upload media files easily and quickly. This topic provides an example to describe how to use the upload SDK for Python.

Features

The upload SDK for Python provides the following functionalities:
  • Allows you to upload various types of media files to ApsaraVideo VOD, including video and audio files, images, and auxiliary media assets such as watermarks and subtitle files.
  • Allows you to upload local media files to ApsaraVideo VOD. By default, multipart upload is used. A single file to be uploaded can be up to 48.8 TB in size. Resumable upload is not supported.
  • Allows you to upload online media files to ApsaraVideo VOD. A single file to be uploaded can be up to 48.8 TB in size. Online media files are downloaded to local temporary directories and then uploaded to ApsaraVideo VOD. Resumable upload is not supported.
  • Allows you to upload M3U8 video files. ApsaraVideo VOD can parse M3U8 index files to obtain the URLs of segmented files. You can also specify the URLs of segmented files.
The upload SDK for Python provides the following additional features:
  • Provides a progress bar for uploads and supports the callbacks for default and custom upload progress.
  • Allows you to specify the region of an Elastic Compute Service (ECS) instance on which to deploy the upload script. If the region of the ECS instance is the same as the storage region of ApsaraVideo VOD, videos are automatically uploaded over the internal network. This speeds up uploads and reduces Internet traffic.
  • Allows you to specify the VOD center and storage region to facilitate uploads that originate outside mainland China. The default VOD center is in the China (Shanghai) region.
  • Allows you to set the metadata, such as the title, storage location, user data, and transcoding template for the upload.

For more information about file formats that are supported by ApsaraVideo VOD, see Overview.

Prerequisites

Install the SDK

  1. Run the following commands to install dependencies:

    pip install aliyun-python-sdk-core

    pip install aliyun-python-sdk-vod

    pip install oss2

    Note If you are using Python 3.x, replace pip install aliyun-python-sdk-core with pip install aliyun-python-sdk-core-v3. If you have installed different versions, you can run the pip3 command.
  2. Download the VodUploadSDK-Python_1.3.1.zip package, which contains the upload SDK for Python and sample code. For more information, see SDK download.
    Note In this topic, the SDK V1.3.1 is used as an example. You can use other versions based on your business requirements.
  3. Decompress the VodUploadSDK-Python_1.3.1.zip package and copy voduploadsdk from the VodUploadSDK-Python_1.3.1 directory to your local installation directory.

Update the SDK

If new operations or new features of existing operations are unavailable in the current SDK, update the SDK to the latest version.

  1. Run the following commands to install dependencies:

    pip install --upgrade aliyun-python-sdk-vod

    pip install --upgrade oss2

  2. Download the latest version of the upload SDK for Python to overwrite the existing SDK. For more information, see SDK download.
    Note You can open the ChangeLog.txt file in the voduploadsdk directory to view the version number and release date of the current SDK.

SDK directories

  • voduploadsdk directory
    • AliyunVodUtils.py
      • AliyunVodLog: the log class of the upload SDK. It is implemented based on logging.
      • AliyunVodUtils: the utility class of the upload SDK.
      • AliyunVodException: the exception class of the upload SDK. It supports unified exception handling. You only need to detect AliyunVodException exceptions for external calls.
    • UploadVideoRequest.py

      UploadVideoRequest: the request class for uploading videos. For more information about the parameters, see CreateUploadVideo.

    • UploadImageRequest.py

      UploadImageRequest: the request class for uploading images. For more information about the parameters, see CreateUploadImage.

    • UploadAttachedMediaRequest.py

      UploadAttachedMediaRequest: the request class for uploading auxiliary media assets. For more information about the parameters, see CreateUploadAttachedMedia.

    • AliyunVodUploader.py includes operations of the AliyunVodUploader class.
      • uploadLocalVideo: an operation used to upload local videos.
      • uploadWebVideo: an operation used to upload online videos.
      • uploadLocalM3u8: an operation used to upload local M3U8 videos.
      • uploadWebM3u8: an operation used to upload online M3U8 videos.
      • uploadImage: an operation used to upload local or online images.
      • uploadAttachedMedia: an operation used to upload local or online auxiliary media assets.
      • parseWebM3u8: an operation used to parse segment information of online M3U8 videos.
      • parseLocalM3u8: an operation used to parse segment information of local M3U8 videos.
      • setApiRegion: an operation used to specify the access region of ApsaraVideo VOD. The default region is the China (Shanghai) region. You can also specify a region outside mainland China, such as the Singapore (Singapore) region. For more information, see VOD centers and endpoints.
      • setMultipartUpload: an operation used to set the threshold of the file size based on which multipart upload starts and the size of each part in multipart upload.
      • uploadProgressCallback: an operation used to set the callback for upload progress. This method can be overridden.
      • setEnableCrc: an operation to specify whether to enable cyclic redundancy check (CRC) during the upload. By default, CRC is enabled.
    • ChangeLog.txt: provides version history. You can obtain the version number and release date of the current SDK in the first line.
  • samples directory
    • uploadVideo.py: provides the sample code for video upload.
    • uploadImage.py: provides the sample code for image upload.

Examples

The following sample code shows you how to upload a local video and an online video:

# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest 

# Test the upload of a local video.
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
    try:
        # You can specify the ECS region where the upload script is deployed. If the ECS region is the same as the storage region of ApsaraVideo VOD, the local video is automatically uploaded over the internal network. This speeds up uploads and reduces Internet traffic. 
        # ecsRegionId ="cn-shanghai"
        # uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
        # You do not specify the ECS region where the upload script is deployed. 
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(filePath, 'test upload local video')
        # Specify the video thumbnail. If the video thumbnail is a local or online image, you can call the UploadImageRequest operation to upload the image to ApsaraVideo VOD and obtain the value of the ImageURL parameter.
        #uploadVideoRequest.setCoverURL('https://sample.com/sample.jpg')  
        #uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadLocalVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
        
    except AliyunVodException as e:
        print(e)
 
# Test the upload of an online video.
def testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl, storageLocation=None):
    try:
        uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
        uploadVideoRequest = UploadVideoRequest(fileUrl, 'test upload web video')
        uploadVideoRequest.setTags('tag1,tag2')
        if storageLocation:
            uploadVideoRequest.setStorageLocation(storageLocation)
        videoId = uploader.uploadWebVideo(uploadVideoRequest)
        print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
        
    except AliyunVodException as e:
        print(e)

####   Run the test code.   ####   
accessKeyId = '<AccessKeyId>'
accessKeySecret = '<AccessKeySecret>'

localFilePath = '/opt/video/sample.mp4'
testUploadLocalVideo(accessKeyId, accessKeySecret, localFilePath)

fileUrl = 'http://sample.oss.aliyuncs.com/video/sample.mp4'
#testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)

For more sample code, see the files in the samples directory.