ApsaraVideo VOD is built based on OSS. After you activate ApsaraVideo VOD, a dedicated input bucket and a dedicated output bucket are allocated. The input bucket is used to store video or audio source files. The output bucket is used to store transcoded files, screenshots, and album arts, and is used to serve as the CDN origin site for VOD. You can upload images and other media resources to VOD. You can use ApsaraVideo Upload SDK to quickly upload media files.
ApsaraVideo Upload SDK has the following functions:
- Upload a local video to VOD. Multi-fragment upload is used by default. The maximum size of a single file that can be uploaded is 48.8 TB. Resumable upload is not supported currently.
- Upload a network video to VOD. You can specify a URL and the system automatically downloads the video from the URL and uploads it to VOD. The maximum size of a single file that can be uploaded is 5 GB.
- Upload a local m3u8 video (including all the fragment files) to VOD. You just need to specify the address of the local m3u8 index file.
- Upload a network m3u8 video (including all the fragment files) to VOD. You must specify the URLs of the m3u8 index file and fragment files.
- Upload a local image to VOD. The output bucket is used by default to facilitate distribution acceleration.
- Upload a network image to VOD. You can specify a URL and the system automatically downloads the image from the URL and uploads it to VOD.
- Automatically upload the file through the optimal route: You need to specify the region of the ECS instance on which the upload script is deployed. If the ECS instance and the storage are in the same region, typically in China (Shanghai), the file is automatically uploaded through the Intranet to accelerate the upload speed and save the public traffic.
- Unified exception handling. For external calls, the system only needs to detect AliyunVodException exceptions (in AliyunVodUtils.py).
- Easy log output. For more information, see the AliyunVodLog class (in AliyunVodUtils.py).
- You have installed Python 2.7 or a later version. If not, go to the Python official website and download the required version.
- You have installed PIP. If not, go to the PIP official website and install PIP.
Install dependency packages
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-vod
pip install oss2
If some operations failed due to permission issues, add sudo before the command.
Download the SDKDownload address: Download ApsaraVideo Upload SDK.Download a Python version. Decompress the package and copy voduploadsdk in the VodUploadSDK-Python directory. Then, you can use the SDK. The samples directory contains the sample code.
If the current SDK does not support the new operations or the new operations do not support the new functions, you can update it to the latest version.
- Update the dependency packages
pip install --upgrade aliyun-python-sdk-vod
pip install --upgrade oss2
- Download the latest ApsaraVideo Upload SDK Python edition.
- AliyunVodUtils.py file
- AliyunVodLog: the log class. It is implemented based on logging.
- AliyunVodUtils: the tool class. It provides static functions for truncating strings and obtaining extensions and file names.
- AliyunVodException: the exception class. It supports unified exception handling. You only need to detect this exception for external calls.
- UploadVideoRequest.py file.
- UploadVideoRequest: the upload video request class. For more information about the request and response parameters, see the CreateUploadVideo operation.
- UploadImageRequest.py file
- UploadImageRequest: the upload image request class. For more information about the request and response parameters, see the CreateUploadImage operation.
- AliyunVodUploader.py file, mainly includes operations in the AliyunVodUploader class.
- uploadLocalVideo: local video upload operation.
- uploadWebVideo: network video upload operation.
- uploadLocalM3u8: local m3u8 video upload operation.
- uploadWebM3u8: network m3u8 video upload operation.
- uploadImage: local or network image upload operation.
- parseWebM3u8: operation for parsing the fragmentation information of m3u8 network files.
- setMultipartUpload: operation for setting the threshold and fragment size in multi-fragment mode.
- uploadVideo.py: sample code for video upload.
- uploadImage.py: sample code for image upload.
The following uses uploading a local video and a network video file as an example:
# -*- coding: UTF-8 -*-
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest
# Test uploading of a local video
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath):
uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
uploadVideoRequest = UploadVideoRequest(filePath, 'test upload local video')
# Sets the video album art. For a local or network image, you can call UploadImageRequest to upload the image to VOD and obtain the ImageURL.
videoId = uploader.uploadLocalVideo(uploadVideoRequest)
print "file: %s, videoId: %s" % (filePath, videoId)
# Test uploading of a network video
def testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl):
uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
uploadVideoRequest = UploadVideoRequest(fileUrl, 'test upload web video')
videoId = uploader.uploadWebVideo(uploadVideoRequest)
print "file: %s, videoId: %s" % (fileUrl, videoId)
#### Execute the test code ####
accessKeyId = '<your AccessKeyId>'
accessKeySecret = '<your AccessKeySecret>'
localFilePath = '/Users/yours/Desktop/Video/sample.mov'
testUploadLocalVideo(accessKeyId, accessKeySecret, localFilePath)
fileUrl = 'http://vod-test2.cn-shanghai.aliyuncs.com/72f8b5e970024318b672f2e9c7f610f2/0e9fb231b82b41ef8703a5c1882a7478-5287d2089db37e62345123a1be272f8b.mp4'
testUploadWebVideo(accessKeyId, accessKeySecret, fileUrl)
For more sample code, see the samples directory in the VodUploadSDK-Python directory after decompression.