生活随笔
收集整理的這篇文章主要介紹了
【阿里云MPS】Demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
前言
- jdk1.8
- MTS SDK(java版) 3.3.33
- aliyun-java-sdk-mts: https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-mts
- aliyun-java-sdk-core: https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core
準備
- 開通OSS(參考這里)且創建一個Bucket,并記下Bucket名字(testyeyaochangmingzi)和地域(cn-beijing)。
- 開通MPS,并授權MPS可以訪問OSS(授權完成后,多了一個名為AliyunMTSDefaultRole的角色)。參考這里。
- 創建用于MPS的RAM用戶(參考這里),并記下key(LTAI5tKfMJji4jVVNxxxxx)和secret(bhaC4jXeOIQnIt9xxxxxxx)。
- 開啟管道
- 添加轉碼模板。也可以使用預制的模板。
- 向OSS中添加測試視頻(input.mp4),該視頻用于轉碼使用。
轉碼
參考這里。
Output 中的 Container、Video、Audio是可選參數:
簡化的代碼:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.mts.model.v20140618.*;
public class SimpleTranscode {private static String accessKeyId
= "xxx";private static String accessKeySecret
= "xxx";private static String mpsRegionId
= "cn-hangzhou";private static String pipelineId
= "xxx";private static String templateId
= "S00000001-200010";private static String ossLocation
= "oss-cn-hangzhou";private static String ossBucket
= "xxx";private static String ossInputObject
= "input.mp4";private static String ossOutputObject
= "output.mp4";public static void main(String[] args
) {DefaultProfile profile
= DefaultProfile.getProfile(mpsRegionId
, accessKeyId
, accessKeySecret
); IAcsClient client
= new DefaultAcsClient(profile
);SubmitJobsRequest request
= new SubmitJobsRequest();JSONObject input
= new JSONObject();input
.put("Location", ossLocation
);input
.put("Bucket", ossBucket
);try {input
.put("Object", URLEncoder.encode(ossInputObject
, "utf-8"));} catch (UnsupportedEncodingException e
) {throw new RuntimeException("input URL encode failed");}request
.setInput(input
.toJSONString());JSONObject output
= new JSONObject();try {output
.put("OutputObject", URLEncoder.encode(ossOutputObject
, "utf-8"));} catch (UnsupportedEncodingException e
) {throw new RuntimeException("output URL encode failed");}output
.put("TemplateId", templateId
);JSONArray outputs
= new JSONArray();outputs
.add(output
);request
.setOutputs(outputs
.toJSONString());request
.setOutputBucket(ossBucket
);request
.setOutputLocation(ossLocation
);request
.setPipelineId(pipelineId
);SubmitJobsResponse response
;try {response
= client
.getAcsResponse(request
);System.out
.println("RequestId is:"+response
.getRequestId());if (response
.getJobResultList().get(0).getSuccess()) {System.out
.println("JobId is:" + response
.getJobResultList().get(0).getJob().getJobId());} else {System.out
.println("SubmitJobs Failed code:" + response
.getJobResultList().get(0).getCode() +" message:" + response
.getJobResultList().get(0).getMessage());}} catch (ServerException e
) {e
.printStackTrace();} catch (ClientException e
) {e
.printStackTrace();}}
}
總結
以上是生活随笔為你收集整理的【阿里云MPS】Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。