全部产品

媒资管理

更新时间:2021-02-18 10:25

本篇文档提供了Python SDK媒资管理模块相关功能的API调用示例。主要包含搜索媒资信息、获取视频信息、修改视频信息、删除视频、获取源文件信息、获取图片信息、删除图片信息等。

初始化客户端

使用前请先初始化客户端,请参见初始化

搜索媒资信息

调用SearchMedia接口,完成搜索媒资信息功能。

接口参数和返回字段请参见SearchMedia。调用示例如下:

from aliyunsdkvod.request.v20170321 import SearchMediaRequest
def search_media(clt):
    request = SearchMediaRequest.SearchMediaRequest()
    request.set_Fields('Title,CoverURL,Status');
    request.set_Match('Status in ("Normal","Checking") and CreationTime = ("2018-10-01T08:00:00Z","2018-12-25T08:00:00Z")')
    request.set_PageNo(1)
    request.set_PageSize(10)
    request.set_SearchType('video')
    request.set_SortBy('CreationTime:Desc')

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    medias = search_media(clt)
    print(medias['MediaList'])
    print(json.dumps(medias, ensure_ascii=False, indent=4))

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

获取视频信息

调用GetVideoInfo接口,完成获取视频信息功能。

接口参数和返回字段请参见GetVideoInfo。调用示例如下:

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    videoInfo = get_video_info(clt, '<videoId>')
    print(json.dumps(videoInfo, ensure_ascii=False, indent=4))

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

批量获取视频信息

调用GetVideoInfos接口,完成批量获取视频信息功能。

接口参数和返回字段请参见GetVideoInfos。调用示例如下:

from aliyunsdkvod.request.v20170321 import GetVideoInfosRequest
def get_video_infos(clt):
    request = GetVideoInfosRequest.GetVideoInfosRequest()
    videoIds = ['videoId1', 'videoId2']
    request.set_VideoIds(','.join(videoIds))

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    videos = get_video_infos(clt)
    #print(videos['VideoList'])
    print(videos['NonExistVideoIds'])
    print(json.dumps(videos, ensure_ascii=False, indent=4))

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

修改视频信息

调用UpdateVideoInfo接口,完成修改视频信息功能。

接口参数和返回字段请参见UpdateVideoInfo。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateVideoInfoRequest
def update_video_info(clt, videoId):
    request = UpdateVideoInfoRequest.UpdateVideoInfoRequest()
    request.set_VideoId(videoId)
    request.set_Title('New Title') 
    request.set_Description('New Description')
    request.set_CoverURL('http192.168.0.0/16/tps/TB1qnJ1PVXXXXXCXXXXXXXXXXXX-700-700.png')  
    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('<AccessKeyId>', '<AccessKeySecret>')
    updateInfo = update_video_info(clt, '<videoId>')
    print(json.dumps(updateInfo, ensure_ascii=False, indent=4))

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

批量修改视频信息

调用UpdateVideoInfos接口,完成批量修改视频信息功能。

接口参数和返回字段请参见UpdateVideoInfos。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateVideoInfosRequest
def update_video_infos(clt):
    request = UpdateVideoInfosRequest.UpdateVideoInfosRequest()

    updateContent = []
    updateItem1 = {'VideoId': '<VideoId1>', 'Title': 'New Sample Title1', 'Tags': 'NewTag1,NewTag2'}
    updateItem2 = {'VideoId': '<VideoId2>', 'Title': 'New Sample Title2', 'Tags': 'NewTag3,NewTag4'}
    updateContent.append(updateItem1)
    updateContent.append(updateItem2)
    request.set_UpdateContent(json.dumps(updateContent))

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    update = update_video_infos(clt)
    print(update['NonExistVideoIds'])
    print(json.dumps(update, ensure_ascii=False, indent=4))

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

删除视频

调用DeleteVideo接口,完成删除视频功能。

接口参数和返回字段请参见DeleteVideo。调用示例如下:

from aliyunsdkvod.request.v20170321 import DeleteVideoRequest
def delete_videos(clt):
    request = DeleteVideoRequest.DeleteVideoRequest()
    videoIds = ['videoId1', 'videoId2']
    request.set_VideoIds(','.join(videoIds))  # 支持批量删除视频,多个用逗号分隔

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    delInfo = delete_videos(clt)
    print(json.dumps(delInfo, ensure_ascii=False, indent=4))

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

获取源文件信息(含原片下载地址)

调用GetMezzanineInfo接口,完成获取源文件信息功能。

接口参数和返回字段请参见GetMezzanineInfo。调用示例如下:

from aliyunsdkvod.request.v20170321 import GetMezzanineInfoRequest
def get_mezzanine_info(clt, videoId):
    request = GetMezzanineInfoRequest.GetMezzanineInfoRequest()
    request.set_VideoId(videoId)
    request.set_AuthTimeout(3600*5)
    request.set_accept_format('JSON')
    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    mezzanine = get_mezzanine_info(clt, '<videoId>')
    print(json.dumps(mezzanine, ensure_ascii=False, indent=4))

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

获取视频列表

调用GetVideoList接口,完成获取视频列表功能。

接口参数和返回字段请参见GetVideoList。调用示例如下:

注意

时间参数要使用UTC时间,北京时间与UTC的时差为+8,也就是UTC+8,例如2018-01-01T12:00:00Z表示北京时间2018年1月1日20点0分0秒。

from aliyunsdkvod.request.v20170321 import GetVideoListRequest
def get_video_list(clt):
    request = GetVideoListRequest.GetVideoListRequest()
    # 以获取最近30天的视频列表为例
    utcNow = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%SZ")
    utcMonthAgo = datetime.datetime.utcfromtimestamp(time.time() - 30*86400).strftime("%Y-%m-%dT%H:%M:%SZ")
    request.set_StartTime(utcMonthAgo)   # 视频创建的起始时间,为UTC格式
    request.set_EndTime(utcNow)          # 视频创建的结束时间,为UTC格式
    #request.set_Status('Uploading,Normal,Transcoding')  # 视频状态,默认获取所有状态的视频,多个用逗号分隔
    #request.set_CateId(0)               # 按分类进行筛选
    request.set_PageNo(1)
    request.set_PageSize(20)

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    videoList = get_video_list(clt)
    print(json.dumps(videoList, ensure_ascii=False, indent=4))

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

删除媒体流

调用DeleteStream接口,完成删除媒体流功能。

接口参数和返回字段请参见DeleteStream。调用示例如下:

from aliyunsdkvod.request.v20170321 import DeleteStreamRequest
# 可删除视频流或音频流信息及存储文件,并支持批量删除;删除后当CDN缓存过期,该路流会无法播放,请谨慎操作
def delete_stream(clt):
    request = DeleteStreamRequest.DeleteStreamRequest()
    request.set_VideoId('<videoId>')
    # 媒体流转码的作业ID列表,多个用逗号分隔;JobId可通过获取播放地址接口(GetPlayInfo)获取到。
    jobIds = ['jobId1', 'jobId2']
    request.set_JobIds(','.join(jobIds))
    request.set_accept_format('JSON')

    response = json.loads(clt.do_action_with_exception(request))
    return response

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    delStream = delete_stream(clt)
    print(json.dumps(delStream, ensure_ascii=False, indent=4))

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

批量删除源文件

调用DeleteMezzanines接口,完成批量删除源文件功能。

接口参数和返回字段请参见DeleteMezzanines。调用示例如下:

from aliyunsdkvod.request.v20170321 import DeleteMezzaninesRequest
def delete_mezzanines(clt):
    request = DeleteMezzaninesRequest.DeleteMezzaninesRequest()

    videoIds = ['videoId1', 'videoId2']
    request.set_VideoIds(','.join(videoIds))

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    delInfo = delete_mezzanines(clt)
    print(delInfo['NonExistVideoIds'])
    print(json.dumps(delInfo, ensure_ascii=False, indent=4))

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

批量更新图片信息

调用UpdateImageInfos接口,完成批量更新图片信息功能。

接口参数和返回字段请参见UpdateImageInfos。调用示例如下:

from aliyunsdkvod.request.v20170321 import UpdateImageInfosRequest
def update_image_infos(clt):
    request = UpdateImageInfosRequest.UpdateImageInfosRequest()

    updateContent = []
    updateItem1 = {'ImageId': '<imageId1>', 'Title': 'New Sample Title1', 'Tags': 'NewTag1,NewTag2'}
    updateItem2 = {'ImageId': '<imageId2>', 'Title': 'New Sample Title2', 'Tags': 'NewTag3,NewTag4'}
    updateContent.append(updateItem1)
    updateContent.append(updateItem2)
    request.set_UpdateContent(json.dumps(updateContent))

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    update = update_image_infos(clt)
    print(update['NonExistImageIds']['ImageId'])
    print(json.dumps(update, ensure_ascii=False, indent=4))

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

获取图片信息

调用GetImageInfo接口,完成获取图片信息功能。

接口参数和返回字段请参见GetImageInfo。调用示例如下:

from aliyunsdkvod.request.v20170321 import GetImageInfoRequest
def get_image_info(clt):
    request = GetImageInfoRequest.GetImageInfoRequest()
    request.set_ImageId('<imageId>')
    request.set_AuthTimeout(3600*24)

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    image = get_image_info(clt)
    print(image['ImageInfo'])
    print(json.dumps(image, ensure_ascii=False, indent=4))

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

删除图片

调用DeleteImage接口,完成删除图片功能。

接口参数和返回字段请参见DeleteImage。调用示例如下:

from aliyunsdkvod.request.v20170321 import DeleteImageRequest
def delete_image(clt):
    request = DeleteImageRequest.DeleteImageRequest()

    #根据ImageURL删除图片文件
    request.set_DeleteImageType('ImageURL')
    request.set_ImageURLs('http://192.168.0.0/16/cover.jpg')

    #根据ImageId删除图片文件
    #request.set_DeleteImageType('ImageId')
    #request.set_ImageIds('ImageId1,ImageId2')

    #根据VideoId删除指定ImageType的图片文件
    #request.set_DeleteImageType('VideoId')
    #request.set_VideoId('VideoId')
    #request.set_ImageType('SpriteSnapshot')

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    delInfo = delete_image(clt)
    print(json.dumps(delInfo, ensure_ascii=False, indent=4))

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