参考官方文档: Python上传SDK-普通文件上传、获取播放地址
本文只对上传视频和获取单个视频播放地址操作。更多案例如批量获取所有视频列表等操作请直接参考官方文档: 媒体管理
一、上传视频:上传完成后返回一个videoId参数,后续使用这个videoId获取视频播放地址
from voduploadsdk.AliyunVodUtils import *
from voduploadsdk.AliyunVodUploader import AliyunVodUploader
from voduploadsdk.UploadVideoRequest import UploadVideoRequest
import re
filePath = "F:/Download/新增红色视频/丹心映红六盘山——夜宿单家集探寻“红色密码”.mp4"
# print(filename.rindex('/'))
# print(filename[filename.rindex('/')+1: filename.index('.')])
filename = filePath[filePath.rindex('/')+1: filePath.index('.')]
# 测试上传本地音视频
def testUploadLocalVideo(accessKeyId, accessKeySecret, filePath, storageLocation=None):
try:
# 可以指定上传脚本部署的ECS区域。如果ECS区域和视频点播存储区域相同,则自动使用内网上传,上传更快且更省公网流量。
# ecsRegionId ="cn-shanghai"
# uploader = AliyunVodUploader(accessKeyId, accessKeySecret, ecsRegionId)
# 不指定上传脚本部署的ECS区域。
uploader = AliyunVodUploader(accessKeyId, accessKeySecret)
uploadVideoRequest = UploadVideoRequest(filePath, filename)
# 可以设置视频封面,如果是本地或网络图片可使用UploadImageRequest上传图片到视频点播,获取到ImageURL
# ImageURL示例:https://example.com/sample-****.jpg
# uploadVideoRequest.setCoverURL('<your Image URL>')
# 标签
# uploadVideoRequest.setTags('tag1,tag2')
if storageLocation:
uploadVideoRequest.setStorageLocation(storageLocation)
videoId = uploader.uploadLocalVideo(uploadVideoRequest)
print("file: %s, videoId: %s" % (uploadVideoRequest.filePath, videoId))
except AliyunVodException as e:
print(e)
testUploadLocalVideo('yourAccessKeyID', "yourAccessKeySecret", filePath)
将代码中的yourAccessKeyID和yourAccessSecret替换为自己阿里云主账号的AccessKeyID和AccessKeySecret或RAM 访问控制的用户的AccessKeyID和AccessKeySecret即可
二、根据videoId获取视频播放地址:在返回中的参数PlayURL即是播放地址
官方参数说明文档: GetPlayInfo接口说明
import json
import traceback
from aliyunsdkcore.client import AcsClient
def init_vod_client(accessKeyId, accessKeySecret):
regionId = 'cn-shanghai' # 点播服务接入地域
connectTimeout = 3 # 连接超时,单位为秒
return AcsClient(accessKeyId, accessKeySecret, regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout)
from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest
def get_play_info(clt, videoId):
request = GetPlayInfoRequest.GetPlayInfoRequest()
request.set_accept_format('JSON')
request.set_VideoId(videoId)
request.set_AuthTimeout(3600*5) # 设置播放地址的有效时间。单位:秒。
response = json.loads(clt.do_action_with_exception(request))
return response
try:
clt = init_vod_client('yourAccessKeyID', "yourAccessKeySecret")
playInfo = get_play_info(clt, '4aa4f9c0b73c71ed80110675a0ec0102') # 第二个参数是videoID
print(json.dumps(playInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
将代码中的yourAccessKeyID和yourAccessSecret替换为自己阿里云主账号的AccessKeyID和AccessKeySecret或RAM 访问控制的用户的AccessKeyID和AccessKeySecret即可
注:如在上传或获取地址等操作时报错,可能是因为使用的RAM用户没有权限,去对该用户搜索添加相应的权限即可
三、还可以使用sts获取token后访问
1、通过RAM访问控制里创建的用户获取AccessKeyID和AccessKeySecret,创建的角色里获取ARN(之前发布的文章里有具体说明这些参数的获取方式,这里就不赘述了)
2、通过AccessKeyID、AccessKeySecret和ARN获取SecurityToken、STS的AccessKeyID和AccessKeySecret用来接下来的访问识别图片文字的接口
参考官方api:STS SDK概览、安全令牌的获取(请在API接口中填写必要的参数后复制代码即可)
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdksts.request.v20150401.AssumeRoleRequest import AssumeRoleRequest
credentials = AccessKeyCredential('yourAccessKeyID', 'yourAccessSecret')
# use STS Token
# credentials = StsTokenCredential('<your-access-key-id>', '<your-access-key-secret>', '<your-sts-token>')
client = AcsClient(region_id='cn-shanghai', credential=credentials)
request = AssumeRoleRequest()
request.set_accept_format('json')
policy_text = '{"Version": "1","Statement": [{"Action": "vod:*","Resource": "*","Effect": "Allow"}]}' # 策略
request.set_RoleArn("yourRoleArn") # 在RAM访问控制中的角色里获取ARN
request.set_RoleSessionName("client") # 任意写
request.set_Policy(policy_text)
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
请将上面代码中的yourAccessKeyID、yourAccessKeySecret和yourRoleArn替换为第一步中获取的值即可
2,使用SecurityToken请求接口获取数据文章来源:https://www.toymoban.com/news/detail-427412.html
import json
import traceback
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.auth.credentials import StsTokenCredential
from aliyunsdkvod.request.v20170321 import GetPlayInfoRequest
from aliyunsdkvod.request.v20170321 import CreateUploadVideoRequest
def init_vod_client(accessKeyId, accessKeySecret, securityToken):
regionId = 'cn-shanghai' # 点播服务接入地域
connectTimeout = 3 # 连接超时,单位为秒
credential = StsTokenCredential(accessKeyId, accessKeySecret, securityToken)
return AcsClient(region_id=regionId, auto_retry=True, max_retry_time=3, timeout=connectTimeout, credential=credential)
def get_play_info(clt, videoId):
request = GetPlayInfoRequest.GetPlayInfoRequest()
request.set_accept_format('JSON')
request.set_VideoId(videoId)
request.set_AuthTimeout(3600*5)
response = json.loads(clt.do_action_with_exception(request))
return response
try:
clt = init_vod_client('yourAccessKeyID', 'yourAccessSecret', "yourSecurityToken")
playInfo = get_play_info(clt, '20b87bd0b73e71ed801d0674a2ce0102') # 第二个参数是videoID
print(json.dumps(playInfo, ensure_ascii=False, indent=4))
except Exception as e:
print(e)
print(traceback.format_exc())
将上面代码中的yourAccessKeyID、yourAccessKeySecret和yourSecurityToken替换成第三步第2小步骤代码返回的SecurityToken、AccessKeyId和AccessKeySecret即可文章来源地址https://www.toymoban.com/news/detail-427412.html
到了这里,关于阿里云视频点播上传视频和获取视频播放地址的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!