# -*- coding: utf-8 -*-
import json
from alibabacloud_imm20200930 import models as imm_20200930_models
from alibabacloud_imm20200930.client import Client as imm20200930Client
from alibabacloud_tea_openapi import models as open_api_models
class Sample:
def __init__(self):
pass
@staticmethod
def create_client(
access_key_id: str,
access_key_secret: str,
) -> imm20200930Client:
"""
使用AccessKey ID和AccessKey Secret初始化账号Client。
@param access_key_id:
@param access_key_secret:
@return: Client
@throws Exception
"""
config = open_api_models.Config(
# 填写您的AccessKey ID。
access_key_id=access_key_id,
# 填写您的AccessKey Secret。
access_key_secret=access_key_secret
)
# 填写访问的IMM域名。
config.endpoint = f'imm.cn-hangzhou.aliyuncs.com'
return imm20200930Client(config)
@staticmethod
def main() -> None:
# 填写IMM的AccessKey ID、AccessKey Secret,建议从配置中读取。
imm_access_key_id = "yourAccessKeyId"
imm_access_key_secret = "yourAccessKeySecret"
# 填写项目名称。
project_name = "test-project"
# 填写源视频的OSS URI。
source_uri = "oss://your-oss-bucket-name/test.avi"
# 目标URI。
target_uri_prefix = "oss://your-oss-bucket-name/output"
master_uri = f"{target_uri_prefix}/master.m3u8"
target_1080p_uri = f"{target_uri_prefix}/1080p/1080p"
target_720p_uri = f"{target_uri_prefix}/720p/720p"
target_540p_uri = f"{target_uri_prefix}/540p/540p"
# 初始化客户端。
client = Sample.create_client(imm_access_key_id, imm_access_key_secret)
# 创建 1080p Target。
target_1080p = imm_20200930_models.GenerateVideoPlaylistRequestTargets(
audio=imm_20200930_models.TargetAudio(
transcode_audio=imm_20200930_models.TargetAudioTranscodeAudio(
codec="aac",
bitrate=131072,
sample_rate=44100,
)
),
duration=10,
initial_segments=[2, 2, 2],
initial_transcode=36,
uri=target_1080p_uri,
video=imm_20200930_models.TargetVideo(
transcode_video=imm_20200930_models.TargetVideoTranscodeVideo(
codec="h264",
crf=26,
frame_rate=25,
resolution="1920x",
scale_type="fit",
)
),
)
# 创建 720p Target。
target_720p = imm_20200930_models.GenerateVideoPlaylistRequestTargets(
audio=imm_20200930_models.TargetAudio(
transcode_audio=imm_20200930_models.TargetAudioTranscodeAudio(
codec="aac",
bitrate=98304,
sample_rate=44100,
)
),
duration=10,
initial_segments=[2, 2, 2],
initial_transcode=36,
uri=target_720p_uri,
video=imm_20200930_models.TargetVideo(
transcode_video=imm_20200930_models.TargetVideoTranscodeVideo(
codec="h264",
crf=26,
frame_rate=25,
resolution="1280x",
scale_type="fit",
)
),
)
# 创建 540p Target。
target_540p = imm_20200930_models.GenerateVideoPlaylistRequestTargets(
audio=imm_20200930_models.TargetAudio(
transcode_audio=imm_20200930_models.TargetAudioTranscodeAudio(
codec="aac",
bitrate=65536,
sample_rate=44100,
)
),
duration=10,
initial_segments=[2, 2, 2],
initial_transcode=36,
uri=target_540p_uri,
video=imm_20200930_models.TargetVideo(
transcode_video=imm_20200930_models.TargetVideoTranscodeVideo(
codec="h264",
crf=26,
frame_rate=25,
resolution="720x",
scale_type="fit",
)
),
)
# 创建API请求。
req = imm_20200930_models.GenerateVideoPlaylistRequest(
project_name=project_name,
source_uri=source_uri,
master_uri=master_uri,
targets=[target_1080p, target_720p, target_540p]
)
# 打印API的请求值。
print(json.dumps(req.to_map(), indent=4))
# 发起请求
response = client.generate_video_playlist(req)
# 打印API的返回值。
print(json.dumps(response.body.to_map(), indent=4))
if __name__ == '__main__':
Sample.main()
package main
import (
"fmt"
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
imm "github.com/alibabacloud-go/imm-20200930/v2/client"
"github.com/alibabacloud-go/tea/tea"
)
func main() {
// 初始化IMM客户端。
immClient, err := imm.NewClient(&openapi.Config{
RegionId: tea.String("cn-hangzhou"), // 填写项目所在地域ID。
AccessKeyId: tea.String("your_access_key_id"), // 填写RAM用户的AccessKey ID。
AccessKeySecret: tea.String("your_access_key_secret"), // 填写RAM用户的AccessKey Secret。
})
if err != nil {
// 错误处理。
panic(err)
}
// 填写项目名称。
projectName := "test-project"
// 填写视频的OSS URI。
sourceUri := "oss://your-oss-bucket-name/test.avi"
// 目标URI。
targetURIPrefix := "oss://your-oss-bucket-name/output"
masterURI := targetURIPrefix + "/master.m3u8"
target1080pURI := targetURIPrefix + "/1080p/1080p"
target720pURI := targetURIPrefix + "/720p/720p"
target540pURI := targetURIPrefix + "/540p/540p"
target1080p := &imm.GenerateVideoPlaylistRequestTargets{
Audio: &imm.TargetAudio{
TranscodeAudio: &imm.TargetAudioTranscodeAudio{
Bitrate: tea.Int32(131072),
Codec: tea.String("aac"),
SampleRate: tea.Int32(44100),
},
},
Duration: tea.Float32(10),
InitialSegments: []*float32{tea.Float32(2), tea.Float32(2), tea.Float32(2)},
InitialTranscode: tea.Float32(36),
URI: tea.String(target1080pURI),
Video: &imm.TargetVideo{
TranscodeVideo: &imm.TargetVideoTranscodeVideo{
Codec: tea.String("h264"),
CRF: tea.Float32(26),
FrameRate: tea.Float32(25),
Resolution: tea.String("1920x"),
ScaleType: tea.String("fit"),
},
},
}
target720p := &imm.GenerateVideoPlaylistRequestTargets{
Audio: &imm.TargetAudio{
TranscodeAudio: &imm.TargetAudioTranscodeAudio{
Bitrate: tea.Int32(98304),
Codec: tea.String("aac"),
SampleRate: tea.Int32(44100),
},
},
Duration: tea.Float32(10),
InitialSegments: []*float32{tea.Float32(2), tea.Float32(2), tea.Float32(2)},
InitialTranscode: tea.Float32(36),
URI: tea.String(target720pURI),
Video: &imm.TargetVideo{
TranscodeVideo: &imm.TargetVideoTranscodeVideo{
Codec: tea.String("h264"),
CRF: tea.Float32(26),
FrameRate: tea.Float32(25),
Resolution: tea.String("1280x"),
ScaleType: tea.String("fit"),
},
},
}
target540p := &imm.GenerateVideoPlaylistRequestTargets{
Audio: &imm.TargetAudio{
TranscodeAudio: &imm.TargetAudioTranscodeAudio{
Bitrate: tea.Int32(65536),
Codec: tea.String("aac"),
SampleRate: tea.Int32(44100),
},
},
Duration: tea.Float32(10),
InitialSegments: []*float32{tea.Float32(2), tea.Float32(2), tea.Float32(2)},
InitialTranscode: tea.Float32(36),
URI: tea.String(target540pURI),
Video: &imm.TargetVideo{
TranscodeVideo: &imm.TargetVideoTranscodeVideo{
Codec: tea.String("h264"),
CRF: tea.Float32(26),
FrameRate: tea.Float32(25),
Resolution: tea.String("720x"),
ScaleType: tea.String("fit"),
},
},
}
// 创建API请求。
req := &imm.GenerateVideoPlaylistRequest{
ProjectName: tea.String(projectName),
SourceURI: tea.String(sourceUri),
MasterURI: tea.String(masterURI),
Targets: []*imm.GenerateVideoPlaylistRequestTargets{target1080p, target720p, target540p},
}
// 发起请求
res, err := immClient.GenerateVideoPlaylist(req)
if err != nil {
panic(err)
}
// 打印API的返回值。
fmt.Println("Response:", *res.Body)
}