全部產品
Search
文件中心

ApsaraVideo VOD:媒體上傳

更新時間:Jul 13, 2024

點播服務端提供豐富的上傳介面,支援開發人員採用不同的上傳方式實現媒體上傳。本文介紹Python SDK調用服務端上傳介面的使用情境和調用樣本。

使用情境

重要

本文僅提供API調用樣本,即在點播服務中擷取上傳憑證和地址等操作樣本。要實現完整的媒體檔案上傳邏輯,請參考下表跳轉至對應的上傳方式和使用情境。

介面

使用情境

擷取音視頻上傳地址和憑證

  • OSS原生SDK上傳情境下,使用者應用伺服器整合點播服務端SDK並調用相關介面擷取或重新整理上傳地址和憑證。擷取到的上傳地址和憑證需要Base64解碼後作為初始化OSS上傳用戶端執行個體的參數。詳情請參見基於OSS原生SDK上傳。其中,Java語言提供了基於OSS原生SDK上傳的完整的媒體檔案上傳邏輯的Demo樣本,其他語言可以參考實現,詳情請參見通過ApsaraVideo for VODAPI上傳媒體檔案

  • 點播用戶端上傳SDK情境下,使用者應用伺服器整合點播服務端SDK並調用相關介面擷取或重新整理上傳地址和憑證。擷取到的上傳地址和憑證無需解碼即可下發給用戶端。詳情請參見用戶端上傳SDK指引

重新整理音視頻上傳憑證

擷取圖片上傳地址和憑證

擷取輔助媒資上傳地址和憑證

URL批量拉取上傳

  • 調用URL批量拉取上傳介面實現離線拉取上傳。這種方式可以免去先將檔案下載到本機伺服器或終端再使用上傳SDK上傳到點播的過程。

    說明

    URL拉取上傳是非同步任務,非即時,不保證時效性,一般提交後會在數小時、甚至數天內完成遷移上傳。

  • 調用擷取URL上傳資訊擷取上傳進度。

擷取URL上傳資訊

註冊媒資資訊

前提條件

  • 調用介面前請先安裝SDK。操作指引請參見安裝

  • 調用介面前請先初始化用戶端執行個體。操作指引請參見初始化

說明

以下樣本均通過AccessKey初始化用戶端執行個體。

介面調用說明

  • 本文提供的介面調用樣本均通過AccessKey初始化用戶端執行個體。

  • 介面的參數解釋和返回欄位的詳細說明請訪問阿里雲OpenAPI門戶,在各介面右側的文檔頁簽查看。

  • 本文僅提供部分複雜介面的程式碼範例,其餘介面的SDK程式碼範例,可以通過阿里雲OpenAPI門戶擷取。訪問阿里雲OpenAPI門戶,在介面的左側參數配置頁簽,填寫需要的參數資訊並發起調用後,在右側的SDK樣本頁簽,選擇SDK版本,選擇目標語言,查看並下載範例程式碼。

  • 本文均以V1.0版本的SDK為例進行介面調用,如需擷取V2.0版本的SDK樣本,請在通過阿里雲OpenAPI門戶擷取SDK樣本時,指定到對應的SDK版本。image.png

介面調用樣本

擷取音/視頻上傳地址和憑證

調用CreateUploadVideo介面,完成擷取音/視頻上傳地址和憑證功能。

阿里雲OpenAPI門戶地址:CreateUploadVideo

調用樣本如下:

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')
    # CoverURL樣本:"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())

重新整理音/視頻上傳憑證

調用RefreshUploadVideo介面,完成重新整理音/視頻上傳憑證功能。

阿里雲OpenAPI門戶地址:RefreshUploadVideo

調用樣本如下:

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())

擷取圖片上傳地址和憑證

調用CreateUploadImage介面,完成擷取圖片上傳地址和憑證功能。

阿里雲OpenAPI門戶地址:CreateUploadImage

調用樣本如下:

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())

擷取輔助媒資上傳地址和憑證

調用CreateUploadAttachedMedia介面,完成擷取輔助媒資上傳地址和憑證功能。

阿里雲OpenAPI門戶地址:CreateUploadAttachedMedia

調用樣本如下:

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())

URL批量拉取上傳

調用UploadMediaByURL介面,完成URL批量拉取上傳功能。

阿里雲OpenAPI門戶地址:UploadMediaByURL

調用樣本如下:

from aliyunsdkvod.request.v20170321 import UploadMediaByURLRequest
import sys,urllib
def upload_media_by_url(clt):
    request = UploadMediaByURLRequest.UploadMediaByURLRequest()
    # URL樣本:"http://example-bucket-****.cn-shanghai.aliyuncs.com/sample1-****.mp4","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())

擷取URL上傳資訊

調用GetURLUploadInfos介面,完成擷取URL上傳資訊功能。

調用樣本如下:

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

    # 對URL進行編碼
    uploadUrls = []
    for url in urls:
        if sys.version_info[0] == 3:
            encodeUrl = urllib.parse.quote(url)
        else:
            encodeUrl = urllib.quote(url)
        uploadUrls.append(encodeUrl)

    # 設定上傳的URL列表,用逗號分隔
    request.set_UploadURLs(','.join(uploadUrls))

    # 也可以傳入jobId查詢
    #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())

註冊媒資資訊

調用RegisterMedia介面,完成註冊媒資資訊功能。

阿里雲OpenAPI門戶地址:RegisterMedia

調用樣本如下:

from aliyunsdkvod.request.v20170321 import RegisterMediaRequest
def register_media(clt):
    request = RegisterMediaRequest.RegisterMediaRequest()
    metadatas = []
    # FlieURL樣本: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())