Video merging allows you to merge videos of different formats, bitrates, and resolutions into a longer video of a specific format, bitrate, and resolution. It is usually used to add opening or ending parts to a video, or to merge the clips of recorded live streams. Video clipping allows you to capture a clip from an original video, and save the clip as a new video. It is usually used to capture highlighted parts from videos. This topic describes how to merge and edit videos by using ApsaraVideo Media Processing (MPS) SDK for Java.
Sample code
www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqingwww.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-aimport com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.mps.sdk.utils.InitClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.mts.model.v20140618.SubmitJobsRequest;
import com.aliyuncs.mts.model.v20140618.SubmitJobsResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* ***** Usage notes ******
* This demo provides examples on how to merge and edit videos.
*
* ***** Methods ******
* mergrUrlListJob: merges multiple video clips in sequence.
* mergrConfigFileJob: merges video clips by using the Object Storage Service (OSS) path of a configuration file.
* openAndTailJob: embeds opening parts at the beginning of the input video or adds ending parts to the end of the input video.
*
* For more information about parameters, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a.
*/
public class MergeTransCodeJobs {
// The ID of the MPS queue. To view the ID of an MPS queue, log on to the MPS console and choose Global Settings > MPS Queue and Callback in the left-side navigation pane.
private static String pipelineId = "bee7a5b9*********a0cbf";
// The ID of the template. For more information about preset templates, visit https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/yuzhimobanxiangqing.
private static String templateId = "S00000001-200010";
public static void main(String[] args) throws ClientException {
// Initialize a client.
DefaultAcsClient client = InitClient.initMpsClient();
SubmitJobsResponse response;
try {
response = mergrUrlListJob(client);
System.out.println("RequestId is:" + response.getRequestId());
System.out.println("JobId is:" + JSON.toJSON(response.getJobResultList().get(0).getJob().getJobId()));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Merge multiple video clips in sequence.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrUrlListJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure the job input. Make sure that the media file used as the job input resides in the same region as the client.
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
// Configure the job output.
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/merge-out.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
// Construct data for video clipping.
JSONObject clip = new JSONObject();
// Cut the video from 1.000s to 5.030s.
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
// Cut the video from 1.000s to 5.030s that remains from the end of the video.
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
// Specifies whether to edit the first clip of the video. Valid values: true and false. A value of true specifies that the video clips are merged for a transcoding job after editing. A value of false specifies that the video clips are merged for a transcoding job before editing.
clip.put("ConfigToClipFirstPart", true);
// Construct data for video merging.
JSONArray mergeList = new JSONArray();
JSONObject merge = new JSONObject();
merge.put("MergeURL", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/test2mp4", "utf-8"));
merge.put("Start", "00:00:03.000");
merge.put("Duration", "00:00:13.000");
mergeList.add(merge);
output.put("Clip", clip);
output.put("MergeList", mergeList);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
// The OSS bucket that stores the output file.
request.setOutputBucket("<your bucket name>");
// The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* Merge video clips by using the OSS path of a configuration file.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrConfigFileJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure the job input. Make sure that the media file used as the job input resides in the same region as the client.
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
// Configure the job output.
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/merge-out.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
// Construct data for video clipping.
JSONObject clip = new JSONObject();
// Cut the video from 1.000s to 5.030s.
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
// Cut the video from 1.000s to 5.030s that remains from the end of the video.
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
// Specifies whether to edit the first clip of the video. Valid values: true and false. A value of true specifies that the video clips are merged for a transcoding job after editing. A value of false specifies that the video clips are merged for a transcoding job before editing.
clip.put("ConfigToClipFirstPart", true);
output.put("Clip", clip);
// The OSS path of the configuration file must be an HTTP URL.
output.put("MergeConfigUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/mps-test/demo/mergeConfigfile");
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
// The OSS bucket that stores the output file.
request.setOutputBucket("<your bucket name>");
// The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* Embed opening parts at the beginning of the input video or add ending parts to the end of the input video.
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse openAndTailJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
// Construct output parameters.
JSONArray outputs = new JSONArray();
// Configure the job input. Make sure that the media file used as the job input resides in the same region as the client.
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
// Configure the job output.
JSONObject output = new JSONObject();
try {
input.put("Object", URLEncoder.encode("mps-test/demo/test.mp4", "utf-8"));
String outPutObject = URLEncoder.encode("mps-test/demo/test-open-tail.mp4", "utf-8");
output.put("OutputObject", outPutObject);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("input URL encode failed");
}
output.put("TemplateId", templateId);
JSONArray openingList = new JSONArray();
JSONObject opening = new JSONObject();
opening.put("OpenUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/open.mp4", "utf-8"));
opening.put("Start", "3");
opening.put("Width", "680");
opening.put("Height", "480");
openingList.add(opening);
JSONArray tailSlateList = new JSONArray();
JSONObject tailSlate = new JSONObject();
tailSlate.put("TailUrl", "http://bucket-name.oss-cn-beijing.aliyuncs.com/" + URLEncoder.encode("mps-test/demo/tail.mp4", "utf-8"));
tailSlate.put("BlendDuration", "2");
tailSlate.put("Width", "680");
tailSlate.put("Height", "480");
tailSlate.put("IsMergeAudio", true);
tailSlate.put("BgColor", "White");
tailSlateList.add(tailSlate);
output.put("OpeningList", openingList);
output.put("TailSlateList", tailSlateList);
outputs.add(output);
request.setInput(input.toJSONString());
request.setOutputs(outputs.toJSONString());
// The OSS bucket that stores the output file.
request.setOutputBucket("<your bucket name>");
// The region in which the output OSS bucket resides. Specify the region in the oss-cn-**** format. Example: oss-cn-beijing.
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}