CodeSample小助手 2020-04-15
在使用本教程之前,请确保已完成以下操作:
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-mts -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-mts</artifactId>
<version>2.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.mts.model.v20140618.SubmitJobsRequest;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.sun.deploy.net.URLEncoder;
public class SubmitJobsTest {
// 初始化Client
public static IAcsClient createClient(){
// 创建DefaultAcsClient实例并初始化
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou", // 地域ID
"yourAccessKeyID", // RAM账号的AccessKey ID
"yourAccessKeySecret"); // RAM账号Access Key Secret
return new DefaultAcsClient(profile);
}
public static void main(String[] args) throws Exception {
IAcsClient client = createClient();
// 创建API请求并设置参数
SubmitJobsRequest request = new SubmitJobsRequest();
// 组装input参数
JSONObject input = new JSONObject();
// oss bucket 名称。
input.put("Bucket", "osstest");
// 输入OSS Bucket所在数据中心(OSS Location)。
input.put("Location", "osstest.oss-cn-hangzhou.aliyuncs.com");
// 输入文件 (OSS Object)。
input.put("Object", URLEncoder.encode("example.mp4", "UTF-8"));
request.setInput(input.toJSONString());
// 组装output。
// outputs是数组类型的JSON字符串
JSONArray outputs = new JSONArray();
JSONObject output = new JSONObject();
output.put("OutputObject",URLEncoder.encode("newTest.mp4", "UTF-8"));
output.put("TemplateId","75a4e3xxxxxxxxxxx6f2509");
// 字幕配置
JSONObject subtitleConfig = new JSONObject();
JSONArray extSubtitles = new JSONArray();
JSONObject extSubtitle = new JSONObject();
JSONObject subtitleInput = new JSONObject();
// 字幕文件导入入配置
subtitleInput .put("Bucket","osstest");
subtitleInput .put("Location","osstest.oss-cn-hangzhou.aliyuncs.com");
subtitleInput .put("Object",URLEncoder.encode("example.txt", "UTF-8"));
extSubtitle.put("Input",subtitleInput.toJSONString());
extSubtitle.put("CharEnc" ,"UTF-8");
extSubtitles.add(extSubtitle);
subtitleConfig.put("ExtSubtitleList", extSubtitles.toJSONString());
output.put("SubtitleConfig",subtitleConfig.toJSONString());
outputs.add(output);
request.setOutputs(outputs.toJSONString());
// 输出Bucket,需在控制台中完成云资源授权。
request.setOutputBucket("osstest");
// 管道ID。
request.setPipelineId("4da7xxxxxxxxxx41a4f2");
// 提交转码作业
SubmitJobsResponse response = client.getAcsResponse(request);
System.out.println(JSON.toJSONString(response,true));
}
}