全部产品

媒体处理

更新时间:2020-11-30 17:49:56

本篇文档提供了Python SDK媒体处理模块相关功能的API调用示例,包含提交转码作业(HLS标准加密、普通)、提交截图作业、查询截图数据、导播台视频预处理。

初始化客户端

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

提交转码作业-普通

调用SubmitTranscodeJobs接口,完成提交转码作业-普通功能。

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

说明

此示例代码为普通转码(不加密)。阿里云视频加密方案请参见阿里云视频加密

"""
* 构建覆盖参数,目前只支持图片水印文件地址、文字水印的内容覆盖;如不使用可忽略
* 需要替换的水印信息对应水印ID必须是关联在指定的转码模板ID上(即TranscodeTemplateId)
* 不支持通过本接口去增加一个没有关联上的水印
"""
def build_override_params():
    # 以替换水印为例
    watermarks = []

    # 替换图片水印文件
    watermark1 = {'WatermarkId': '<watermarkId>', 'FileUrl': 'https://192.168.0.0/16/watermarks/sample.png'}
    watermarks.append(watermark1)

    # 替换文字水印
    watermark2 = {'WatermarkId': '<watermarkId>', 'Content': 'new Text'}
    watermarks.append(watermark2)

    return {'Watermarks': watermarks}


"""  发起转码处理   """
from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
def normal_submit_transcode_jobs(clt):
    request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()

    # 需要转码的视频ID
    request.set_VideoId('<videoId>')

    # 指定转码模板组
    request.set_TemplateGroupId('<templateGroupId>')

    """
    # 可选设置覆盖参数,如替换水印
    overrideParams = build_override_params()
    request.set_OverrideParams(json.dumps(overrideParams))
    """

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    jobs = normal_submit_transcode_jobs(clt)
    print(jobs['TranscodeJobs']['TranscodeJob'])
    print(json.dumps(jobs, ensure_ascii=False, indent=4))

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

提交转码作业-HLS标准加密

调用SubmitTranscodeJobs接口,完成提交转提交转码作业-HLS标准加密功能。

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

说明

此示例代码为使用HLS标准加密提交转码作业的示例。

"""
* 生成HLS标准加密参数的可选配置,用不到的可忽略
* 此时依赖KMS服务,需要安装KMS依赖包 aliyun-python-sdk-kms,并引入相关类
* 生成密钥API参考:https://192.168.0.0/16/document_detail/28948.html
"""
from aliyunsdkkms.request.v20160120 import GenerateDataKeyRequest
from aliyunsdkcore.http import protocol_type
def build_encrypt_config(clt):
    try:
        # 随机生成一个加密的秘钥,返回的response包含明文秘钥以及密文秘钥,
        # 视频标准加密只需要传递密文秘钥即可
        request = GenerateDataKeyRequest.GenerateDataKeyRequest()
        request.set_KeyId('<serviceKey>')
        request.set_KeySpec('AES_128')

        # KMS接口必须使用HTTPS访问
        request.set_protocol_type(protocol_type.HTTPS)
        request.set_accept_format('JSON')
        response = json.loads(clt.do_action_with_exception(request))

        # 解密接口地址,该参数需要将每次生成的密文秘钥与接口URL拼接生成,表示每个视频的解密的密文秘钥都不一样;注意您需要自己部署解密服务
        # Ciphertext作为解密接口的参数名称,可自定义,此处只作为参考
        decryptKeyUri = 'http://decrypt.demo.com/decrypt?' + 'Ciphertext=' + response['CiphertextBlob']

        return {'DecryptKeyUri': decryptKeyUri, 'KeyServiceType': 'KMS', 'CipherText': response['CiphertextBlob']}

    except Exception as e:
        print(e)
        #print(traceback.format_exc())
        return None


"""  发起转码处理   """
from aliyunsdkvod.request.v20170321 import SubmitTranscodeJobsRequest
def hlsencrypt_submit_transcode_jobs(clt):
    request = SubmitTranscodeJobsRequest.SubmitTranscodeJobsRequest()

    # 需要转码的视频ID
    request.set_VideoId('<videoId>')

    # 指定转码模板组
    request.set_TemplateGroupId('<templateGroupId>')

    # 使用KMS生成随机的加密密钥
    encryptConfig = build_encrypt_config(clt)
    if encryptConfig is not None:
        request.set_EncryptConfig(json.dumps(encryptConfig))

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

try:
    clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
    jobs = hlsencrypt_submit_transcode_jobs(clt)
    print(jobs['TranscodeJobs']['TranscodeJob'])
    print(json.dumps(jobs, ensure_ascii=False, indent=4))

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

提交截图作业

调用SubmitSnapshotJob接口,完成提交截图作业功能。

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

说明

创建截图模板详细请参见截图模板

from aliyunsdkvod.request.v20170321 import SubmitSnapshotJobRequest
def submit_snapshot_job(clt):
    request = SubmitSnapshotJobRequest.SubmitSnapshotJobRequest()

    # 需要截图的视频ID
    request.set_VideoId('<videoId>')

    # 截图模板ID
    #request.set_SnapshotTemplateId('<snapshotTemplateId>')

    # 如果设置了截图模板ID,则会忽略下面参数
    request.set_Count(50L)
    request.set_SpecifiedOffsetTime(0L)
    request.set_Interval(1L)
    request.set_Width(200)
    request.set_Height(200)

    # 雪碧图参数,如不使用可不设置
    spriteSnapshotConfig = {'CellWidth': 120, 'CellHeight': 68, 'Columns': 3,
                            'Lines': 10, 'Padding': 20, 'Margin': 50}
    # 保留雪碧图原始图
    spriteSnapshotConfig['KeepCellPic'] = 'keep'
    spriteSnapshotConfig['Color'] = 'tomato'
    request.set_SpriteSnapshotConfig(json.dumps(spriteSnapshotConfig))

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

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

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

查询截图数据

调用ListSnapshots接口,完成查询截图数据功能。

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

from aliyunsdkvod.request.v20170321 import ListSnapshotsRequest
def list_snapshots(clt):
    request = ListSnapshotsRequest.ListSnapshotsRequest()

    request.set_VideoId('<videoId>')
    request.set_SnapshotType('CoverSnapshot')
    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>')
    snapshots = list_snapshots(clt)
    print(snapshots['MediaSnapshot'])
    print(json.dumps(snapshots, ensure_ascii=False, indent=4))

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

导播台视频预处理

调用SubmitPreprocessJobs接口,完成导播台视频预处理功能。

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

from aliyunsdkvod.req uest.v20170321 import SubmitPreprocessJobsRequest
def submit_preprocess_jobs(clt):
    request = SubmitPreprocessJobsRequest.SubmitPreprocessJobsRequest()

    request.set_VideoId('<videoId>')
    request.set_PreprocessType('PreprocessType')

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

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

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