拼接是把多個不同格式 、不同編碼、解析度的視頻拼接在一起,輸出成一個格式、編碼、解析度相同的新視頻。常用於添加固定的標題和片尾、直播錄製視頻拼接。剪輯是指裁剪視頻的某一段,輸出成一個新視頻。常用於截取視頻中精彩或關鍵的內容。本文介紹ApsaraVideo for Media ProcessingJava SDK進行視頻拼接和簡單剪輯的範例程式碼。
範例程式碼
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;
/**
* ***** 使用須知 ******
* 本demo為視頻拼接和簡單剪輯樣本
*
* ***** 方法介紹 ******
* mergrUrlListJob URL方式視頻拼接
* mergrConfigFileJob 設定檔方式視頻拼接
* openAndTailJob 拼接開板、尾板視頻
*
* 更多參數請參考 https://www.alibabacloud.com/help/zh/apsaravideo-for-media-processing/latest/parameter-details-a
*/
public class MergeTransCodeJobs {
//管道ID, 可以在控制台 【全域設定】->【管道】查看
private static String pipelineId = "bee7a5b9*********a0cbf";
//模板ID, 預置模板參考 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 {
//初始化調用 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();
}
}
/**
* 普通視頻拼接任務 URL方式
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrUrlListJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location地區和服務client地區一致
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
//構建一個輸出對象
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);
//構建一個素材截取資料
JSONObject clip = new JSONObject();
//代表從01秒000毫秒開始,截取到第5秒30毫秒為止
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
//代表從01秒000毫秒開始,截取到距離片尾剩餘5秒30毫秒為止
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
//true:先剪輯第一個片段,再拼接(轉碼)
clip.put("ConfigToClipFirstPart", true);
//構建一個拼接資料
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());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在地區,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 普通視頻拼接任務 ConfigFile方式
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse mergrConfigFileJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location地區和服務client地區一致
JSONObject input = new JSONObject();
input.put("Bucket", "<your bucket name>");
input.put("Location", "oss-cn-beijing");
//構建一個輸出對象
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);
//構建一個素材截取資料
JSONObject clip = new JSONObject();
//代表從01秒000毫秒開始,截取到第5秒30毫秒為止
//clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"Duration\":\"5.30\"}");
//代表從01秒000毫秒開始,截取到距離片尾剩餘5秒30毫秒為止
clip.put("TimeSpan", "{\"Seek\":\"00:00:01.000\",\"End\":\"5.30\"}");
//true:先剪輯第一個片段,再拼接(轉碼)
clip.put("ConfigToClipFirstPart", true);
output.put("Clip", clip);
//MergeConfigUrl的地址必須為HTTP地址
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());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在地區,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
/**
* 開板尾板拼接任務
* @param client
* @return
* @throws Exception
*/
public static SubmitJobsResponse openAndTailJob(DefaultAcsClient client) throws Exception {
SubmitJobsRequest request = new SubmitJobsRequest();
//構建輸出參數
JSONArray outputs = new JSONArray();
//構建input, 需要保證Location地區和服務client地區一致
JSONObject input = new JSONObject();
input.put("Location", "oss-cn-beijing");
input.put("Bucket", "<your bucket name>");
//構建一個輸出對象
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());
//指定輸出bucket
request.setOutputBucket("<your bucket name>");
//輸出bucket的所在地區,格式: oss-cn-****, 例如北京 oss-cn-beijing
request.setOutputLocation("oss-cn-beijing");
// PipelineId
request.setPipelineId(pipelineId);
return client.getAcsResponse(request);
}
}