This topic describes how to use ApsaraVideo Media Processing (MPS) SDK for Java to transcode media files. This topic also provides the complete sample code.
For more information about the API operations that are used to manage transcoding jobs and parameters of the API operations, see SubmitJobs.
- Create an AcsClient instance.
DefaultProfile profile = DefaultProfile.getProfile( mpsRegionId, // The ID of the region in which you use the MPS service. accessKeyId, // The AccessKey ID of the RAM user. accessKeySecret); // The AccessKey secret of the RAM user. IAcsClient client = new DefaultAcsClient(profile);
- Create a request and set the required parameters.
SubmitJobsRequest request = new SubmitJobsRequest();
- Set the transcoding parameters.
Note
- When you submit a transcoding job by using the SDK, you must perform URL encoding for the objects. Otherwise, the transcoding job fails. For more information, see URL encoding.
- You must specify an object name that conforms to the naming conventions. Otherwise, the object cannot be found and the transcoding job fails. For more information about the naming conventions, see Parameter details.
- Input
JSONObject input = new JSONObject(); input.put("Location", ossLocation); input.put("Bucket", ossBucket); input.put("Object", URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20")); request.setInput(input.toJSONString());
- Output
String outputOSSObject = URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20"); JSONObject output = new JSONObject(); output.put("OutputObject", outputOSSObject);
- Container
JSONObject container = new JSONObject(); container.put("Format", "mp4"); output.put("Container", container.toJSONString());
- Video
Note If you transcode an object by using a transcoding template, you do not need to set this parameter. If you set this parameter, the specified value overwrites the value of the corresponding parameter in the transcoding template.
JSONObject video = new JSONObject(); video.put("Codec", "H.264"); video.put("Bitrate", "1500"); video.put("Width", "1280"); video.put("Fps", "25"); output.put("Video", video.toJSONString());
- Audio
Note If you transcode an object by using a transcoding template, you do not need to set this parameter. If you set this parameter, the specified value overwrites the value of the corresponding parameter in the transcoding template.
JSONObject audio = new JSONObject(); audio.put("Codec", "AAC"); audio.put("Bitrate", "128"); audio.put("Channels", "2"); audio.put("Samplerate", "44100"); output.put("Audio", audio.toJSONString());
- TemplateId
output.put("TemplateId", templateId);
- Container
- PipelineId
Note You can bind an MPS queue to a Message Service (MNS) topic or queue to receive notification messages. If you want to receive notification messages for an MPS queue, bind it to an MNS topic or queue before use.
request.setPipelineId(pipelineId);
- Send the request and handle the response or exception.
SubmitJobsResponse response; 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()); }
Sample code
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) {
// Create a DefaultAcsClient instance and initialize the instance.
DefaultProfile profile = DefaultProfile.getProfile(
mpsRegionId, // The ID of the region in which you use the MPS service.
accessKeyId, // The AccessKey ID of the RAM user.
accessKeySecret); // The AccessKey secret of the RAM user.
IAcsClient client = new DefaultAcsClient(profile);
// Create an API request and set the required parameters.
SubmitJobsRequest request = new SubmitJobsRequest();
// Input
JSONObject input = new JSONObject();
input.put("Location", ossLocation);
input.put("Bucket", ossBucket);
try {
input.put("Object", URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
request.setInput(input.toJSONString());
// Output
String outputOSSObject;
try {
outputOSSObject = URLEncoder.encode(ossInputObject, "utf-8").replace("+", "%20");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("output URL encode failed");
}
JSONObject output = new JSONObject();
output.put("OutputObject", outputOSSObject);
// Ouput->Container
JSONObject container = new JSONObject();
container.put("Format", "mp4");
output.put("Container", container.toJSONString());
// Ouput->Video
JSONObject video = new JSONObject();
video.put("Codec", "H.264");
video.put("Bitrate", "1500");
video.put("Width", "1280");
video.put("Fps", "25");
output.put("Video", video.toJSONString());
// Ouput->Audio
JSONObject audio = new JSONObject();
audio.put("Codec", "AAC");
audio.put("Bitrate", "128");
audio.put("Channels", "2");
audio.put("Samplerate", "44100");
output.put("Audio", audio.toJSONString());
// Ouput->TemplateId
output.put("TemplateId", templateId);
JSONArray outputs = new JSONArray();
outputs.add(output);
request.setOutputs(outputs.toJSONString());
request.setOutputBucket(ossBucket);
request.setOutputLocation(ossLocation);
// PipelineId
request.setPipelineId(pipelineId);
// Send the request and handle the response or exception.
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();
}
}
}