生活随笔
收集整理的這篇文章主要介紹了
【阿里云MPS】Demo
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
前言
- 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
準(zhǔn)備
- 開(kāi)通OSS(參考這里)且創(chuàng)建一個(gè)Bucket,并記下Bucket名字(testyeyaochangmingzi)和地域(cn-beijing)。
- 開(kāi)通MPS,并授權(quán)MPS可以訪問(wèn)OSS(授權(quán)完成后,多了一個(gè)名為AliyunMTSDefaultRole的角色)。參考這里。
- 創(chuàng)建用于MPS的RAM用戶(參考這里),并記下key(LTAI5tKfMJji4jVVNxxxxx)和secret(bhaC4jXeOIQnIt9xxxxxxx)。
- 開(kāi)啟管道
- 添加轉(zhuǎn)碼模板。也可以使用預(yù)制的模板。
- 向OSS中添加測(cè)試視頻(input.mp4),該視頻用于轉(zhuǎn)碼使用。
轉(zhuǎn)碼
參考這里。
Output 中的 Container、Video、Audio是可選參數(shù):
簡(jiǎn)化的代碼:
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();}}
}
總結(jié)
以上是生活随笔為你收集整理的【阿里云MPS】Demo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。