Python SDK examples for managing video AI jobs and templates in ApsaraVideo VOD. All examples use the aliyunsdkvod package (API version 2017-03-21).
Prerequisites
Before you begin, make sure that you have:
Python 3.x installed
The
aliyun-python-sdk-vodSDK package installed (pip install aliyun-python-sdk-vod)An AccessKey pair (AccessKey ID and AccessKey secret) with permissions for ApsaraVideo VOD
A VOD client initialized as described in Initialization
Usage notes
All examples initialize the client with an AccessKey pair. Store credentials in environment variables rather than hardcoding them.
For request and response parameter details, see the linked API reference for each operation.
To generate sample code for operations not covered here, open OpenAPI Explorer, find the operation in the left navigation pane, set the required parameters on the Parameters tab, then click Initiate Call. Select a language on the SDK Sample Code tab to view and download the code.
Initialize a client
Initialize a VOD client before calling any operation. For details, see Initialization.
Submit an AI job
Call the SubmitAIJob operation to submit an AI job.
Query AI jobs
Call the ListAIJob operation to query AI jobs.
Create an AI template
Call the AddAITemplate operation to create an AI template.
The following example creates an automated review template with specific audit items and ranges:
from aliyunsdkvod.request.v20170321 import AddAITemplateRequest
import json
import traceback
def add_ai_template(clt):
request = AddAITemplateRequest.AddAITemplateRequest()
# Template type: AIMediaAudit (automated review)
request.set_TemplateType('AIMediaAudit')
# User-defined template name
request.set_TemplateName('My AI Template')
# Template configurations
auditItem = ['terrorism', 'porn']
auditRange = ['video', 'image-cover', 'text-title']
auditContent = ['screen']
templateConfig = {
'AuditItem': auditItem,
'AuditRange': auditRange,
'AuditContent': auditContent,
'AuditAutoBlock': 'no'
}
request.set_TemplateConfig(json.dumps(templateConfig))
request.set_accept_format('JSON')
response = json.loads(clt.do_action_with_exception(request))
return response
try:
clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
res = add_ai_template(clt)
print(res['TemplateId'])
print(json.dumps(res, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())Replace the following placeholders with your values:
| Placeholder | Description | Example |
|---|---|---|
<AccessKeyId> | Your AccessKey ID | LTAI5tXxx |
<AccessKeySecret> | Your AccessKey secret | xXxXxXx |
Template configuration parameters
The TemplateConfig object accepts the following fields:
| Field | Type | Description |
|---|---|---|
AuditItem | Array of strings | Categories to check. Values: terrorism, porn. |
AuditRange | Array of strings | Media types to audit. Values: video, image-cover, text-title. |
AuditContent | Array of strings | Content source to analyze. Values: screen. |
AuditAutoBlock | String | Whether to automatically block flagged content. Values: yes, no. |
Modify an AI template
Call the UpdateAITemplate operation to modify an existing AI template.
The following example updates the template name, removes text-title from the audit range, and enables auto-blocking:
from aliyunsdkvod.request.v20170321 import UpdateAITemplateRequest
import json
import traceback
def update_ai_template(clt):
request = UpdateAITemplateRequest.UpdateAITemplateRequest()
# ID of the template to modify
request.set_TemplateId('<TemplateId>')
# New template name
request.set_TemplateName('New AI Template Name')
# Updated template configurations
auditItem = ['terrorism', 'porn']
auditRange = ['video', 'image-cover']
auditContent = ['screen']
templateConfig = {
'AuditItem': auditItem,
'AuditRange': auditRange,
'AuditContent': auditContent,
'AuditAutoBlock': 'yes'
}
request.set_TemplateConfig(json.dumps(templateConfig))
request.set_accept_format('JSON')
response = json.loads(clt.do_action_with_exception(request))
return response
try:
clt = init_vod_client('<AccessKeyId>', '<AccessKeySecret>')
res = update_ai_template(clt)
print(json.dumps(res, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())Replace <TemplateId> with the ID returned by AddAITemplate.
Query an AI template
Call the GetAITemplate operation to query details of a specific AI template.
Query AI templates
Call the ListAITemplate operation to retrieve a list of AI templates.
Set a default AI template
Call the SetDefaultAITemplate operation to set a default AI template.
Query the default AI template
Call the GetDefaultAITemplate operation to query details of the default AI template.
Delete an AI template
Call the DeleteAITemplate operation to delete an AI template.
References
| Operation | Description | API reference |
|---|---|---|
| SubmitAIJob | Submit an AI job | - |
| ListAIJob | Query AI jobs | - |
| AddAITemplate | Create an AI template | AddAITemplate |
| UpdateAITemplate | Modify an AI template | UpdateAITemplate |
| GetAITemplate | Query an AI template | GetAITemplate |
| ListAITemplate | List AI templates | ListAITemplate |
| SetDefaultAITemplate | Set the default AI template | SetDefaultAITemplate |
| GetDefaultAITemplate | Query the default AI template | GetDefaultAITemplate |
| DeleteAITemplate | Delete an AI template | - |