边转边播任务

最近更新时间:2024-07-15 14:57:01

我的收藏

简介

边转边播任务功能是由 数据万象(Cloud Infinite,CI)提供的,数据万象将处理能力与 COS SDK 完全结合,您可以直接按照本篇文档指引进行使用。 本文档提供关于边转边播相关 API 概览以及 SDK 示例代码。
注意:
该功能需要 COS JAVA SDK v5.6.219.1 及以上版本。旧版本 SDK 该接口可能未覆盖或缺少更新参数,使用时建议升级到 最新版本
API
操作描述
生成边转边播的播放列表能够分析视频文件产出 m3u8 文件。生成播放列表后即时播放,并根据播放进度实施按需转码,相比离线转码能极大减少了转码等待时间并大幅度降低了转码和存储开销。

一、提交边转边播任务

功能说明

提交边转边播任务请求,在指定位置生成m3u8资源。

请求示例

public static void generatePlayList(COSClient client) { //1.创建任务请求对象 MediaJobsRequestV2 request = new MediaJobsRequestV2(); request.setBucketName("demo-1234567890"); //2.添加请求参数 参数详情请见api接口文档 request.setTag("GeneratePlayList"); request.getInput().setObject("1.mp4"); MediaContainerObject container = request.getOperation().getTranscode().getContainer(); container.setFormat("hls"); container.getClipConfig().setDuration("5"); MediaTransConfigObject transConfig = request.getOperation().getTranscode().getTransConfig(); transConfig.setCosTag("DemoTag=demo1&DemoTag2=demo2");
//hls加密操作 默认非加密 //transConfig.getHlsEncrypt().setIsHlsEncrypt("true"); MediaTranscodeVideoObject video = request.getOperation().getTranscode().getVideo(); video.setCodec("H.264"); video.setWidth("1280"); video.setHeight("960"); request.getOperation().getOutput().setBucket("demo-1234567890"); request.getOperation().getOutput().setRegion("ap-beijing"); request.getOperation().getOutput().setObject("output/media/test.m3u8"); //3.调用接口,获取任务响应对象 MediaJobResponseV2 response = client.createMediaJobsV2(request); System.out.println(Jackson.toJsonString(response)); }

参数和响应内容说明

请参考 API文档 中参数说明,与 SDK 字段一一对应,统一改为 Java 代码规范中的小驼峰式参数命名。

二、获取边转边播播放地址

2.1 获取未加密的播放地址

请求示例

public static void generatePlayListUrl(COSClient client) { String bucketName = "demo-1251704708"; String key = "output/media/test.m3u8"; // 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时) Date expirationDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000); // 填写本次请求的参数,需与实际请求相同,能够防止用户篡改此签名的 HTTP 请求的参数 Map<String, String> params = new HashMap<String, String>(); params.put("ci-process", "getplaylist"); params.put("expires", "43200"); Map<String, String> headers = new HashMap<String, String>(); HttpMethodName method = HttpMethodName.GET; URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params); System.out.println(url.toString()); }

返回结果说明

成功:返回 URL 实例对象。
失败:发生错误(如 Bucket 不存在),抛出异常 CosClientException 或者 CosServiceException。详情请参见 异常处理

2.2 获取加密播放地址

public static void getPlayListSimple(COSClient client) throws UnsupportedEncodingException { String bucketName = bucket; String key = objectKey; // 设置签名过期时间(可选), 若未进行设置则默认使用 ClientConfig 中的签名过期时间(1小时) Instant now = Instant.now(); Instant expire = now.plus(Long.parseLong(expires), ChronoUnit.SECONDS); Date expirationDate = Date.from(expire); String token = generateToken(appId, bucket, objectKey, secret, expirationDate); Map<String, String> params = new HashMap<String, String>(); params.put("ci-process", "getplaylist"); params.put("expires", "43200"); params.put("token-type", "JwtToken"); params.put("token",token); Map<String, String> headers = new HashMap<String, String>(); HttpMethodName method = HttpMethodName.GET; URL url = client.generatePresignedUrl(bucketName, key, expirationDate, method, headers, params); System.out.println(url.toString()); }

2.3 Jwt token 生成示例代码

public static String generateToken(String appId, String bucketId, String objectKey, byte[] secret, Date expires) throws UnsupportedEncodingException { Instant now = Instant.now(); String encodedObjectKey; encodedObjectKey = URLEncoder.encode(objectKey, "UTF-8"); Algorithm algorithm = Algorithm.HMAC256(secret); JWTCreator.Builder builder = JWT.create().withIssuer("client") .withIssuedAt(Date.from(now)) .withExpiresAt(expires) .withClaim("Type", "CosCiToken") .withClaim("AppId", appId) .withClaim("BucketId", bucketId) .withClaim("Object", encodedObjectKey) .withClaim("Issuer", "client") .withClaim("IssuedTimeStamp", now.getEpochSecond()) .withClaim("ExpireTimeStamp", expires.getTime()/1000) .withClaim("UsageLimit", 20) .withClaim("ProtectSchema", "rsa1024") // .withClaim("PublicKey", "xxx") .withClaim("ProtectContentKey", 0); return builder.sign(algorithm); }

返回结果说明

成功:返回URL实例对象。
失败:发生错误(如 Bucket 不存在),抛出异常 CosClientException 或者 CosServiceException。详情请参见 异常处理

三、播放地址使用

参见 COS 音视频播放器概述 选择合适的方式。

四、SDK 代码完整示例

请参考 GeneratePlayListDemo 获取完整示例。