全部产品
Search
文档中心

视频点播:媒体上传

更新时间:Jul 31, 2023

点播服务端提供丰富的上传接口,支持开发者采用不同的上传方式实现媒体上传。本文介绍Python SDK调用服务端上传接口的使用场景和调用示例。

使用场景

重要

本文仅提供API调用示例,即在点播服务中获取上传凭证和地址等操作示例。要实现完整的媒体文件上传逻辑,请参考下表跳转至对应的上传方式和使用场景。

接口

使用场景

获取音视频上传地址和凭证

  • OSS原生SDK上传场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证需要Base64解码后作为初始化OSS上传客户端实例的参数。详情请参见基于OSS原生SDK上传。其中,Java语言提供了基于OSS原生SDK上传的完整的媒体文件上传逻辑的Demo示例,其他语言可以参考实现,详情请参见通过视频点播API上传媒资文件

  • 点播客户端上传SDK场景下,用户应用服务器集成点播服务端SDK并调用相关接口获取或刷新上传地址和凭证。获取到的上传地址和凭证无需解码即可下发给客户端。详情请参见客户端上传SDK指引

刷新音视频上传凭证

获取图片上传地址和凭证

获取辅助媒资上传地址和凭证

URL批量拉取上传

  • 调用URL批量拉取上传接口实现离线拉取上传。这种方式可以免去先将文件下载到本地服务器或终端再使用上传SDK上传到点播的过程。

    说明

    URL拉取上传是异步任务,非实时,不保证时效性,一般提交后会在数小时、甚至数天内完成迁移上传。

  • 调用获取URL上传信息获取上传进度。

获取URL上传信息

注册媒资信息

前提条件

  • 调用接口前请先安装SDK。操作指引请参见安装

  • 调用接口前请先初始化客户端实例。操作指引请参见初始化

说明

以下示例均通过AccessKey初始化客户端实例。

接口调用说明

  • 本文提供的接口调用示例均通过AccessKey初始化客户端实例。

  • 接口的参数解释和返回字段的详细说明请访问阿里云OpenAPI门户,在各接口右侧的文档页签查看。

  • 本文仅提供部分复杂接口的代码示例,其余接口的SDK代码示例,可以通过阿里云OpenAPI门户获取。访问阿里云OpenAPI门户,在接口的左侧参数配置页签,填写需要的参数信息并发起调用后,在右侧的SDK示例页签,选择目标语言,查看并下载示例代码。

接口调用示例

获取音/视频上传地址和凭证

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