创建与查询转码任务
引导式阅读
Java
创建与查询转码任务
作者
C***
上架时间
2024-02-06 02:26:03

版本说明

本示例基于华为云SDK V3.0版本开发。

功能介绍

本示例介绍了如何使用媒体处理SDK发起转码任务,以及查询任务状态。

准备工作

1、已安装JDK1.8及以上版本并完成环境配置,可参考JDK安装完成配置。

2、已安装Maven,如若未安装,请下载安装。

3、已具备Eclipse等开发环境。

4、转码任务输入、输出的OBS桶必须是和endpoint是在同一个region,且已进行桶授权进行授权

5、查询region,请参考地区和终端节点

6、媒体处理SDK获取,请参考SDK

代码示例

1.创建转码任务,并查询任务状态,更多参数请参考API参考

public class TestTranscode { /** * 初始化 MpcClient * @return */ public static MpcClient getMpcClient() { HttpConfig httpConfig = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true).withTimeout(3); // 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全; // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。 String ak = System.getenv("HUAWEICLOUD_SDK_AK"); String sk = System.getenv("HUAWEICLOUD_SDK_SK"); //根据实际填写项目ID,华为云控制台账号名下“我的凭证”>API凭证下查看您的项目ID String projectId = "********************************"; //根据实际填写所需endpoint,这里以华东2为例 String endpoint = "https://mpc.cn-east-2.myhuaweicloud.com"; BasicCredentials auth = new BasicCredentials().withAk(ak).withSk(sk).withProjectId(projectId); return MpcClient.newBuilder() .withHttpConfig(httpConfig) .withCredential(auth) .withEndpoint(endpoint) .build(); } /** * 创建转码任务 */ public static void main() { //设置转码输入视频地址和输出视频路径 ObsObjInfo input = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("ok.mp4"); ObsObjInfo output = new ObsObjInfo().withBucket("mpc-east-2").withLocation("cn-east-2").withObject("output"); //创建转码请求 final int templateId = 7000530; CreateTranscodingTaskRequest request = new CreateTranscodingTaskRequest().withBody(new CreateTranscodingReq() .withInput(input) .withOutput(output) //设置转码模板,预置模板Id可以在MPC console页面上查看 .withTransTemplateId(Collections.singletonList(templateId)) ); try { CreateTranscodingTaskResponse response = getMpcClient().createTranscodingTask(request); System.out.println("CreateTranscodingTaskResponse=" + response); } catch (ConnectionException e) { System.out.println(e);; } catch (RequestTimeoutException e) { System.out.println(e);; } catch (ServiceResponseException e) { System.out.println(e);; System.out.println(e.getHttpStatusCode()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } catch (Exception e) { System.out.println(e); } } /** * 查询转码任务 */ @Test public void test_ListTranscode() { try { //按TaskId查询任务,TaskId是转码请求响应中返回的任务ID final long taskId = 2210718; ListTranscodingTaskResponse listTranscodingTaskResponse = getMpcClient().listTranscodingTask(new ListTranscodingTaskRequest().withTaskId(Collections.singletonList(taskId))); System.out.println(JsonUtils.toJSON(listTranscodingTaskResponse)); } catch (ConnectionException e) { System.out.println(e); } catch (RequestTimeoutException e) { System.out.println(e); } catch (ServiceResponseException e) { System.out.println(e); System.out.println(e.getHttpStatusCode()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } catch (Exception e) { System.out.println(e); } } }

参考

更多信息请参考APIExplorer